=~ return value ? boolean ?

T

tamiry

Hi,
I wanted to know if the matching operator returns a boolean value.

I want to replace:
if($file_name =~ /_prev_txt$/)
{$idx = 1}
else
{$idx = 0}
print "$idx"

with:
$idx = ($file_name =~ /_prev_txt$/);
print "$idx"

But, with the second piece of code, where $file_name
was 'data_cur_txt', I got an empty line. so what does
=~ return ?

lots of thanks
 
G

Gunnar Hjalmarsson

tamiry said:
I wanted to know if the matching operator returns a boolean value.

I want to replace:
if($file_name =~ /_prev_txt$/)
{$idx = 1}
else
{$idx = 0}
print "$idx"

with:
$idx = ($file_name =~ /_prev_txt$/);
print "$idx"

But, with the second piece of code, where $file_name
was 'data_cur_txt', I got an empty line. so what does
=~ return ?

An empty string, apparently.

if ($idx) {
print "$idx\n";
} elsif ( defined $idx and $idx eq '' ) {
print "False\n";
}
 
J

Joe Smith

tamiry said:
$idx = ($file_name =~ /_prev_txt$/);
print "$idx"

But, with the second piece of code, where $file_name
was 'data_cur_txt', I got an empty line. so what does
=~ return ?

It returns true or false. In this case, false is ''. Use
print $idx+0;
to get a numeric answer.
-Joe
 
D

David K. Wall

tamiry said:
I wanted to know if the matching operator returns a boolean value.

I want to replace:
if($file_name =~ /_prev_txt$/)
{$idx = 1}
else
{$idx = 0}
print "$idx"

with:
$idx = ($file_name =~ /_prev_txt$/);
print "$idx"

But, with the second piece of code, where $file_name
was 'data_cur_txt', I got an empty line. so what does
=~ return ?

Hmm, I wonder what it says in perlop about binding operators?

Binding Operators

Binary "=~" binds a scalar expression to a pattern
match. Certain operations search or modify the string
$_ by default. This operator makes that kind of
operation work on some other string. The right argument
is a search pattern, substitution, or transliteration.
The left argument is what is supposed to be searched,
substituted, or transliterated instead of the default
$_. When used in scalar context, the return value
generally indicates the success of the operation.
Behavior in list context depends on the particular
operator. See "Regexp Quote-Like Operators" for details.
 
X

xhoster

tamiry said:
Hi,
I wanted to know if the matching operator returns a boolean value.

What is a boolean value in perl? Unlike Java, Perl allows all values to
be used in a boolean context.

Xho
 
S

Stephane Zuckerman

Hi,
What is a boolean value in perl? Unlike Java, Perl allows all values to
be used in a boolean context.
Well, Perl acts much like C regarding "semi-boolean" contexts : a variable
is either {undef,0} ({NULL,0} in C), or is "true" (any other value).
In this context, the answer to the OP is "yes, when the the matching
operator has found a match, it returns a non-{undef,0} value."
 
G

Gunnar Hjalmarsson

Stephane said:
In this context, the answer to the OP is "yes, when the the matching
operator has found a match, it returns a non-{undef,0} value."

It returns neither undef nor '0'. It returns ''.
 
S

Stephane Zuckerman

In this context, the answer to the OP is "yes, when the the matching
It returns neither undef nor '0'. It returns ''.

Ooooops. I knew I had forgotten one "false" value.
 
A

Anno Siegel

Gunnar Hjalmarsson said:
It returns neither undef nor '0'. It returns ''.

That's not the whole truth either. Boolean false in Perl is dual-valued:
Used as a number it is 0, used as a string, it is ''. A true value
behaves like an ordinary 1.

printf "False as number: %d, as string: '%s'\n", !1, !1;
printf "True as number: %d, as string: '%s'\n", !!1, !!1;

False as number: 0, as string: ''
True as number: 1, as string: '1'

Anno
 
G

Gunnar Hjalmarsson

Anno said:
That's not the whole truth either. Boolean false in Perl is dual-valued:
Used as a number it is 0, used as a string, it is ''. A true value
behaves like an ordinary 1.

printf "False as number: %d, as string: '%s'\n", !1, !1;
printf "True as number: %d, as string: '%s'\n", !!1, !!1;

False as number: 0, as string: ''
True as number: 1, as string: '1'

Thanks Anno, very elucidative.
 
T

Tad McClellan

And that could change at any version, since it is documented
to return only "false", not any of the particular values
that qualify as false.

Ooooops. I knew I had forgotten one "false" value.


It looks to me like you had forgotten _two_ (of the four) false values:

undef

numerically zero

empty string

one-character string where the one character is the zero digit
 

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,170
Messages
2,570,925
Members
47,468
Latest member
Fannie44U3

Latest Threads

Top