Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using -e and -s switch in perl

Tags:

perl

I am starting to lern Perl. I tried

perl -e 'print "The value of \$x=$x\n";'

gives:

The value of $x=

whereas:

perl -s -x=10 -e 'print "The value of \$x=$x\n";'

gives

No Perl script found in input

I would like to use the -s switch together with -e switch.. Why is it not working?

like image 835
Håkon Hægland Avatar asked Feb 01 '14 08:02

Håkon Hægland


People also ask

Can we use switch in perl?

Perl does not support the use of switch statements. A switch statement allows a variable to be tested against a set list of values. In Perl, the equivalent of the switch statement is the given-when syntax. We state the values as given-when .

How to write switch case in perl?

The switch statement takes a single scalar argument of any type, specified in parentheses. The value is followed by a block, which may contain one or more case statement followed by a block of Perl statement(s).

What is $_ in perl?

The most commonly used special variable is $_, which contains the default input and pattern-searching string. For example, in the following lines − #!/usr/bin/perl foreach ('hickory','dickory','doc') { print $_; print "\n"; }


2 Answers

Arguments to the script itself should come after the script, regardless of whether it's a one-liner or a file. While not always necessary, you can separate the arguments for perl from the arguments to your script with a --:

$ perl -s -E'say "\$x=$x"' -- -x=42
x=42

Ending the arguments to perl with -- is necessary because -x is a flag understood by perl itself, so your script would never see it. From perldoc perlrun:

-x

-xdirectory

tells Perl that the program is embedded in a larger chunk of unrelated text, such as in a mail message. Leading garbage will be discarded until the first line that starts with "#!" and contains the string "perl". Any meaningful switches on that line will be applied.

[…]

If a directory name is specified, Perl will switch to that directory before running the program. The -x switch controls only the disposal of leading garbage. The program must be terminated with "END" if there is trailing garbage to be ignored; the program can process any or all of the trailing garbage via the "DATA" filehandle if desired.

The directory, if specified, must appear immediately following the -x with no intervening whitespace.

As the specified input print "The value of \$x=$x\n"; does not contain a shebang, the actual script wasn't found and thus the error you encountered was emitted.

Do not use -s switch parsing for any but the most simple one-liners. Use Getopt::Long instead.

like image 68
amon Avatar answered Oct 18 '22 22:10

amon


perl -se 'print "The value of \$x=$x\n";' -- -x=10

perlrun documentation could be more clear on this option,

-s enables rudimentary switch parsing for switches on the command line after the program name but before any filename arguments (or before an argument of --). Any switch found there is removed from @ARGV and sets the corresponding variable in the Perl program. The following program prints "1" if the program is invoked with a -xyz switch, and "abc" if it is invoked with -xyz=abc.

like image 45
mpapec Avatar answered Oct 18 '22 22:10

mpapec