Getopt::Std problem

Discussion in 'Perl Misc' started by S, Feb 2, 2004.

  1. S

    S Guest

    Hi,

    I'm using Getopt::Std in a script that I'm writing. While testing
    boundary conditions, I notice some strange behavior. Here's the code
    snippet in question:

    use vars qw /%option/;
    getopts('avs:m', \%option) or die Usage();
    die "You did not enter any options - run $0 -h to view options\n" if !
    %option;

    Now, if I run my script with the -s option, but no argument, it dies as
    expected, except for the following:


    1 at ./make-acct-ctl.pl line 14.

    Can anyone help to explain/correct this?
    S, Feb 2, 2004
    #1
    1. Advertising

  2. In article <bvm93c$9mi$>,
    S <> wrote:
    :I'm using Getopt::Std in a script that I'm writing. While testing
    :boundary conditions, I notice some strange behavior. Here's the code
    :snippet in question:

    :use vars qw /%option/;
    :getopts('avs:m', \%option) or die Usage();
    :die "You did not enter any options - run $0 -h to view options\n" if !
    :%option;

    :Now, if I run my script with the -s option, but no argument, it dies as
    :expected, except for the following:

    :1 at ./make-acct-ctl.pl line 14.

    :Can anyone help to explain/correct this?

    What exactly does Usage() compute? Is it possible that Usage() is
    returning the 1 ? It should return a list, but perhaps along the
    way you have an accidental scalar context that is converting a
    list containing a single string into scalar(@thatlist) which
    would be 1 ?

    --
    "There are three kinds of lies: lies, damn lies, and statistics."
    -- not Twain, perhaps Disraeli, first quoted by Leonard Courtney
    Walter Roberson, Feb 2, 2004
    #2
    1. Advertising

  3. S

    S Guest

    Walter Roberson wrote:
    > In article <bvm93c$9mi$>,
    > S <> wrote:
    > :I'm using Getopt::Std in a script that I'm writing. While testing
    > :boundary conditions, I notice some strange behavior. Here's the code
    > :snippet in question:
    >
    > :use vars qw /%option/;
    > :getopts('avs:m', \%option) or die Usage();
    > :die "You did not enter any options - run $0 -h to view options\n" if !
    > :%option;
    >
    > :Now, if I run my script with the -s option, but no argument, it dies as
    > :expected, except for the following:
    >
    > :1 at ./make-acct-ctl.pl line 14.
    >
    > :Can anyone help to explain/correct this?
    >
    > What exactly does Usage() compute? Is it possible that Usage() is
    > returning the 1 ? It should return a list, but perhaps along the
    > way you have an accidental scalar context that is converting a
    > list containing a single string into scalar(@thatlist) which
    > would be 1 ?
    >

    sub Usage
    {
    print <<HELP;

    $0 -s <server> | -v | -a | -m

    HELP
    }
    S, Feb 2, 2004
    #3
  4. S

    Ben Morrow Guest

    S <> wrote:
    > Walter Roberson wrote:
    > > What exactly does Usage() compute? Is it possible that Usage() is
    > > returning the 1 ? It should return a list, but perhaps along the
    > > way you have an accidental scalar context that is converting a
    > > list containing a single string into scalar(@thatlist) which
    > > would be 1 ?
    > >

    > sub Usage
    > {
    > print <<HELP;
    >
    > $0 -s <server> | -v | -a | -m
    >
    > HELP
    > }


    Well, there you go then: print() returns 1 if the print succeeds. You
    probably meant

    return <<HELP;
    ....

    Ben


    --
    It will be seen that the Erwhonians are a meek and long-suffering people,
    easily led by the nose, and quick to offer up common sense at the shrine of
    logic, when a philosopher convinces them that their institutions are not based
    on the strictest morality. [Samuel Butler, paraphrased]
    Ben Morrow, Feb 2, 2004
    #4
  5. S

    S Guest

    Ben Morrow wrote:
    > S <> wrote:
    >
    >>Walter Roberson wrote:
    >>
    >>>What exactly does Usage() compute? Is it possible that Usage() is
    >>>returning the 1 ? It should return a list, but perhaps along the
    >>>way you have an accidental scalar context that is converting a
    >>>list containing a single string into scalar(@thatlist) which
    >>>would be 1 ?
    >>>

    >>
    >>sub Usage
    >>{
    >>print <<HELP;
    >>
    >>$0 -s <server> | -v | -a | -m
    >>
    >>HELP
    >>}

    >
    >
    > Well, there you go then: print() returns 1 if the print succeeds. You
    > probably meant
    >
    > return <<HELP;
    > ...
    >
    > Ben
    >
    >


    you are correct!!! thanks!
    S, Feb 2, 2004
    #5
  6. S

    Uri Guttman Guest

    >>>>> "S" == S <> writes:

    S> sub Usage
    S> {
    S> print <<HELP;

    S> $0 -s <server> | -v | -a | -m

    S> HELP
    S> }

    see my comments in another current thread on how to do a better usage
    sub. the usage sub should do the die/exit itself. and it should take an
    error string argument. this makes usage calls much cleaner in the main code.

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
    Uri Guttman, Feb 2, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Jansson
    Replies:
    5
    Views:
    6,272
    Ivan Vecerina
    Mar 17, 2005
  2. Vinu
    Replies:
    4
    Views:
    345
    Jim Langston
    Jul 7, 2005
  3. Vinu
    Replies:
    0
    Views:
    344
  4. hymie!
    Replies:
    1
    Views:
    128
    hymie!
    Dec 27, 2005
  5. Davy
    Replies:
    2
    Views:
    99
    Paul Lalli
    Aug 20, 2006
Loading...

Share This Page