in reply to Begining Monk seeks Wisdom.......

OK, I've checked out the "Getopt::Std" module and thought that I could make it work with.....
use Getopt::Std; getopt('ed'); if ($opt_e = 1){ print "option e\n"; } else{ print "option d\n"; }
although this doesn't seem to work, am I missing something? "The Universe is not only more complex than we imagine, it is more complex than the can imagine." - Albert Einstein

Edit: chipmunk 2001-05-04

Replies are listed 'Best First'.
Re: Re: Begining Monk seeks Wisdom.......
by azatoth (Curate) on May 04, 2001 at 18:37 UTC
    First, use code tags to format your code when you post it. See the FAQ for details.

    use Getopt::Std; getopts('ed:', \%opts); if ($opts{e}) # updated as per [Chemboy] { print "option e used!\n"; } else { print "option e not used!\n"; }
    The code above should do the trick...You weren't passing your switches into \%opts!

    UPDATE:
    Hmm i am stumped. Use Getopt::Long, it's cooler anyhoo :P
    use Getopt::Long; my %opt = (); GetOptions(\%opt, "file=s", ); die "No file specified : $!" unless $opt{file}; if ($opt{file}) { print "File is $opt{file}!\n"; }


    Azatoth a.k.a Captain Whiplash

    Make Your Die Messages Full of Wisdom!
    Get YOUR PerlMonks Stagename here!
    Want to speak like a Londoner?
      Well, it must be me. I copied your code and all i get is "option e used!" now matter what option I give it.

      "The Universe is not only more complex than we imagine, it is more complex than the can imagine." - Albert Einstein
Re: Re: Begining Monk seeks Wisdom.......
by grinder (Bishop) on May 04, 2001 at 19:53 UTC

    If you are use'ing strict; (and you should be), then you have to predefine your variables as package variables.

    usr vars qw/$opt_x $opt_y $opt_z/;

    A couple of meta-comments on your post: choose a more precise subject title, otherwise people will ignore the post (too much noise, not enough signal). Secondly, sling a couple of <br>'s before your sig.


    --
    g r i n d e r
Re: Re: Begining Monk seeks Wisdom.......
by jeroenes (Priest) on May 04, 2001 at 19:14 UTC
    The problem is with your if statement. Just use
    if ( $opt_e ) { print "option e\n"; ...
    Your code assigns one to $opt_e. If you want to compare, use '==' for numerical or 'eq' for string comparison. See perlop.

    Jeroen
    "We are not alone"(FZ)

Re: Re: Begining Monk seeks Wisdom.......
by ChemBoy (Priest) on May 04, 2001 at 19:25 UTC

    Yes, you're missing a bracket at the end of the line. :-) Syntax errors aside (use the -w switch to avoid the problem jeroenes pointed out), I don't think anything's wrong with what you wrote, in principle. I can't get Getopt::Std::getopt() to work either (I'm not sure what's up with that--possibly we're both idiots), but if you use getopts() (note the s) instead, it should work (assuming, of course, that your syntax checks out otherwise ;-).

    If you like use strict; (and you should), try the two-argument syntax that azatoth mentions (check the docs for how), so you don't have to declare a variable for every switch.

    Update:
    I got so excited about getopt that I forgot the main point: check the return value of the whichever function you use to see if it's failing (returns a true value if it works normally).
    Correction: <blush>
    only check the return value of getopts(), getopt() always returns false.

    Update 2:
    The source of your original problem was probably that getopt() expects all flags to have arguments (e.g. -e1 or -e 1), not be booleans (-e): getopts() allows for both. Which I should have pointed out earlier, sorry.



    If God had meant us to fly, he would *never* have give us the railroads.
        --Michael Flanders

      Thanks for all the help

      "The Universe is not only more complex than we imagine, it is more complex than the can imagine." - Albert Einstein