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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |