How to use -s option

J

James

Is there a way to assign a variable from the command line (without
exporting a variable) ?
I tried this, which does not work.

$ perl -s -e 'print $VAR' -VAR=123

In the script, -s option works fine;
#!/usr/bin/perl -s
but not in the command line?

James
 
P

Paul Lalli

James said:
Is there a way to assign a variable from the command line (without
exporting a variable) ?
I tried this, which does not work.

$ perl -s -e 'print $VAR' -VAR=123

In the script, -s option works fine;
#!/usr/bin/perl -s
but not in the command line?

Read perldoc perlrun for the usage of this option. The -VAR options
need to follow either the program name or the -- option. Since there
is no program name when using -e, you need to specify --, like so:

perl -s -le 'print $VAR' -- -VAR=123

Paul Lalli
 
C

chris-usenet

Paul Lalli said:
Read perldoc perlrun for the usage of this option. The -VAR options
need to follow either the program name or the -- option.

Quote,
-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 --).

I read this as meaning that the -s flag must be before an argument of
"--". You're saying it's the opposite?

Puzzled,
Chris
 
T

Tad McClellan

Quote,
-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 --).

I read this as meaning that the -s flag must be before an argument of
"--". You're saying it's the opposite?


The docs are talking about where the _switches_ that -s enables go,
not where the -s switch itself goes.

ie. if you use -s, then switches between the progname and the --
will be processed for you.
 
X

xhoster

Quote,
-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 --).

I read this as meaning that the -s flag must be before an argument of
"--". You're saying it's the opposite?

You are running a perl executable, which is in turn running a Perl program.
The executable has to decide which of the arguments are meant for it, and
which are meant to be passed on to the program that it is in turn running.

The -s switch tells the perl executable to preprocess some of the arguments
to the Perl program in a certain way.

$ perl -e 'print "ARGV is @ARGV"; print "VAR is $VAR"' \
-l -s -- -VAR=foo -- bar

ARGV is bar
VAR is foo

Arguments before the first '--' are arguments/switches to the perl
executable, not to the Perl program.

Arguments after the first '--' are arguments/switches to the Perl program,
(which in this case is the string specified by -e).

Arguments after the first '--' but before the second one are arguments to
the Perl program eligible to be munged at the command of the -s switch.

Arguments after the second '--' are arguments to the Perl program which
are protected from being munged by the -s switch.

Xho
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,950
Members
47,503
Latest member
supremedee

Latest Threads

Top