From owner-freebsd-questions@FreeBSD.ORG Fri Apr 28 00:20:39 2006 Return-Path: X-Original-To: freebsd-questions@freebsd.org Delivered-To: freebsd-questions@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 1873416A404 for ; Fri, 28 Apr 2006 00:20:39 +0000 (UTC) (envelope-from keramida@ceid.upatras.gr) Received: from igloo.linux.gr (igloo.linux.gr [62.1.205.36]) by mx1.FreeBSD.org (Postfix) with ESMTP id 48F5B43D45 for ; Fri, 28 Apr 2006 00:20:37 +0000 (GMT) (envelope-from keramida@ceid.upatras.gr) Received: from gothmog.pc (aris.bedc.ondsl.gr [62.103.39.226]) (authenticated bits=128) by igloo.linux.gr (8.13.6/8.13.6/Debian-1) with ESMTP id k3S0KBsi009222 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NOT); Fri, 28 Apr 2006 03:20:15 +0300 Received: from gothmog.pc (gothmog [127.0.0.1]) by gothmog.pc (8.13.6/8.13.6) with ESMTP id k3S0KGtI002546; Fri, 28 Apr 2006 03:20:16 +0300 (EEST) (envelope-from keramida@ceid.upatras.gr) Received: (from giorgos@localhost) by gothmog.pc (8.13.6/8.13.6/Submit) id k3S0KGfY002545; Fri, 28 Apr 2006 03:20:16 +0300 (EEST) (envelope-from keramida@ceid.upatras.gr) Date: Fri, 28 Apr 2006 03:20:16 +0300 From: Giorgos Keramidas To: Gary Kline Message-ID: <20060428002016.GB1742@gothmog.pc> References: <20060427024158.GA71123@thought.org> <20060427031043.GA69851@gothmog.pc> <20060427214854.GA2601@thought.org> <1146188104.7085.8.camel@bursar> <20060427235828.GD2601@thought.org> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20060427235828.GD2601@thought.org> X-Hellug-MailScanner: Found to be clean X-Hellug-MailScanner-SpamCheck: not spam, SpamAssassin (score=-3.392, required 5, autolearn=not spam, ALL_TRUSTED -1.80, AWL 0.81, BAYES_00 -2.60, DNS_FROM_RFC_ABUSE 0.20) X-Hellug-MailScanner-From: keramida@ceid.upatras.gr X-Spam-Status: No Cc: freebsd-questions@freebsd.org, Arne Skjaerholt Subject: Re: scripting languages... X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 28 Apr 2006 00:20:39 -0000 On 2006-04-27 16:58, Gary Kline 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 :)