Boolean ?

J

Julia De Silva

Hi there all
I can do this in other languages. Have I got the syntax wrong or is this not
possible in Perl ?

my $testtime = 1107267063;
my $isbeforenow = ($testtime > time);
print"isbeforenow - $isbeforenow\n";

Output is >> isbeforenow -

TIA

J
 
A

Arndt Jonasson

Julia De Silva said:
I can do this in other languages. Have I got the syntax wrong or is this not
possible in Perl ?

my $testtime = 1107267063;
my $isbeforenow = ($testtime > time);
print"isbeforenow - $isbeforenow\n";

Output is >> isbeforenow -

You are assigning the result of a test to a variable, and printing it
out. Yes, you can - the value produced by logical operations to mean
"false" by Perl is the empty string, which is precisely what you're
getting.

Additional values are treated as "false" when being used in
tests. From "perldoc perlsyn":

The number 0, the strings C<'0'> and C<''>, the empty list C<()>, and
C<undef> are all false in a boolean context. All other values are true.
 
A

A. Sinan Unur

Hi there all
I can do this in other languages. Have I got the syntax wrong or is
this not possible in Perl ?

my $testtime = 1107267063;
my $isbeforenow = ($testtime > time);
print"isbeforenow - $isbeforenow\n";

Output is >> isbeforenow -

$isbeforenow is set to the empty string which is a false value in Perl.

You can do this:

my $isbeforenow = 0 + ($testtime > time);

if you really want to force the result to be numeric.

Sinan.
 
J

Julia De Silva

my $isbeforenow = 0 + ($testtime > time);

Excellent, just what I was after, thanks.

J
 
P

Paul Lalli

A. Sinan Unur said:
You can do this:

my $isbeforenow = 0 + ($testtime > time);

if you really want to force the result to be numeric.

An alternative suggestion would be to explicitly provide the two values
you want to have in your 'boolean' variable:

my $isbeforenow = $testtime > time ? 1 : 0;

Paul Lalli
 
A

Anno Siegel

A. Sinan Unur said:
$isbeforenow is set to the empty string which is a false value in Perl.

More precisely, comparisons return a scalar whose string value is empty
*and* whose numeric value is 0.
You can do this:

my $isbeforenow = 0 + ($testtime > time);

if you really want to force the result to be numeric.

If ($testtime > time) were just an empty string, you couldn't do this
under warnings.

Anno
 
I

ioneabu

Hey, your right. This produces an error:
#!/usr/bin/perl
use strict;
use warnings;

my $a = 0 + '';
 
A

A. Sinan Unur

(e-mail address removed)-berlin.de (Anno Siegel) wrote in @mamenchi.zrz.TU-Berlin.DE:
...
More precisely, comparisons return a scalar whose string value is
empty *and* whose numeric value is 0.

Thanks for catching that.

Sinan
 
J

jl_post

A. Sinan Unur said:
You can do this:

my $isbeforenow = 0 + ($testtime > time);

if you really want to force the result to be numeric.


Another alternative suggestion is this:

my $isbeforenow = ($testtime > time) || 0;

(This way it's guaranteed to be zero if it turns out
to be false.)

-- Jean-Luc
 

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
474,167
Messages
2,570,910
Members
47,453
Latest member
MadelinePh

Latest Threads

Top