Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Command line args

by iamcal (Friar)
on Jun 06, 2001 at 18:49 UTC ( [id://86249]=perlquestion: print w/replies, xml ) Need Help??

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

I'm writing a script which needs to take command like arguments and have data piped to it. eg:

echo "data1" | foo.pl "data2"

The problem i'm having is that it's not getting the arguments into $ARGV. When i shift the code that checks $ARGV and <> to above the use DBI; statement in my script, the $ARGV works but the <> doesn't. In the example code above, i get a "Can't open data2: No such file or directory" message from bash. What am i doing wrong?

How do you pass strings as command line parameters?

Replies are listed 'Best First'.
Re: Command line args
by japhy (Canon) on Jun 06, 2001 at 18:53 UTC
    When arguments are sent to Perl, they're in the @ARGV array. When you read from <>, Perl treats the elements in @ARGV as though they were actually filenames.

    Perhaps you want to force a read from STDIN, or else remove the elements from @ARGV (locally, perhaps).
    while (<STDIN>) { ... } # or { local @ARGV; while (<>) { ... } }


    japhy -- Perl and Regex Hacker
      Excellent - that was the problem. Using <> when i should have been using <STDIN>.
Re: Command line args
by azatoth (Curate) on Jun 06, 2001 at 19:09 UTC
    Alternatively, check out Getopt::Long. You'll like it.

    #it's easy, mm-kay? a snippet from one of my scripts. use Getopt::Long; my %opt = (); GetOptions(\%opt, "rhost=s", # remote host as a (s)tring "ruser=s", # remote username as a (s)tring "rpwd=s", # remote password as a (s)tring "localDir=s", # local directory as a (s)tring ) or die "Invalid Command Line Options!\n"; #then use like this do_this() if $opt{rhost}; do_that() if $opt{ruser}; chdir $opt{localDir}; #etc etc
    Update : Bah, runrig beat me to it.

    Azatoth a.k.a Captain Whiplash

    Make Your Die Messages Full of Wisdom!
    Get YOUR PerlMonks Stagename here!
    Want to speak like a Londoner?
Re: Command line args
by John M. Dlugosz (Monsignor) on Jun 06, 2001 at 19:27 UTC
    The magical <> will read from standard input once the argument list is used up. So remove the non-filename arguments from the list (and there will be nothing left) and <> will work as expected: read from files specified on the command line or from standard input if none are given.
      perfect. i was confusing <> and <STDIN> and forgot the magic. thanks!
        It's better to use the magic. That way you can write either foo fname.txt or bar | foo without any care in the implementation of foo.

        —John

Re: Command line args
by VSarkiss (Monsignor) on Jun 06, 2001 at 19:04 UTC
    You've got a bit of a mish-mosh here.

    Command line arguments are passed in the array @ARGV, not the scalar $ARGV. It doesn't matter whether you use it before or after "use DBI", so if the behavior of your program changes when you do that, you must be making some other change as well, but I can't tell what without looking at the (Perl) code.

    Finally, that error message is from your program, not bash; if bash tried to open a file and couldn't, it would only say: data2: No such file or directory Somewhere in your code, you must be saying something along the lines of open X, $filename or die "Can't open $filename: $!"; So the next question: does the file data2 exist??

    So.... To answer your question "How do you pass strings as command line parameters?": they're passed in @ARGV; see, for example, man perlrun.

    To answer your question, "What am i doing wrong?": It's not clear, maybe several things. Code snippet would help.

    HTH

      Stated that a big wrong - i meant to say $ARGV[0] wasn't containing anything. But this was because is was confusing <> and <STDIN>.
Re: Command line args
by runrig (Abbot) on Jun 06, 2001 at 19:09 UTC
    Why are you not using Getopt::Std or Getopt::Long? They make life (well, command line args anyway) much easier :-)

    Update:I (now) realize he's only passing one argument and wants to read STDIN, but I would still prefer to use one of the Getopt modules, which would still allow use of '<>', and allow mixing file names with STDIN (when you use '-' as a filename arg).

    Update2:Okay, sounds like there's probably good reason to use 'STDIN' in this case, nice that perl makes it easy :)

      Thanks, but i'm just passing a single string on the command line. I was being a muppet and confusing <> with <STDIN>

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2024-06-17 17:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.