From owner-freebsd-questions@FreeBSD.ORG Tue Apr 6 14:22:15 2010 Return-Path: Delivered-To: freebsd-questions@FreeBSD.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 370A8106564A for ; Tue, 6 Apr 2010 14:22:15 +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 A1D0F8FC1E for ; Tue, 6 Apr 2010 14:22:14 +0000 (UTC) X-Spam-Status: No X-Hellug-MailScanner-From: keramida@ceid.upatras.gr X-Hellug-MailScanner-SpamCheck: not spam, SpamAssassin (not cached, score=-2.9, required 5, autolearn=not spam, ALL_TRUSTED -1.00, BAYES_00 -1.90) X-Hellug-MailScanner: Found to be clean X-Hellug-MailScanner-ID: o36EJ1Rx023730 Received: from kobe.laptop (ppp-94-64-237-90.home.otenet.gr [94.64.237.90]) (authenticated bits=128) by igloo.linux.gr (8.14.3/8.14.3/Debian-9.1) with ESMTP id o36EJ1Rx023730 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NOT); Tue, 6 Apr 2010 17:19:11 +0300 Received: from kobe.laptop (kobe.laptop [127.0.0.1]) by kobe.laptop (8.14.4/8.14.4) with ESMTP id o36EJ1AV019925 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Tue, 6 Apr 2010 17:19:01 +0300 (EEST) (envelope-from keramida@ceid.upatras.gr) Received: (from keramida@localhost) by kobe.laptop (8.14.4/8.14.4/Submit) id o36EIxE6019900; Tue, 6 Apr 2010 17:18:59 +0300 (EEST) (envelope-from keramida@ceid.upatras.gr) From: Giorgos Keramidas To: kline@thought.org References: <20100403210610.GA4135@thought.org> <4BB8108A.9080104@FreeBSD.org> <1270371713.5861.98.camel@tao.thought.org> Date: Tue, 06 Apr 2010 17:18:59 +0300 In-Reply-To: <1270371713.5861.98.camel@tao.thought.org> (Gary Kline's message of "Sun, 04 Apr 2010 02:01:53 -0700") Message-ID: <874ojor7e4.fsf@kobe.laptop> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.0.50 (berkeley-unix) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Cc: glarkin@FreeBSD.org, FreeBSD Mailing List Subject: Re: perl qstn... 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: Tue, 06 Apr 2010 14:22:15 -0000 On Sun, 04 Apr 2010 02:01:53 -0700, Gary Kline wrote: > thanks for your url as well and the others to posted. but it seems > like overkill since i dont need any explicit option or argument. i > just need the script to tell me whether i have an arg or not. > following is something i've kept in one of my junk drawers from when i > was learning to write bourne sscripts. it uses the "$[token]" syntax > that determines whether there are Any args on the cmdline. if not, > the script prints a message and exits. > > #!/bin/sh > if [ $# -eq 0 ] > then > echo "No args; need filename." > else > echo "$1" > fi > > After a couple hours experimentation, the following does the same for my > perl scripts: > > > #!/usr/bin/perl > $argc = @ARGV; > if (! $argc ) { > printf("No args; need filename.\n"); > } > else { > printf("%s\n", @ARGV); > } Yes, that's very close to the sh(1) version. Perl's behavior in this case is described in the 'perlvar' manpage: @ARGV The array @ARGV contains the command-line arguments intended for the script. $#ARGV is generally the number of arguments minus one, because $ARGV[0] is the first argument, not the program's command name itself. See $0 for the command name. In other words, when @ARGV appears in "scalar context" it yields the 'size' of the @ARGV array, e.g.: % cat foo.pl printf("%d .. args = [%s]\n", int(@ARGV), join(', ', (@ARGV))); % perl foo.pl 0 .. args = [] % perl foo.pl 1 1 .. args = [1] % perl foo.pl 1 2 3 3 .. args = [1, 2, 3] So when int(@ARGV) is zero you know that there are no arguments at all. This means you can write your sh version like this in Perl: #!/usr/bin/perl if (int(@ARGV) == 0) { die "No args; at least one filename expected"; } printf("%s\n", join(' ', (@ARGV))); This is "good enough" as a command-line handling trick for really simple scripts, but you should probably have a look at the Getopt::Std and the Getopt::Long modules for longer scripts. Using them will make your option parsing code much cleaner and easier to change in the future.