Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Help with processing command line arguments

by phatbasturd (Initiate)
on Dec 15, 2002 at 18:09 UTC ( #220031=perlquestion: print w/replies, xml ) Need Help??

phatbasturd has asked for the wisdom of the Perl Monks concerning the following question:

Could someone give a begginer a helping hand? On the following script, I keep getting the error Bad Command or File Name when ran from the command line. This script should either do addition or multiplication depending which subroutine is called on at the command line. For example: mathtest.pl add 3 6 1 9 should call the add subroutine add the numbers and then print the result. Can someone steer me into the right direction. It should not matter how many numbers are input. Here is the code:
while (@ARGV){ if ($ARGV[0] =~ /add/) { $rtn = add($ARGV[1], /.../); print "The sum is: $rtn"; } elsif ($ARGV[0] =~ /multiply/) { $rtn = multiply($ARGV[1], /.../); print "The product is: $rtn"; } sub add { $sum = $sum + $ARGV; return $sum; } sub multiply { $product = $product * $ARGV; return $product; } }

edited: Mon Dec 16 17:02:01 2002 by jeffa - changed title (was: Help for a beginner)

Replies are listed 'Best First'.
(jeffa) Re: Help with processing command line arguments
by jeffa (Bishop) on Dec 15, 2002 at 18:30 UTC
    At the risk of this being homework, here is some help. If you want to use the contents of @ARGV, don't use a while loop. Just shift off the first arg, which should be the operation to perform. Then you can decide which function to call (and pass that function the remaining arguments inside @ARGV).
    use strict; # always! my $op = shift; if ($op =~ /add/) { print "The sum is: ", add(@ARGV), "\n"; } elsif ($op =~ /multiply/) { print "The product is: ", multiply(@ARGV), "\n"; }
    Now for the subroutines - $ARGV is not a built-in scalar, you just made it up. :P Try this instead for add:
    sub add { my $sum = 0; $sum += $_ for @_; return $sum; } # and if that's too consise - try this one: sub add { my $sum = 0; foreach my $number (@_) { $sum = $sum + $number; } return $sum; }
    I'll leave multiply as an exercise. ;)

    Oh yeah ... you should take a look at Learning Perl.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: Help with processing command line arguments
by diotalevi (Canon) on Dec 15, 2002 at 18:52 UTC

    It sounds like you are using something DOS, Windows 95, Windows 98 or Windows ME. You can't run scripts just by typing their name - you have to pass the script file name into perl first. This looks something like C:\SRC> perl myscript.pl add 1 2. I think you're typing something like C:\SRC> myscript.pl add 1 2 instead. Or at least that's what I get from your error message.

      Depends. If you've got a file association set up with perl scripts (or more correctly put, an extension, such as '.pl'), then a call to perl is not necessary. But in his case, I believe you are correct in a way: I don't think he has a file association set up for the extension he uses for his scripts.

      -------------------------------------
      eval reverse@{[lreP
      =>q{ tsuJ\{qq},' rehtonA'
      ,q{\}rekcaH },' tnirp']}[1+1+
      1,1-1,1+1,1*1,(1+1)*(1+1)];
      -------------------------------------
      

        Oh but it is, it is! The problem is that the DOS/Windows versions I've listed don't have a command shell powerful enough associate file extensions with an interpreter. Remember - you're restricted to command.com here.


        Fun Fun Fun in the Fluffy Chair

        A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Help with processing command line arguments
by jpfarmer (Pilgrim) on Dec 15, 2002 at 18:53 UTC
    I assume you're running a flavor of UNIX/Linux? If so, make sure that you have execute permissions on the file and it's 1. either in your path or 2. you call it as ./filename when you're in the same directory as the script. If you put it in your path and it still doesn't work, try running 'rehash'.
Re: Help with processing command line arguments
by ibanix (Hermit) on Dec 16, 2002 at 08:01 UTC
    As the other Monks above noticed, you may not have the file associations set correctly.

    If you're in a sane Win32 (ie, WinNT/2K/XP), you can do this:
    ftype perl_script="C:\perl\bin\perl.exe" "%1" %* assoc .pl=perl_script
    If you're on earlier Win32, god help you. Use a batch file as a wrapper.

    And if you're on a Unix-like OS, just make sure you chmod a+x the file.

    HTH,
    ibanix

    $ echo '$0 & $0 &' > foo; chmod a+x foo; foo;

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2022-10-05 21:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My preferred way to holiday/vacation is:











    Results (25 votes). Check out past polls.

    Notices?