Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: m//g behaves strange...

by pg (Canon)
on Nov 10, 2003 at 02:59 UTC ( [id://305775]=note: print w/replies, xml ) Need Help??


in reply to m//g behaves strange...

I had recently answered a post, Re: An Insane Typo Bug, and it relates to your question in an interesting way. The original post in that thread has a totally different face with your wonder, but both are about the same fact that, in scalar context, m// returns either 1 or 0.

For pos(), try this, it gives you 1 and 9, so pos() does work:

use strict; use warnings; $_ = "0123456789"; my $ret = m/0/g; print "ret = $ret, pos = " . pos() . "\n"; $ret = m/8/g; print "ret = $ret, pos = " . pos() . "\n";

Replies are listed 'Best First'.
Re: Re: m//g behaves strange...
by Anonymous Monk on Nov 10, 2003 at 03:08 UTC
    <pedantic> actually, m// in scalar context returns either 1 or "" (empty string). </pedantic>.

      You are very close to 100% right. However I do observe something else, and I don't let things escape easily.

      If I do this:

      use strict; use warnings; { $_ = "1234"; my $ret = /2/g; print "($ret)\n" } { $_ = "1234"; my $ret = /9/g; print "($ret)\n" }

      The outputs are 1 and "empty string", which indicate that you are right.

      However, try this:

      use strict; use warnings; { my $a = 0; print "(" . ~$a . ")\n"; } { my $a = 1; print "(" . ~$a . ")\n"; } { my $a = ""; print "(" . ~$a . ")\n"; }

      It returns:

      (4294967295) (4294967294) ()

      Remeber the return values for zero and empty string, and then try this:

      use strict; use warnings; { $_ = "1234"; my $ret; print ~ m/2/, "\n"; } { $_ = "1234"; my $ret; print ~ m/9/, "\n"; }

      It gives you:

      4294967294 4294967295

      Which indicates the "~" operator does receive 0, not "empty string". Rememebr that in the case that we explicitly pass "~" an empty string, it is not converted to 0

      However, if we do this:

      use strict; use warnings; { $_ = "1234"; my $ret; print ~ ($ret = m/2/), "\n"; print "($ret)\n"; } { $_ = "1234"; my $ret; print ~ ($ret = m/9/), "\n"; print "($ret)\n"; }

      You get:

      4294967294 (1) 4294967295 ()

      It seems that although $ret receives "empty string", "~" operator receives 0, again rememebr that we didn't see this kind of auto-convertion in the explicitly-passing-empty-string case.

        !1 has a string value of "" and a numeric value of 0 (so as to not generate a warning when used as a number -- 0+"" generates a warning). This is what Perl uses all over the place for 'false' (when it isn't using undef). In the C code for Perl, this is PL_sv_no.

                        - tye

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://305775]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (3)
As of 2024-04-19 02:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found