Testing perlfuncs (getopt) for true/false. (yeah i'm newbie.)

Ask a Question related to PERL Miscellaneous, Design and Development.

  1. #1

    Default Re: Testing perlfuncs (getopt) for true/false. (yeah i'm newbie.)

    ThePotPlants wrote:
    >
    > I keep seeing in the documentation about functions returning 1 or 0 true or
    > false... But I can't see how to test for it.
    > I want to know how to "test" something to see if it is true or false. Both
    > in general, and specifically for my problem.
    if ( $scalar ) {
    print "$scalar is true\n"
    }
    else {
    print "$scalar is false\n"
    }


    unless ( $scalar ) {
    print "$scalar is false\n"
    }
    else {
    print "$scalar is true\n"
    }

    > My particular problem is using Getopt::Std;
    > My code works.. (sort of), but I know i'm not using it properly.
    >
    > Getopt takes command line options and the following values (which is pretty
    > cool.)
    >
    > Essentially I'm testing to see if the associated value is null to decide
    > whether to call the sub routines, rather than testing if the option has been
    > invoked.
    >
    > See my programme below.
    >
    > If no options are supplied the else condition should complain "invalid
    > option".
    >
    > In my programme -e is the first valid option and -h is for help.
    > If I type: "opt2.pl -e arg1" then the argument arg1 is passed so that
    > variable $opt_e = arg1. And it calls the sub. (hoorah!)
    > If I type "opt2.pl -e " i.e with no argument then it fails, and complains
    > "invalid option". (also hoorah.. of sorts..)
    >
    > It's all good so far... now the annoying bit...
    >
    > If want help I should just type: "opt2.pl -h"
    >
    > But because I have not typed something after the -h then the value $opt_h is
    > empty. Even though the option is activated.
    >
    > Specifically: How do I test $opt_h to see if is true or false? or rather to
    > see if -h has been invoked?
    > Generally: How can you test a variable to see if it's "not null" / if
    > there's something there/ id it's true/false/1/0 etc etc...
    perldoc -f defined

    > Any help (which doesn't include RTFM) would be greatly appreciated
    Oops, too late. :-)
    > #!/usr/bin/perl
    use warnings;
    use strict;

    > use Getopt::Std;
    >
    > getopt('eh');
    You might want to use getopts() instead of getopt(), it has better
    features. Also the two argument form of getopt() or getopts() will work
    better when warnings and strict are enabled.

    my %opt;
    getopts( 'e:h', \%opt );
    > print "
    > P1 Option -e $opt_e
    > P2 Option -h $opt_h
    > \n" ;
    >
    > if ($opt_h ne "" ) {&hlp();
    > }
    > elsif ($opt_e ne "" ) {&proc1($opt_e) ;
    > }
    > else {print "invalid option \n" ;
    > }
    if ( exists $opt{ h } ) { hlp() }
    elsif ( exists $opt{ e } ) { proc1( $opt{ e } ) }

    > sub hlp{ print "
    > Usage opt2.pl -[eh]
    > -e environment
    > -h help \n" ;
    > return 1;
    > }
    >
    > sub proc1 { print "yeah it works \n" ; return 1 ;}

    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn Guest

  2. Similar Questions and Discussions

    1. True or False and why?
      (Contribute CS3 for Windows) A brand new Contributor user gets an account on a web server. There are no files in the directory (no index.htm,...
    2. cfselect and true/false instead of 1/0
      Hi, can any one tell me why does it return the text true and false instead of 1 and 0 for a BIT column of a table from MS SQL Server. I have CFMX...
    3. format true/false
      Hi Webform; bound datagrid. My boolean column shows TRUE or FALSE. I'd rather have a checkbox. How to format please? Thanks Graeme
    4. not (true) not (false)
      I am having a major brain block. I am reviewing this code and am getting my logic confused. This is the code: <cfif (not...
  3. #2

    Default Re: Testing perlfuncs (getopt) for true/false. (yeah i'm newbie.)

    ThePotPlants <thepotplants@yahoo.com> wrote:
    > Any help (which doesn't include RTFM) would be greatly appreciated

    So long then.


    --
    Tad McClellan SGML consulting
    [email]tadmc@augustmail.com[/email] Perl programming
    Fort Worth, Texas
    Tad McClellan Guest

  4. #3

    Default Re: Testing perlfuncs (getopt) for true/false. (yeah i'm newbie.)


    no attachments. just paste in the code or a url to it.

    uri

    --
    Uri Guttman ------ [email]uri@stemsystems.com[/email] -------- [url]http://www.stemsystems.com[/url]
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- [url]http://jobs.perl.org[/url]
    Uri Guttman Guest

  5. #4

    Default Re: Testing perlfuncs (getopt) for true/false. (yeah i'm newbie.)

    "Uri Guttman" <uri@stemsystems.com> wrote in message
    news:x74r279lys.fsf@mail.sysarch.com...
    > no attachments. just paste in the code or a url to it.
    <!DOCTYPE NETSCAPE-Bookmark-file-1>
    <!-- This is an automatically generated file.
    It will be read and overwritten.
    Do Not Edit! -->
    <TITLE>Bookmarks</TITLE>
    <H1>Bookmarks</H1>
    <DL><p>
    <DT><A HREF="http://lcg-www.uia.ac.be/~erikt/perl/ln02.html"
    ADD_DATE="1056854755" LAST_VISIT="1057014456" LAST_MODIFIED="1056854758">A
    Shortcut to Perl (2)</A>
    <DT><A HREF="file:///C:/Perl/html/faq/Windows/ActivePerl-Winfaq.html"
    ADD_DATE="1055910976" LAST_VISIT="1057014458"
    LAST_MODIFIED="1055910976">ActivePerl faq - ActivePerl Frequently Asked
    Questions</A>
    <DT><A HREF="http://activestate.com/" ADD_DATE="1053406411"
    LAST_VISIT="1057014458" LAST_MODIFIED="1052718570">ActiveState Perl</A>
    <DT><A HREF="http://tech.irt.org/articles/js127/" ADD_DATE="1054780677"
    LAST_VISIT="1057014458" LAST_MODIFIED="1054780678">Deleting Files in
    Perl</A>
    <DT><A HREF="http://www.devdaily.com/perl/edu/qanda/plqa00001.shtml"
    ADD_DATE="1056426316" LAST_VISIT="1057014456"
    LAST_MODIFIED="1056426316">DevDaily - Learn Perl Perl Q&A Reading
    command-line arguments</A>
    <DT><A HREF="http://www.effectiveperl.com/recipes/sorting.html"
    ADD_DATE="1056854978" LAST_VISIT="1057014456"
    LAST_MODIFIED="1056854978">Effective Perl - Recipes for sorting</A>
    <DT><A
    HREF="http://www.perldoc.com/perl5.8.0/pod/perlfaq8.html#How-do-I-add-the-di
    rectory-my-program-lives-in-to-the-module-library-search-path-"
    ADD_DATE="1054606085" LAST_VISIT="1057014456"
    LAST_MODIFIED="1054606085">http--www.perldoc.com-perl5.8.0-pod-perlfaq8.html
    </A>
    <DT><A HREF="http://www.theperlreview.com/Articles/v0i4/golf.pdf"
    ADD_DATE="1055824963" LAST_VISIT="1057014458"
    LAST_MODIFIED="1055824964">http--www.theperlreview.com-Articles-v0i4-golf.pd
    f</A>
    <DT><A HREF="http://www.devdaily.com/perl/edu/qanda/"
    ADD_DATE="1056426574" LAST_VISIT="1057014458"
    LAST_MODIFIED="1056426574">Learn Perl Perl Question & Answer Center</A>
    <DT><A HREF="http://perlmonks.com/index.pl?node_id=102347"
    ADD_DATE="1053404081" LAST_VISIT="1057014455"
    LAST_MODIFIED="1053404082">Perl Monks</A>
    <DT><A HREF="http://www.perl.com/pub/a/2003/05/29/treasures.html"
    ADD_DATE="1055239920" LAST_VISIT="1057014458"
    LAST_MODIFIED="1055239920">perl.com Hidden Treasures of the Perl Core [May.
    29, 2003]</A>
    <DT><A HREF="http://www.perl.com/" ADD_DATE="1054597618"
    LAST_VISIT="1057014458" LAST_MODIFIED="1054597619">Perl.com The Source for
    Perl -- perl development, perl conferences</A>
    <DT><A HREF="http://perl.oreilly.com/" ADD_DATE="1053405893"
    LAST_VISIT="1057014458" LAST_MODIFIED="1053405893">perl.oreilly.com --
    Welcome to the O'Reilly Perl Center -- computer books, perl books, perl
    programming</A>
    <DT><A HREF="http://www.perldoc.com/perl5.8.0/lib/lib.html"
    ADD_DATE="1054603910" LAST_VISIT="1057014458"
    LAST_MODIFIED="1054603911">perldoc.com</A>
    <DT><A HREF="http://www.xav.com/perl/lib/Pod/perlfunc.html"
    ADD_DATE="1056851805" LAST_VISIT="1057014456"
    LAST_MODIFIED="1056851806">perlfunc - Perl builtin functions</A>
    <DT><A HREF="http://www.ourwasteland.com/phpwiki/index.php/PerlExamples"
    ADD_DATE="1056424110" LAST_VISIT="1057014458"
    LAST_MODIFIED="1056424111">PhpWiki - Perl Examples</A>
    <DT><A HREF="http://search.cpan.org/" ADD_DATE="1053393228"
    LAST_VISIT="1057014455" LAST_MODIFIED="1053393229">search.cpan.org The CPAN
    Search Site</A>
    </DL><p>



    ThePotPlants Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139