Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re: problem with Getopt::Long

by etcshadow (Priest)
on Aug 20, 2004 at 06:26 UTC ( #384543=note: print w/replies, xml ) Need Help??

in reply to problem with Getopt::Long

Well, one problem is that you are first calling GetOptions, and then asking that @ARGV not be empty... however, the way that GetOptions works is to remove values from @ARGV. So if all that is contained in @ARGV are -options, then after GetOptions, they'll have all been removed from @ARGV, and @ARGV will be empty.

Another problem (that isn't with your Getopt::Long), is with your shell scripting. If you want to pass all arguments through to a command in a shell script, it's done like:

command "$@"
rather than
command $*
The difference can be demonstrated if you just take this simple shell script (lets call it ""):
#!/bin/sh perl -e "use Data::Dumper; print Dumper \@ARGV" $*
and call it with some arguments that will demonstrate why this breaks:
[sstone@ernie1 scratch]$ sh 1 2 "3 and more stuff" $VAR1 = [ 1, 2, 3, 'and', 'more', 'stuff' ]; [sstone@ernie1 scratch]$
but if we change to:
#!/bin/sh perl -e "use Data::Dumper; print Dumper \@ARGV" "$@"
and run it the same way:
[sstone@ernie1 scratch]$ sh 1 2 "3 and more stuff" $VAR1 = [ 1, 2, '3 and more stuff' ]; [sstone@ernie1 scratch]$
The difference being that "$@" will preserve the arg list as a list, whereas $* will flatten it all into a string (basically just like join(" ",@ARGV)), and then split it on whitespace... so if you had args with whitespace in them it would get all messed up.

At the very least, that's a couple of things that might be causing you problems.

------------ :Wq Not an editor command: Wq

Replies are listed 'Best First'.
Re^2: problem with Getopt::Long
by syedtoah (Initiate) on Aug 20, 2004 at 06:59 UTC
    i did what u have mentioned in ur mail, i changed $* to $@ and the result is the same. and one more thing i forgot to meition, when i call the shell script giving command line arguments and pass them to the perl script i see all the command line arguments and options present in the perl command in the shell script, but the thing happening here is that the very first if loop as mentioned in the code ie if (exists $options{x}) { do some thing } is giving positive irrespective of the option passed.... what wud be the problem here
      hey i have got the solution. As i had guessed there was a problem with the "exists". In all the if conditions the exists key word has to be removed for it to work as expectedloops...........any way thanks for contribution.....

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://384543]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2019-09-19 20:50 GMT
Find Nodes?
    Voting Booth?
    The room is dark, and your next move is ...

    Results (251 votes). Check out past polls.