Ask a Question related to PERL Miscellaneous, Design and Development.
-
John W. Krahn #1
Re: Testing perlfuncs (getopt) for true/false. (yeah i'm newbie.)
ThePotPlants wrote:
if ( $scalar ) {>
> 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.
print "$scalar is true\n"
}
else {
print "$scalar is false\n"
}
unless ( $scalar ) {
print "$scalar is false\n"
}
else {
print "$scalar is true\n"
}
perldoc -f defined> 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...
Oops, too late. :-)> Any help (which doesn't include RTFM) would be greatly appreciated
use warnings;> #!/usr/bin/perl
use strict;
You might want to use getopts() instead of getopt(), it has better> use Getopt::Std;
>
> getopt('eh');
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 );
if ( exists $opt{ h } ) { hlp() }> 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" ;
> }
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
-
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,... -
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... -
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 -
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... -
Theory Question: True & False
you sure ! -
Tad McClellan #2
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
-
Uri Guttman #3
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
-
ThePotPlants #4
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...<!DOCTYPE NETSCAPE-Bookmark-file-1>> no attachments. just paste in the code or a url to it.
<!-- 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



Reply With Quote

