Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Re: m//g behaves strange...

by Anonymous Monk
on Nov 10, 2003 at 03:08 UTC ( #305778=note: print w/replies, xml ) Need Help??


in reply to Re: m//g behaves strange...
in thread m//g behaves strange...

<pedantic> actually, m// in scalar context returns either 1 or "" (empty string). </pedantic>.

Replies are listed 'Best First'.
Re: Re: Re: m//g behaves strange...
by pg (Canon) on Nov 10, 2003 at 03:59 UTC

    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
        Daddy?

        Yes, I know what you said. If this is the logical answer, so simple and straight, I have used it in my previous post already. Does not seem to me that you have read and understood my previous post carefully. What you said is right, but it does not mean anything here.

        Also I don't understand why you involve !1, when you can simply state that both 0 and "" are "false". How is this "!" operator get involved, when we are talking about "~" operator. They are relatives, but not the same person.

        Although Perl consider multiple values as "false", for a particular function, it usually always returns a single fixed consistent value to represent "false". Ask you an extream question, both 1 and 2 represents "true", when one of your function needs to return "true", will you consistently return 1, or will you sometime return 1 and sometime return 2?

        You failed to answer why should "~" receive 0, when "~" can be applied against "empty string" with no problem, and as we see thru lots of my demo, m// actually returns "empty string" from time to time to represent "false".

        Your 0+"" example also means nothing here. In your case, "+" operator has to be applied to numbers, but in my case, it is absolutely valid to use "~" operator against a string. try this:

        use strict; use warnings; print ~"abcd";

        From a logic point of view, what you did is like: I asked how much 1+1 is, and you answered 2+3=5.

        Update:

        Anonymous Monk's dump is useful, and my further explanation is that:

        • as print requires a string value, so "" is used.
        • for "~", although it can be used against both string and number, when both "" and 0 are available to be used, Perl arbitrarily picks 0.

        One reminder, the dump you given shows ""\0, please don't make up this !1. State what you observed, and only what you observed.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2021-07-28 06:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?