void has asked for the wisdom of the Perl Monks concerning the following question:

O keepers of the great perl wisdom, I seek your knowledge.........Is it possible to direct a perl program to start with a different subroutines by using a switch after the program name (exp. test.pl -e or test.pl /e)?

Also what is the best way to "wipeout" the data thats in a variable? (as in a password or passphrase)I was thinking about just writing random data to the variable right after it's checked.

Replies are listed 'Best First'.
Re: Begining Monk seeks Wisdom.......
by azatoth (Curate) on May 04, 2001 at 16:44 UTC
Re: Begining Monk seeks Wisdom.......
by suaveant (Parson) on May 04, 2001 at 17:00 UTC
    undef $variable;
    that will wipe out a variable... though I don't know that it will actually clear the data out of RAM.
                    - Ant
Re: Begining Monk seeks Wisdom.......
by void (Scribe) on May 04, 2001 at 18:34 UTC
    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

      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

      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
      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)

      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
(tye)Re: Begining Monk seeks Wisdom.......
by tye (Sage) on May 04, 2001 at 22:19 UTC

    To clear out a variable so that its previous value doesn't linger in RAM: $var= "\0" x length($var); where you can use any character you like. There still might be copies elsewhere depending on what operations you did on the password (such as in the IO buffer if you read the password in).

            - tye (but my friends call me "Tye")
      If you are using Unicode you will want to follow that with:
      $var .= $var;
Re: Begining Monk seeks Wisdom.......
by larryk (Friar) on May 09, 2001 at 14:28 UTC
    if you are only using single character command line switches then I believe #!/usr/bin/perl -s will enable what the docs call "rudimentary switch parsing". basically (and this worked for me) perl puts a true value into a variable named the same as the switch so...
    #!/usr/bin/perl -s if ($a) { print "user selected a\n"; } elsif ($b) { print "user selected b\n"; } elsif ($c) { print "user selected c\n"; } else { print "no args!\n"; }
    Command line:
    [larryk@home] /home/larryk > somescript -a user selected a [larryk@home] /home/larryk > somescript -b user selected b [larryk@home] /home/larryk > somescript -c user selected c [larryk@home] /home/larryk > somescript no args!
    hope this helps.
Re: Begining Monk seeks Wisdom.......
by novitiate (Scribe) on May 04, 2001 at 20:14 UTC
    your tag line looks wrong - the einstein quote, i mean.

    humbly,

    novitiate.

    "Nothing rankles like the tag line not spel checked."
    -Zhuang Tzu