Connecting Tech Pros Worldwide Forums | Help | Site Map

Perl: I do not pass parameters to usage() and @_ is defined yet ???

Newbie
 
Join Date: Oct 2007
Posts: 1
#1: Oct 16 '07
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. use Getopt::Long;
  3.  
  4.  
  5.  
  6.  
  7. $l=@ARGV;
  8.  
  9. print "The number of args is " . @ARGV;print "\n"; 
  10.  
  11. my  $bool= GetOptions('help|?' => \$help, 'url=s' => \$url, 'size=i' => \$size);
  12.  
  13.  
  14. ;
  15. if ( !$bool    or $l < 1 || defined $help ){
  16. &usage();
  17.  
  18. print "=======\n";
  19. print "THE END";
  20.   if (@_ ){
  21.   print "Unknown option: @_\n"}
  22.   print "usage: program [--url URL] [--size SIZE] [--help|-?]\n";
  23.  
  24. }  
  25.  
OUTPUT:
=======
C:\>perld.pl --url www --size 15 -david -fass
The number of args is 6
Unknown option: david
Unknown option: fass


usage: program [--url URL] [--size SIZE] [--help|-?]
=======

please explain why the "additional arguments of perld.pl" are passed through sub usage() to @_????
but I call to sub usage() without parameters????????!!!!!!!!!

KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#2: Oct 16 '07

re: Perl: I do not pass parameters to usage() and @_ is defined yet ???


@_ is global. It will display whatever it's last values were. Since you did not pass any arguments to usage(), @_ is still the values of the paramaters you passed to the script.
Reply