Date: Fri, 28 Apr 2006 03:20:16 +0300 From: Giorgos Keramidas <keramida@ceid.upatras.gr> To: Gary Kline <kline@tao.thought.org> Cc: freebsd-questions@freebsd.org, Arne Skjaerholt <arnsholt@broadpark.no> Subject: Re: scripting languages... Message-ID: <20060428002016.GB1742@gothmog.pc> In-Reply-To: <20060427235828.GD2601@thought.org> References: <20060427024158.GA71123@thought.org> <20060427031043.GA69851@gothmog.pc> <20060427214854.GA2601@thought.org> <1146188104.7085.8.camel@bursar> <20060427235828.GD2601@thought.org>
next in thread | previous in thread | raw e-mail | index | archive | help
On 2006-04-27 16:58, Gary Kline <kline@tao.thought.org> wrote: > > Getting at argv/argc is actually pretty simple in Perl. The global array > > @ARGV contains the arguments given on the command-line, but not the name > > of the file (this datum is contained in $0). Therefore your argv[1] in C > > is $ARGV[0] in Perl. The number of command-line arguments can be > > obtained in two ways, either you interpret the array in a scalar context > > and get its length: ``my $argc = scalar @ARGV'' or you use the last > > index of the array and add one: ``my $argc = $#ARGV + 1''. Of course, in > > most cases you'll just want to loop over the command-line args, so a > > foreach loop should suffice, or of course one of the Getopt (Getopt::Std > > or Getopt::Long in most cases) modules. > > So, could I say: > > my $argc = $#ARGV+1; $count = 0; > while ($argc--) > { > if (! (checkErr($ARGV[$count], $count))) > { > printf("Processing %s\n", $ARGV[$count]); > doWhatever($ARGV[$count]); > } > $count++; > } > > or something close-to!? I believe the idiomatic way of doing this would be something more like: foreach $arg (@ARGV) { if (!checkErr($arg)) { printf("Processing %s\n", $arg); doWhatever($arg); } } Your version may work too, but I'm always wary of all the index trickery involved in handling $#ARGV fearing it may easily lead to off-by-one bugs. So I prefer foreach() loops :)
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20060428002016.GB1742>