Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Copying a value from an element in the ARGV array to a variable...

by arturo (Vicar)
on Sep 17, 2001 at 19:54 UTC ( [id://112884]=note: print w/replies, xml ) Need Help??


in reply to Copying a value from an element in the ARGV array to a variable...

Just a note: you can test whether @ARGV has any members with the idiomatic

my $date; if (@ARGV) { $date = $ARGV[0]; }

Two things: @ARGV gets evaluated in a scalar context, which means that what is checked is the *number* of elements in the array; if that number is *not* 0 (i.e. if there are any elements in the array), the test evaluates to true, and the block will be executed.

Second note : the *declaration* of $date occurs outside the braces, so when you set $date inside the braces, that change will be visible to other code outside the braces. When you use my inside a set of braces, you're creating a *new* variable that is only visible to code inside the braces. You can see how this works with

my $date = "bar"; print "Outside 1 : $date\n"; { my $date = "foo"; print "Inside : $date\n"; } print "Outside 2 : $date\n";

Incidentally, you can make your $date setting very compact by using || or the "ternary operator" (see perlop):

my $date = $ARGV[0] || yesterday(); sub yesterday { my ($yr,$mo,$day) = (localtime( time - 86400 ))[5,4,3]; return sprintf "%04d%02d%02d",1900+$yr,1+$mo,$day; }

if $ARGV[0] evaluates to false ( because it doesn't exist, or was 0 ), then $date gets set to what the yesterday sub returns.

HTH. /msg me if you need anything explained.

perl -e 'print "How sweet does a rose smell? "; chomp ($n = <STDIN>); +$rose = "smells sweet to degree $n"; *other_name = *rose; print "$oth +er_name\n"'

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://112884]
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: (4)
As of 2024-04-25 18:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found