Ask a Question related to PERL Beginners, Design and Development.
-
Trina Espinoza #1
GetOpts: boolean and argument value combos
Hey Perl Peps,
I am stuck in the mudd and hoping someone can give me a few clues that will help get me back on track.
I want to submit some arguments like so:
../script1.pl -book -title HP3 -chapter 04
NOTE: -book does not have an argument
Now I have been using GetOpt::Long and everything works except the first value -book. Since I want to treat -book like a boolean, I do
not give it value, which causes problems because I guess it needs to receive some sort of value in it's current format. This is causing problems
where the value of the second argument -title does not show. Is there a syntactical form I can follow to treat -book like a boolean and feed the other arguments
values? Can I continue to use Getopt::Long?
Any suggestions would be much appreciated.
CODE
-----------------------------------
use Getopt::Long;
use File::Path;
#use strict;
GetOptions (
"book=s" =>\$book,
"magazine=s" =>\$magazine,
"video=s" =>\$video,
"c=s" => \$chapter,
"t=s" => \$title,
);
##Routes to appropriate subroutine
print "SHOW TEST: $sPath\n";
if ($book) {
itemCreator($chapter, $title);
}
###SUBROUTINES
sub showCreator {
my($chapter, $title) = @_;
print "\n";
print "CHAPTER: $chapter\n"; ###Not appearing because -book did not receive an arguement :(
print "TITLE: $title: $template\n";
}
Trina Espinoza Guest
-
#40782 [NEW]: boolean argument type to function throws Catchable fatal error.
From: tims at arizona dot edu Operating system: Debian Linux PHP version: 5.2.1 PHP Bug Type: Scripting Engine problem Bug... -
Getopts::std
I am using various switches in my script. I have the Getopts::std module and I wrote the following line: getopts('F:S:D:T:I:plen', \%OPTS); How... -
boolean cfsqltype
Can someone please advise what cfsqltype should I used for boolean (true/false)? I've tried cf_sql_bit, but my results were inconsistent. i.e. ... -
Problem using Getopts package
I am getting errors when I try to get command line parameters. For example this small code doesn't work on my machine.... -
Big favour needed to test site combos
Hi All I know this is a big favour, but I knew I'd get good honest feedback from you. Basically users are saying that I'm having time out... -
Rob Hanson #2
RE: GetOpts: boolean and argument value combos
> Since I want to treat -book like a boolean
The problem is that you are telling Getopt::Long that "book" expects a
string value:
This is untested, but this should work.> "book=s" =>\$book,
It sets the value of $book to 1 if the option is present.> "book" =>\$book,
See the Getopt::Long docs, the section on Simple Options.
[url]http://search.cpan.org/author/JV/Getopt-Long-2.34/lib/Getopt/Long.pm#Simple_[/url]
options
Rob
-----Original Message-----
From: Trina Espinoza [mailto:trina@theorphanage.com]
Sent: Tuesday, September 23, 2003 9:11 PM
To: [email]beginners@perl.org[/email]
Subject: GetOpts: boolean and argument value combos
Hey Perl Peps,
I am stuck in the mudd and hoping someone can give me a few clues that will
help get me back on track.
I want to submit some arguments like so:
../script1.pl -book -title HP3 -chapter 04
NOTE: -book does not have an argument
Now I have been using GetOpt::Long and everything works except the first
value -book. Since I want to treat -book like a boolean, I do
not give it value, which causes problems because I guess it needs to receive
some sort of value in it's current format. This is causing problems
where the value of the second argument -title does not show. Is there a
syntactical form I can follow to treat -book like a boolean and feed the
other arguments
values? Can I continue to use Getopt::Long?
Any suggestions would be much appreciated.
CODE
-----------------------------------
use Getopt::Long;
use File::Path;
#use strict;
GetOptions (
"book=s" =>\$book,
"magazine=s" =>\$magazine,
"video=s" =>\$video,
"c=s" => \$chapter,
"t=s" => \$title,
);
##Routes to appropriate subroutine
print "SHOW TEST: $sPath\n";
if ($book) {
itemCreator($chapter, $title);
}
###SUBROUTINES
sub showCreator {
my($chapter, $title) = @_;
print "\n";
print "CHAPTER: $chapter\n"; ###Not appearing because -book did not
receive an arguement :(
print "TITLE: $title: $template\n";
}
Rob Hanson Guest
-
Daniel Staal #3
Re: GetOpts: boolean and argument value combos
--On Tuesday, September 23, 2003 18:10 -0700 Trina Espinoza
<trina@theorphanage.com> wrote:
Sure. There are two ways to do this, depending on whether you want> Hey Perl Peps,
>
> I am stuck in the mudd and hoping someone can give me a few clues
> that will help get me back on track.
>
> I want to submit some arguments like so:
> ./script1.pl -book -title HP3 -chapter 04
>
> NOTE: -book does not have an argument
> Now I have been using GetOpt::Long and everything works except the
> first value -book. Since I want to treat -book like a boolean, I do
> not give it value, which causes problems because I guess it needs
> to receive some sort of value in it's current format. This is
> causing problems where the value of the second argument -title does
> not show. Is there a syntactical form I can follow to treat -book
> like a boolean and feed the other arguments values? Can I continue
> to use Getopt::Long?
'--nobook' to set 'book' to false or not. "book" by itself does the
plain version: false if not present, true if present. "book=!" does
false if not present, true if present and not followed by '--nobook'.
(If nobook found afterward the book value will be set to false.)
Daniel T. Staal
---------------------------------------------------------------
This email copyright the author. Unless otherwise noted, you
are expressly allowed to retransmit, quote, or otherwise use
the contents for non-commercial purposes. This copyright will
expire 5 years after the author's death, or in 30 years,
whichever is longer, unless such a period is in excess of
local copyright law.
---------------------------------------------------------------
Daniel Staal Guest
-
Trina Espinoza #4
Re: GetOpts: boolean and argument value combos
Thanks for the suggestions. Your help was much appreciated. I ended using
"book" =>\$book, instead of book=s in my code.
After that my script no longer expected the string, which is good news.
Thanks to you both for helping me with that. I can continue
to script away again :)
-T> will> >
> >
> > ----- Original Message -----
> > From: "Trina Espinoza" <trina@theorphanage.com>
> > To: <beginners@perl.org>
> > Sent: Tuesday, September 23, 2003 6:10 PM
> > Subject: GetOpts: boolean and argument value combos
> >
> >
> > Hey Perl Peps,
> >
> > I am stuck in the mudd and hoping someone can give me a few clues that> receive> > help get me back on track.
> >
> > I want to submit some arguments like so:
> > ./script1.pl -book -title HP3 -chapter 04
> >
> > NOTE: -book does not have an argument
> > Now I have been using GetOpt::Long and everything works except the first
> > value -book. Since I want to treat -book like a boolean, I do
> > not give it value, which causes problems because I guess it needs to> not> > some sort of value in it's current format. This is causing problems
> > where the value of the second argument -title does not show. Is there a
> > syntactical form I can follow to treat -book like a boolean and feed the
> > other arguments
> > values? Can I continue to use Getopt::Long?
> >
> > Any suggestions would be much appreciated.
> >
> > CODE
> > -----------------------------------
> > use Getopt::Long;
> > use File::Path;
> > #use strict;
> >
> >
> > GetOptions (
> > "book=s" =>\$book,
> > "magazine=s" =>\$magazine,
> > "video=s" =>\$video,
> > "c=s" => \$chapter,
> > "t=s" => \$title,
> > );
> >
> > ##Routes to appropriate subroutine
> > print "SHOW TEST: $sPath\n";
> > if ($book) {
> > itemCreator($chapter, $title);
> > }
> > ###SUBROUTINES
> > sub showCreator {
> > my($chapter, $title) = @_;
> > print "\n";
> > print "CHAPTER: $chapter\n"; ###Not appearing because -book did>> > receive an arguement :(
> > print "TITLE: $title: $template\n";
> >
> > }
> >
> >Trina Espinoza Guest



Reply With Quote

