Run perl -e from inside perl script in windows

I need to run the following command from a Perl script on Windows. The code couldn't be simpler:

#! C:\Perl\bin\perl

perl -e "print qq(Hello)";

I save this file as test.pl. I open a command prompt on Windows and run the c:\Per\binfollowing from a directory . When I run it as perl test.pl, I get the following result:

C:\Perl\bin>perl test.pl
syntax error at test.pl line 3, near ""perl -e "print"
Execution of test.pl aborted due to compilation errors.

How can i fix this? If I just run perl -efrom the command line (i.e. not being inside the file), it works fine.

+3
source share
5 answers

The file test.plshould contain:

print qq(Hello);
+4
source

Perl- perl -e …? .

, , , , / / . system, qx open.

+2

Perl, system, .

LIST LIST , , , , . , , , ...

:

#! perl

system("perl", "-le", "print qq(Hello)") == 0
  or warn "$0: perl exited " . ($? >> 8);

Remember that systemruns a command with output to standard output. If you want to capture the output, do as in

open my $fh, "-|", "perl", "-le", "print qq(Hello)"
  or die "$0: could not start perl: $!";

while (<$fh>) {
  print "got: $_";
}

close $fh or warn "$0: close: $!";

As with c system, opening a command specified as a list of several items bypasses the shell.

+2
source

I don’t know why you need this, but:

#!C:\Perl\bin\perl

`perl -e "print qq(Hello)"`;
+1
source

Why not use eval ?

0
source

Source: https://habr.com/ru/post/1759893/


All Articles