Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Processing ARGV

by kmullin (Acolyte)
on Dec 05, 2007 at 17:44 UTC ( #655145=perlquestion: print w/ replies, xml ) Need Help??
kmullin has asked for the wisdom of the Perl Monks concerning the following question:

I am a perl newbie, and am feeling very frustrated with this language. All I want to do is process command line arguments. In my PERL book, I see how to do this. So, I have a command:

#!/opt/third/party/bin/perl -w print "Start of testARGV.pl\n"; if ($#ARGV != 0) { foreach( @ARGV ) { print $_; } }

But when I try to run this, I get:

Start of testARGV.pl ./testARGV.pl:3: command not found: 0 ./testARGV.pl:5: unknown file attribute

What is going on here. The only '0' in my program is in the test to see if I even have command line arguments.

I'd appreciate any perls of wisdom you may have for me.

Comment on Processing ARGV
Select or Download Code
Re: Processing ARGV
by moritz (Cardinal) on Dec 05, 2007 at 17:56 UTC
    It looks as if the script is not executed by perl at all.

    Try to run perl textARGV.pl foo bar on your command line instead.

Re: Processing ARGV
by friedo (Prior) on Dec 05, 2007 at 17:57 UTC
    The errors you're getting sound like you're actually running the program through a shell rather than perl. What happens when you type perl testARGV.pl?

      Interesting. When I run:

      perl testARGV.pl 1 2 3 4 5

      then I get

      Start of testARGV.pl

      and thats it! the error messages are gone, but where are the values I am trying to print? And I have the shebang in my code specifying

      #!/opt/third/party/bin/perl -w

      and I've verified that it does exists. (Its even in my PATH). When I do:

      /opt/third-party/bin/perl -v

      I see

      This is perl, v5.6.0 built for Linux-2.4c2.3-i686 Copyright 1987-2000, Larry Wall Perl may be copied only under the terms of either the Artistic License + or the GNU General Public License, which may be found in the Perl 5.0 source +kit. Complete documentation for Perl, including FAQ lists, should be found +on this system using `man perl' or `perldoc perl'. If you have access to + the Internet, point your browser at http://www.perl.com/, the Perl Home Pa +ge.

      Any more comments? Why don't I get my command line arguments.

        Odd. It works fine for me.
        $ perl testARGV.pl 1 2 3 Start of testARGV.pl 123
        You may want to try
        perl -e 'print $_, $/ for @ARGV' 1 2 3 4 5 #which prints #1 #2 #3 #4 #5
        in order to assure yourself that perl has been installed correctly.
        And of course - see what `which perl` returns in case that the script works fine.


        At the risk of pointing out the obvious, or a typo on your part, the path you give for printing the perl version is not the same path you have in your shebang line. Specifically 'third/party' ne 'third-party'.
Re: Processing ARGV
by almut (Canon) on Dec 05, 2007 at 18:10 UTC

    It seems your perl binary is in fact a zsh  :) — at least, if I change the shebang line in your script to #!/bin/zsh -w I do get similar garbage output on my box:

    $ ./655145.pl Start of testARGV.pl ./655145.pl:3: command not found: 0 ./655145.pl:5: unknown file attribute

      Wow, so your bringing my version of perl into question. I have found 3 versions of perl on our system, and perhaps ther are more, but I get three different responses to the -v option. When I run:

      /apollo/env/fulfillmentCenter/var/apps/perl -v

      I get

      This is perl, v5.8.8 built for Linux-2.4c2.3-i686-64int + Copyright 1987-2006, Larry Wall + Perl may be copied only under the terms of either the Artistic License + or the GNU General Public License, which may be found in the Perl 5 source ki +t. + Complete documentation for Perl, including FAQ lists, should be found +on this system using "man perl" or "perldoc perl". If you have access to + the Internet, point your browser at http://www.perl.org/, the Perl Home Pa +ge.

      and when I run

      /usr/bin/perl -v

      I get

      This is perl, v5.8.0 built for i386-linux-thread-multi (with 1 registered patch, see perl -V for more detail) Copyright 1987-2002, Larry Wall Perl may be copied only under the terms of either the Artistic License + or the GNU General Public License, which may be found in the Perl 5 source ki +t. Complete documentation for Perl, including FAQ lists, should be found +on this system using `man perl' or `perldoc perl'. If you have access to + the Internet, point your browser at http://www.perl.com/, the Perl Home Pa +ge.

      and when I run the version that is in my perl script:

      /opt/third-party/bin/perl -v

      I get

      This is perl, v5.6.0 built for Linux-2.4c2.3-i686 Copyright 1987-2000, Larry Wall Perl may be copied only under the terms of either the Artistic License + or the GNU General Public License, which may be found in the Perl 5.0 source +kit. Complete documentation for Perl, including FAQ lists, should be found +on this system using `man perl' or `perldoc perl'. If you have access to + the Internet, point your browser at http://www.perl.com/, the Perl Home Pa +ge.

      Which is not even the latest version. I'm going to my internal people on this one.

Re: Processing ARGV
by plobsing (Friar) on Dec 06, 2007 at 03:28 UTC
    This doesn't solve your immediate problem, but it may come and bite you eventually. Your program will act as if no arguments were passed if you only passed one argument due to the line:
    if ($#ARGV != 0)
    What you probably want is:
    if (@ARGV)
Re: Processing ARGV
by Erez (Curate) on Dec 06, 2007 at 08:50 UTC

    I'd appreciate any perls of wisdom you may have for me.

    If you really want to get the goods from Perl, I encourage you to learn the language's idioms.

    if ($#ARGV != 0) { foreach( @ARGV )
    could be summed as  while (@ARGV) and  print $_ can be written print;.
    Perl DWYM in running all those checks for you.

    Software speaks in tongues of man; I debug, therefore I code.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://655145]
Approved by friedo
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (6)
As of 2014-08-23 20:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (178 votes), past polls