Command for login history in Strawberry Perl in Windows - perl

I am getting error i.e last is not recognized as a internal or external in cmd prompt
What I have tried:
#!/usr/bin/perl
use strict;
use warnings;
my $d=system("last");
print $d;

You can't use the last command under windows, you'll have to use something like wmic, net user or similar, depending on what info you need/want.

Related

Printing the result of the script(running in background) on the terminal

I am writing a perl script which runs another tcl script from it. The terminal doesn't print anything and waits for the tcl script to complete.
`chmod +x runme.tcl`; `./runme.tcl 2>&1`;
Can anyone please help me on how to print the results of the tcl script on the terminal instead of just waiting for it to get completed?
Thank you
system('chmod +x runme.tcl');
system('/runme.tcl 2>&1');
You can run tcl scripts directly from perl using the Tcl module without having to mess around with qx or system:
#!/usr/bin/env perl
use warnings;
use strict;
use Tcl;
Tcl->new->EvalFile("runme.tcl");
It'll share the same standard output as the perl script.
If you're using a new enough version of Tcl, you can easily create a safe interpreter to evaluate the script in case it tries to do anything nasty:
#!/usr/bin/env perl
use warnings;
use strict;
use Tcl v1.05;
my $interp = Tcl->new;
my $safeinterp = $interp->CreateSlave("safeinterp", 1);
$interp->Eval('interp share {} stdout safeinterp');
$interp->Eval('interp share {} stderr safeinterp');
$safeinterp->EvalFile("runme.tcl");
Backticks capture the output of an external command. You can write that output with a print command in front of the backticks.
`chmod +x runme.tcl`; print `./runme.tcl 2>&1`;

Use of "-w" in the shebang line

I am new to perl scripting and I am wondering what is the use of "-w" in hashbang of perl scripts.
#!/usr/bin/perl -w
my $userid = $ENV{USER};
print "$userid\n";
I know that this part #!/usr/bin/perl is used to inform shell that it is a perl script so that no need to use perl before running the script.
But I couldn't find the exact meaning for -w and most of the scripts I saw have this option .
Kindly let me know if that has any significance as I could not find any difference.
Edit:
Below document is very helpful(Suggested by #Toto)
https://perldoc.perl.org/5.32.0/perlrun.html
Everything after the command is passed as arguments to the command being invoked.
The -w option to the perl command is to enable warnings.

Communication between parent and child process - Perl

I'm running a perl program "a.pl" in the terminal that needs to call another program "b.pl" which then turns the environment to a tcl shell. The program "b.pl" sets environment variables which I have to use back in the main program "a.pl", after which I need to run new commands in the tcl environment created by "b.pl". Please see the example below
Program: a.pl
#!/usr/intel/bin/perl -w
use strict;
use warnings;
#turns it to a tcl shell and sets environment variable VERSION
system ("./b.pl");
system ("source <tclExecutable> -version $VERSION");
The second system command doesn't execute until I exit the tcl shell manually in the terminal. I've looked at fork and opening a pipe but I'm not sure how to go about it. I need to execute the second command in the tcl shell opened by the first system command. How can I make this work?
You may run b.pl "inside" a.pl using require. It may deliver what you want for simple scripts.
a.pl
use strict;
use warnings;
our $Version;
require "/.../b.pl"; # full path to b.pl script
print "Version: $Version\n";
b.pl
use strict;
use warnings;
our $Version;
$Version = "YES!";

Perl script doesn't open command prompt on running in Windows7

My Perl script doesn't execute at all when I press F5. In fact the command prompt also doesn't appear. Please help me as to what is wrong. The following is the Hello World script.
use 5.010;
use strict;
use warnings;
print "Hello World!\n";
The problem might be that your program runs so quickly that command prompt opens and closes faster than you can see it. To see if that's the case, and fix it if so, you can wait for user input:
print "Press ENTER to quit.\n";
scalar <>;

How to run node.js script without a file, from Perl?

I'm generating a node.js script inside a Perl program, and I want to run that through node.js, as a JavaScript interpreter. How canO run the node in $script without writing it to disk and then calling node, afterwards capturing the output.
I'm using the system command, which I think is good for this purpose.
Use IPC::Run or IPC::Open3.
use strictures;
use IPC::Run qw(run);
use autodie qw(:all run);
my $in = '… JavaScript goes here …';
my $out;
run ['node'], \$in, \$out;
Use open instead of system
#!/usr/bin/perl
open(FOO, "|node");
print FOO "console.log('hello world');";
Or if you don't need to do it from inside the perl script, just from your shell:
$ ./myscript.pl | node
Where myscript.pl exits after printing the javascript code