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

Re: Passing switches through pl2bat scripts

by flounder99 (Friar)
on Nov 17, 2003 at 15:47 UTC ( [id://307701]=note: print w/replies, xml ) Need Help??


in reply to Passing switches through pl2bat scripts

Read the man page to Getopt::Std a little closer.
NAME getopt - Process single-character switches with switch clustering getopts - Process single-character switches with switch clustering SYNOPSIS use Getopt::Std; getopt('oDI'); # -o, -D & -I take arg. Sets $opt_* as a side e +ffect. getopt('oDI', \%opts); # -o, -D & -I take arg. Values in %opts getopts('oif:'); # -o & -i are boolean flags, -f takes an argumen +t # Sets $opt_* as a side effect. getopts('oif:', \%opts); # options as above. Values in %opts
You want getopts not getopt. Because getopt is expecting a value and getopts is for binary flags. when you do a
C:\>z -z
you are setting the value of %opt{z} == "" which is false.
<code> C:\>z -z -z
sets the value of %opt{z} == "-z" which is evaluated as true.

Don't feel bad, I've been there and done that.

--

flounder

Replies are listed 'Best First'.
Re: Re: Passing switches through pl2bat scripts
by demerphq (Chancellor) on Nov 17, 2003 at 16:19 UTC

    you are setting the value of %opt{z} == "" which is false.

    Actually I disagree. The documentation says quite clearly that this behaviour is incorrect:

    The getopt() functions processes single-character switches with switch clustering. Pass one argument which is a string containing all switches that take an argument. For each switch found, sets $opt_x (where x is the switch name) to the value of the argument, or 1 if no argument. Switches which take an argument don't care whether there is a space between the switch and the argument.

    Although I do agree with you that probably he should be using getopts and not getopt, but even better would be to just ditch Getopt::Std and use Getopt::Long instead.


    ---
    demerphq

      First they ignore you, then they laugh at you, then they fight you, then you win.
      -- Gandhi


      But getopt always assumes all the named switches take arguments.
      getopt('oDI'); # -o, -D & -I take arg. Sets $opt_* as a side e +ffect. getopt('oDI', \%opts); # -o, -D & -I take arg. Values in %opts
      Try running this:
      #!perl use Getopt::Std; my %opts = (); getopt('z', \%opts); for (keys %opts) { print '$opts{', $_, '} = "', $opts{$_}, "\"\n"; }
      you get some interesting results
      C:\myperl\temp>perl temp.pl -z $opts{z} = "" C:\myperl\temp>perl temp.pl -y $opts{y} = "1" C:\myperl\temp>perl temp.pl -y -z $opts{y} = "1" $opts{z} = "" C:\myperl\temp>perl temp.pl -z -y $opts{z} = "-y" C:\myperl\temp>temp -z -z $opts{z} = "-z"

      --

      flounder

        Something is wrong. Either the documentation, or the code. The documentation clearly states that the value of a valueless option is to be set to be 1. It does not actually happen. Thus there is an error involved here. Generally the rule is that the code should fit the docs, so I assume the code is buggy. Anyway, ive sent a patch to p5p.


        ---
        demerphq

          First they ignore you, then they laugh at you, then they fight you, then you win.
          -- Gandhi


Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2024-04-18 00:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found