getopts::std

L

lnatz

I am trying to use Getopts::Std to make switches, some with arguments.
This is a piece of code from my overall script. UsageAndDie is a sub
created earlier in the script. If I don't use any switches, usageAndDie
is suppose to execute and print a warning. No matter what I do, even if
I don't use any switches UsageAndDie never works. And if I do use a
switch, lets say I use F to make a configuration file, $configFile is
not defined. I don't know what the problem is.

getopts('F:S:D:T:I:plen', \%OPTS);

$configFile = uc$OPTS{F};
$dbserver = uc$OPTS{S};
$database = uc$OPTS{D};
$table_name = uc$OPTS{T};
$purging_item = uc$OPTS{I};
$logical_delete = $OPTS{p};
$distinct_users = $OPTS{l};
$existing_users = $OPTS{e};
$nonexisting_users = $OPTS{n};
print "getting to use and die\n";

usageAndDie if (!defined $configFile
|| (!defined $dbserver
&& !defined $database
&& !defined $table_name
&& !defined $purging_item)) ;
 
U

usenet

lnatz said:
$configFile = uc$OPTS{F};
usageAndDie if (!defined $configFile ...

$configFile (and other variables) ARE defined. They might happen to be
null, though. A variable may be defined but null (false).

Just leave off the "defined" functions and it should work as you
expect.
 
P

Paul Lalli

lnatz said:
I am trying to use Getopts::Std to make switches, some with arguments.
This is a piece of code from my overall script. UsageAndDie is a sub
created earlier in the script. If I don't use any switches, usageAndDie
is suppose to execute and print a warning. No matter what I do, even if
I don't use any switches UsageAndDie never works

"never works" is a horrible error description. *How* does it --- oh
screw it. I've posted this rant more times than I can count. Google
search for my name plus "does not work", and you'll find it.

Here's a short-but-complete script (which, by the way, you didn't
bother to provide) which will show you why your subroutine will never
be called with your code:

$ perl -MData::Dumper -e'
$x = undef;
$X = uc $x;
print Dumper(\$x, \$X);
'
$VAR1 = \undef;
$VAR2 = \'';

. And if I do use a
switch, lets say I use F to make a configuration file, $configFile is
not defined.

As shown above, that's simply not true, as $configFile can't *not* be
defined with the code you gave us. Unless you actually meant
"$configFile does not contain the value I expect." And if that's the
case, I instinctively question how you actually used this flag on the
command line. Of course, you didn't show us that, either...
I don't know what the problem is.

I do. You haven't read or followed the posting guidelines for this
group.

[code retained for posterity]
getopts('F:S:D:T:I:plen', \%OPTS);

$configFile = uc$OPTS{F};
$dbserver = uc$OPTS{S};
$database = uc$OPTS{D};
$table_name = uc$OPTS{T};
$purging_item = uc$OPTS{I};
$logical_delete = $OPTS{p};
$distinct_users = $OPTS{l};
$existing_users = $OPTS{e};
$nonexisting_users = $OPTS{n};
print "getting to use and die\n";

usageAndDie if (!defined $configFile
|| (!defined $dbserver
&& !defined $database
&& !defined $table_name
&& !defined $purging_item)) ;

Paul Lalli
 
P

Paul Lalli

$configFile (and other variables) ARE defined. They might happen to be
null, though. A variable may be defined but null (false).

Please don't use the word "null" when talking about Perl to beginners.
"null" has no meaning in Perl. Variables are either defined or not
defined. If they are defined, they may be the empty string, or zero,
or the string containing zero. All of those three defined values, plus
the undefined value are false. Any other value is true.

Paul Lalli
 
U

usenet

Paul said:
Please don't use the word "null" when talking about Perl to beginners.

You're right. I should have said "empty." Or, maybe, "ain't got
nuthin' in it"
 
U

Uri Guttman

u> You're right. I should have said "empty." Or, maybe, "ain't got
u> nuthin' in it"


IMO null string is also an acceptable term but empty string is better for
newbies.

uri
 
A

anno4000

Uri Guttman said:
u> You're right. I should have said "empty." Or, maybe, "ain't got
u> nuthin' in it"


IMO null string is also an acceptable term but empty string is better for
newbies.

"Null string" would be clear enough, but without qualification "null"
should only be used when the context provides a definition. Perl
context doesn't, so saying "$x is null" is ambiguous.

Anno
 
U

Uri Guttman

u> You're right. I should have said "empty." Or, maybe, "ain't got
u> nuthin' in it"
a> "Null string" would be clear enough, but without qualification "null"
a> should only be used when the context provides a definition. Perl
a> context doesn't, so saying "$x is null" is ambiguous.

oh, i agree with you, hence my mention of null string and not null.

uri
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top