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 | [reply] [d/l] |
|
Excellent - that was the problem. Using <> when i should have been using <STDIN>.
| [reply] |
Re: Command line args
by azatoth (Curate) on Jun 06, 2001 at 19:09 UTC
|
#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? | [reply] [d/l] |
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. | [reply] [d/l] [select] |
|
perfect. i was confusing <> and <STDIN> and forgot the magic. thanks!
| [reply] [d/l] [select] |
|
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
| [reply] [d/l] [select] |
|
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 | [reply] [d/l] [select] |
|
Stated that a big wrong - i meant to say $ARGV[0] wasn't containing anything. But this was because is was confusing <> and <STDIN>.
| [reply] [d/l] |
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 :) | [reply] |
|
Thanks, but i'm just passing a single string on the command line. I was being a muppet and confusing <> with <STDIN>
| [reply] |