Can I use True and False like this in Perl ?

G

Guy

Instead of having the following throughout my script:

if ($adm = $password) { &DoThisAndThat; }

I think I'd rather have this at the beginning:

if ($adm = $password) {$adm = True;}

And then have the following throughout my script:

if ($adm) { &DoThisAndThat; }

Can I store (True) in a scalar, and use it like this?

Guy
 
S

sln

Instead of having the following throughout my script:

if ($adm = $password) { &DoThisAndThat; }

I think I'd rather have this at the beginning:

if ($adm = $password) {$adm = True;}

And then have the following throughout my script:

if ($adm) { &DoThisAndThat; }

Can I store (True) in a scalar, and use it like this?

Guy
use constant True => 1;

-sln
 
R

Randal L. Schwartz

Guy> Instead of having the following throughout my script:
Guy> if ($adm = $password) { &DoThisAndThat; }

I think you want eithr == (for numeric equal) or "eq" (for string equal).

Guy> I think I'd rather have this at the beginning:

Guy> if ($adm = $password) {$adm = True;}

I think you just want this:

$adm = $adm eq $password;

Guy> And then have the following throughout my script:

Guy> if ($adm) { &DoThisAndThat; }

Guy> Can I store (True) in a scalar, and use it like this?

Guy> Guy

And yes, that'll then work.

print "Just another Perl hacker,"; # the original
 
S

sln

Guy> Instead of having the following throughout my script:
Guy> if ($adm = $password) { &DoThisAndThat; }

I think you want eithr == (for numeric equal) or "eq" (for string equal).

Guy> I think I'd rather have this at the beginning:

Guy> if ($adm = $password) {$adm = True;}

I think you just want this:

$adm = $adm eq $password;

Guy> And then have the following throughout my script:

Guy> if ($adm) { &DoThisAndThat; }

Guy> Can I store (True) in a scalar, and use it like this?

Guy> Guy

And yes, that'll then work.

print "Just another Perl hacker,"; # the original

Nice tutorial. Slow day?
-sln
 
G

Guy

Randal L. Schwartz said:
Guy> Instead of having the following throughout my script:
Guy> if ($adm = $password) { &DoThisAndThat; }

I think you want eithr == (for numeric equal) or "eq" (for string equal).

Yes I'm sorry about that, I was not pasting code I just typing it on the fly
and I have to admit I'm not used to the double equal.
Guy> I think I'd rather have this at the beginning:

Guy> if ($adm = $password) {$adm = True;}

I think you just want this:

$adm = $adm eq $password;

Oh God I'm sorry, I meant eq.
Yes that looks good, so I assume that $adm will be true if $adm eq
$password.
Guy> And then have the following throughout my script:

Guy> if ($adm) { &DoThisAndThat; }

Guy> Can I store (True) in a scalar, and use it like this?

Guy> Guy

And yes, that'll then work.

I'll give it a try. thanks
Guy
 
S

sln

Yes I'm sorry about that, I was not pasting code I just typing it on the fly
and I have to admit I'm not used to the double equal.
Neither am I. Isn't one '=' enough, why have more is what I say.
-sln
 
J

Jürgen Exner

Guy said:
Instead of having the following throughout my script:
if ($adm = $password) { &DoThisAndThat; }

Are you certain that your passwords are numerical? That is rather
uncommon.

Why are you using the &-from of calling subroutines? Are you aware what
that form is doing?
I think I'd rather have this at the beginning:
if ($adm = $password) {$adm = True;}

Why not
$adm = ($adm eq $password); #paranthesis only for clarification
And then have the following throughout my script:

if ($adm) { &DoThisAndThat; }

Can I store (True) in a scalar, and use it like this?

Yes, but it will do something quite different than you probably think it
is doing.
Any, yes any Perl scalar has a boolean values, e.g. any non-empty string
evaluates as boolean true.

jue
 
G

Gunnar Hjalmarsson

Jürgen Exner said:
... any non-empty string evaluates as boolean true.

$ perl -le '
$_ = "0";
print length;
print $_ ? "True" : "False";
'
1
False
$

;-)
 
R

Randal L. Schwartz

sln> Neither am I. Isn't one '=' enough, why have more is what I say.

Because two works, and one doesn't? That's what I say. :)
 
S

sln

sln> Neither am I. Isn't one '=' enough, why have more is what I say.

Because two works, and one doesn't? That's what I say. :)
Hey, each to his own idiom(acy).
Thats what I say.
-sln
 
T

Tad J McClellan

Guy said:
I was not pasting code I just typing it on the fly


Have you seen the Posting Guidelines that are posted here frequently?



It is bad manners to quote .sigs.
 
R

RedGrittyBrick

Guy said:
Yes that looks good, so I assume that $adm will be true if $adm eq
$password.

Minor point: I'd avoid overloading $adm. I'd pick variable names that
better describe their actual use.

my $isAdm = $enteredPassword eq $admPassword;
...
if ($isAdm) { ... }
 

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

Forum statistics

Threads
474,214
Messages
2,571,111
Members
47,703
Latest member
robert.marryson

Latest Threads

Top