This is probably blindingly simple but I'm not understanding why @ARGV
is not reduced to () (no args)? in this example

I'm trying to understand what happens with ARGV in s getops loop with
arguments after the getopts Options, or any time there is ARGV, I
guess.

Why doesn't the `for loop' reduce ARGV to `()'?

The script included at the end, when given these arguments:

  `./test.pl  -a A -b B one two three'

Generates this output:

  ARGV starts with <7> members
  $opt_a is <A>
  $opt_b is <B>
  ARGV is now reduced to <3> members:
     one
     two
     three
  Now lets ditch the rest in a for loop
  Ditching <one>
  Ditching <two>
  ARGV after shifting away args in `for' loop:
       ARGV=<three>
  OOOPS... ARGV still has <1> member

-------        ---------       ---=---       ---------      -------- 
#!/usr/local/bin/perl

use strict;
use warnings;

print "ARGV starts with <"....@argv."> members\n";

## ==== BEGIN Getopts section ====
## Declare vars inside our()
our ($opt_a,$opt_b);
use Getopt::Std;
my $optstr ="a:b:";
getopts($optstr);

if ($opt_a){
  print "\$opt_a is <$opt_a>\n";
}

if ($opt_b){
  print "\$opt_b is <$opt_b>\n";
}

print "ARGV is now reduced to <"....@argv."> members:\n";

for (@ARGV){
   print "$_\n";
}

print "Now lets ditch the rest in a for loop\n";
for (@ARGV){
  my $ditch = shift;
  print "Ditching <$ditch>\n";
  $ditch = '';
}
print "ARGV after shifting away args in \`for' loop:
     ARGV=<@ARGV>
OOOPS... ARGV still has <". @ARGV ."> member\n";

@ARGV = ();



-- 
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/


Reply via email to