behavior of m// operator

N

Nathan

According to the documentation this program should print nothing since
m// uses the last successful pattern match from m//, split, etc. However
on my linux box it prints 1:

#!/usr/bin/perl -w

$a = "a";
$b = "b";
split /a/,$a;
print $b =~ m//;

Is the documentation wrong?

-Nathan
 
Z

Zak B. Elep

Nathan said:
According to the documentation this program should print nothing since
m// uses the last successful pattern match from m//, split, etc. However
on my linux box it prints 1:

#!/usr/bin/perl -w

$a = "a";
$b = "b";
split /a/,$a;
print $b =~ m//;

Is the documentation wrong?

No, it is correct. `print' emits 1 because the succeeding match
operation ($b =~ m//) succeeded (m// essentially matches a `null'
string, which $b has:

,----[ perl -de 0 ]
| Loading DB routines from perl5db.pl version 1.28
| Editor support available.
|
| Enter h or `h h' for help, or `man perldebug' for more help.
|
| main::(-e:1): 0
| DB<1> ($a, $b) = ( qw(a b) )
|
| DB<2> x split /a/ => $a
| empty array
| DB<3> print $b =~ m//
| 1
| DB<4> x $b =~ m//
| 0 1
| DB<5>
`----

Because you used `=~', m// tries to match from $b, not from $a (really
$_) as you would expect.
 
X

xhoster

Nathan said:
According to the documentation this program should print nothing since
m// uses the last successful pattern match from m//, split, etc.

My docs don't explicitly say whether the regex used in a split counts
as previously "matched" or not in this context. Experimentally, it looks
to me like it does not.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
 

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
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top