Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Getting args

by willa (Acolyte)
on Dec 10, 2001 at 22:32 UTC ( [id://130738]=perlquestion: print w/replies, xml ) Need Help??

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

How do I get a command line argument? It is compulsory, so I don't think I need getopt - just the kind of thing you'd do in UNIX with $1...

Replies are listed 'Best First'.
Re: Getting args
by joealba (Hermit) on Dec 10, 2001 at 22:40 UTC
    Take a look at @ARGV. It holds all your command line arguments, split by whitespace.

    Update: DOH! daveorg beat me to it, so I might as well add some other useful tidbits:

    • Number of arguments passed: scalar @ARGV
    • Program name: $0
    • If all your arguments are filenames and you want to read them all in line by line, you can use <>. That's kinda neat sometimes, if you can trust your input!

    Camel 3, p. 659
      How do I pattern-match a variable with the first one?
      if ($var =~ m/@ARGV[1]/) { ... }
      ???
        You were close.
        if ($var =~ /$ARGV[0]/) { ... }

        A more exact match:
        if ($var =~ /^$ARGV[0]$/) { ... }

        Updated: $ARGV[1] is the SECOND argument. $ARGV[0] is the first one.
Re: Getting args
by davorg (Chancellor) on Dec 10, 2001 at 22:37 UTC

    Your command line arguments are in @ARGV.

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      I hope this helps
      #!/usr/local/bin/perl -w use strict; my $d; shift = $d;

      I hate posting after such experience monks,but
      I think this is what you are after
      learning too monk2b
      update I was very backwards in the original post
      I hope I did not do anyone permanent harm my apologies.
      Thanks davorg

        I think you may be a little confused.

        $ARGV[0]=$d;

        This overwrites the first command line argument with the value of $d - which is undefined.

        shift=$d;

        This doesn't even compile.

        --
        <http://www.dave.org.uk>

        "The first rule of Perl club is you do not talk about Perl club."
        -- Chip Salzenberg

Re: Getting args
by hotshot (Prior) on Dec 10, 2001 at 22:46 UTC
    maybe you should consider using Getopt anyway, coz' it makes it much easier to parse arguments, especialy if you use flags (like: -e, -f, --mode etc.), that expect special values after them. it probably depends on your program. If you need any help I just wrote a command line interface for my product at work so maybe I'll have a few hints for you.

    Hotshot
Re: Getting args
by Dylan (Monk) on Dec 11, 2001 at 00:09 UTC

    Well, TIMTOWTDI...

    All command line args go into the global array @ARGV.
    shift defaults to @ARGV (when it is not in a suboutine)...
    More info on that is in the Perl predefined variables node.
    for one arg, you can shift it, like so: my $var = shift
    or, for many args:
    my ($var1,$var2,var3,$var4)=@ARGV;

    Which is kinda neat.
    Also, you could access them directly: print $ARGV[0]
    Well, there might be even more ways, but you get the idea, right? :)

    Here's some code to play with.

    #!/usr/bin/perl if ($#ARGV == 2) { # $#ARGV is number of args in @ARGV minus one my $foo = shift; #First arg; my $bar = shift; #second arg; my $baz = shift; #Last arg; #print it; print "You say $foo $bar $baz?\n"; } else { #Warn, and exit; warn <<TEXT; I need three arguments! Usage: $0 arg1 arg2 arg3 TEXT }

    I hope that helps. :-)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2024-12-08 00:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which IDE have you been most impressed by?













    Results (50 votes). Check out past polls.