To your first question: we can't be sure the number of days is the SECOND CLI argument (which is what you're extracting in what you've shown -- $ARGV[1]); if the first CLI argument is what you want to feed to $days, then use $ARGV[0].

Here are two examples without the overhead of module loading:

#!/usr/bin/perl use 5.018; use warnings; # 1110785 my $days; if (@ARGV) { $days = $ARGV[0]; say $days; }else{ say "n\t Usage: 1110785.pl n \n"; } deleteData($days); sub deleteData { $days = shift (@_); say "days in sub: $days"; } say "back in main."; =head EXECUTION C:\>D:\_Perl_\PMonks\1110785.pl 3 3 days in sub: 3 back in main. =cut

or (some would say better style):

# ... # 1110785a my $days; sub deleteData { if (@ARGV) { $days = $ARGV[0]; say $days; }else{ say "n\t Usage: 1110785.pl n \n"; } $days = shift (@ARGV); say "days in sub: $days"; # ... } deleteData; say "back in main."; =head EXECUTION C:\>D:\_Perl_\PMonks\1110785a.pl 8 8 days in sub: 8 back in main. =cut

As to the your second question, why not 'try it to see?'


In reply to Re: Use command line argument in script by ww
in thread Use command line argument in script by homer4all

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.