Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > behavior of m// operator

Reply
Thread Tools

behavior of m// operator

 
 
Nathan
Guest
Posts: n/a
 
      07-30-2008
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
 
Reply With Quote
 
 
 
 
Zak B. Elep
Guest
Posts: n/a
 
      07-30-2008
Nathan <(E-Mail Removed)> writes:

> 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.

--
I like the idea of 256 bits, though: 32 for the (Unicode) character leaves
room for 224 Bucky bits, which ought to be enough for anyone.
-- Roland Hutchinson, in alt.folklore.computers
 
Reply With Quote
 
 
 
 
xhoster@gmail.com
Guest
Posts: n/a
 
      07-30-2008
Nathan <(E-Mail Removed)> wrote:
> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ [13.9] intrinsic behavior of operator++(), operator++(int) gob00st@googlemail.com C++ 4 02-20-2009 07:27 PM
need virtual behavior for friend operator<<() noone C++ 5 01-26-2007 07:06 PM
The behavior of the operator % PengYu.UT@gmail.com C Programming 8 03-21-2005 08:11 AM
undefined behavior or not undefined behavior? That is the question Mantorok Redgormor C Programming 70 02-17-2004 02:46 PM
Unusual operator behavior Karlo Basic C++ 11 11-13-2003 09:44 AM



Advertisments