Expanding AM's reply, when we use shebang (#!/usr/bin/perl), we have an advantage to make our program "a real program" by turning our source file's execute bit on. In this case, the OS will call the correspondent interpreter program (perl in this case) to process the file in question, and all perl switches (-w, -s, -T, etc) are taken into account. So we're able to say,
$ ./program.pl # or if it's in known PATH: $ program.pl
Of course, this only applies if the underlying OS supports it, such as Linux.

OTOH, we are of course able to call the perl interpreter directly and feed it with the source file. This way, we can provide some extra switches not exist in the shebang, or even overriding switches in the shebang. It also means that executing a source file this way doesn't require the shebang to be declared in the source file. However, the perl interpreter will consider it if it does exist.

Having said that, I think you can assure yourself about that "pick one", regardless of using -s (with our without argument to your own swithces) or not. Now let me suggest you that, if you're really serious about providing command line arguments for your program, use the more elegant modules from CPAN for parsing the arguments for you. One of them is Getopt::Long that comes with the standard Perl distribution.

$ cat foo.pl #!/usr/bin/perl use strict; use warnings; use Getopt::Long; my %opts; GetOptions(\%opts, 'foo=s', 'v'); # '=s' modifier means foo needs argument, and it can be # any string (another modifier is '=i' for integer.) print "foo = $opts{foo}, verbose = $opts{v}\n"; $ perl foo.pl --foo=bar -v foo = bar, v = 1 $ ./foo.pl --for=bar -v foo = bar, v = 1
Now, I'd say "pick any" instead of "pick one" :-) You don't have to use a hash to store the arguments, you can use direct scalar.
$ cat foo.pl #!/usr/bin/perl use strict; use warnings; use Getopt::Long; use vars qw($foo, $verbose); GetOptions('foo=s' => \$foo, 'v' => \$verbose); # '=s' modifier means foo needs argument, and it can be # any string (another modifier is '=i' for integer.) print "foo = $foo, verbose = $verbose\n"; $ perl foo.pl --foo=bar -v foo = bar, v = 1 $ ./foo.pl --for=bar -v foo = bar, v = 1
See Getopt::Long for detail.

Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!


In reply to Re^3: unexpected results using -s switch by naikonta
in thread unexpected results using -s switch by mojodaddy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.