Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: ARGV question

by toolic (Bishop)
on Aug 18, 2008 at 21:34 UTC ( [id://705077]=note: print w/replies, xml ) Need Help??


in reply to ARGV question

I just can't figure out what's getting assigned to $verbose
Just run some experiments...

> cat 705063.pl my $verbose = (@ARGV && $ARGV[0] eq '-v' && shift); if (defined $verbose) { print "verbose:$verbose:\n"; } else { print "verbose not defined\n"; } > > ./705063.pl verbose:0: > ./705063.pl -v verbose:-v: > ./705063.pl -v -b -n verbose:-v: > ./705063.pl -b verbose::

If no arguments are passed to the script, verbose will be 0.

If any arguments are passed to the script, and the 1st is NOT '-v', then verbose seems to be a zero-length string, but not 'undef'. Perhaps a more knowledgable Monk can explain why.

If any arguments are passed to the script, and the 1st is '-v', then verbose is '-v'.

If this behavior is not what you want and you have control over the code, I suggest re-writing it to achieve what you want in a way that you understand.

Replies are listed 'Best First'.
Re^2: ARGV question
by olus (Curate) on Aug 18, 2008 at 22:13 UTC

    I got curious about that behavior of the zero length result, went looking and now humbly share my findings.

    In chapter 3 of Programming Perl it is said that

    The equal and not-equal operators return 1 for true and "" for false (3.12)

    and

    The && and || operators differ from C's in that, rather than returning 0 or 1, they return the last value evaluated. (3.14)

    so, @ARGV is greater than 0 and passes, eq fails and returns '', and thus && returns the last value evaluated, which is ''.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://705077]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (None)
    As of 2024-04-25 04:00 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found