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"'

In reply to Re: Copying a value from an element in the ARGV array to a variable... by arturo
in thread Copying a value from an element in the ARGV array to a variable... by joelw

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.