in reply to Re: value returned on m// failure
in thread value returned on m// failure

i am quite aware of what values are ture and what are false. explicitly, undef and empty string are false values. and the perlop says...

       m/PATTERN/cgimosx
       /PATTERN/cgimosx
               Searches a string for a pattern match, and in
               scalar context returns true if it succeeds, false
               if it fails. ...

...it doesn't say anything about what the actual values are.

i was asking why is that to express success number 1 is returned, yet a non-number is returned on failure. is it an implementation detail/decision such that empty string is easier/faster to return than the number 0?

Replies are listed 'Best First'.
Re: Re: Re: value returned on m// failure
by pfaut (Priest) on Dec 31, 2002 at 01:41 UTC

    It doesn't return 1 for success. It returns the number of matches (or substitutions for s/// or tr///). If you're not using the g option, it stops on the first match and returns 1. I'm not really up on perl internals but I would guess undef is represented as a null pointer whereas a zero would require creation of an SV.

    --- print map { my ($m)=1<<hex($_)&11?' ':''; $m.=substr('AHJPacehklnorstu',hex($_),1) } split //,'2fde0abe76c36c914586c';

      right you are. i know that s/// & tr/// return the number of matched; never occured to me that m// behaves the same.

      i goofed when i wrote that number 1 (be it as string or numeric) was being returned on a regex match. it just happens that only one thing was being matched.

      thanks for the clue.

Re: Re: Re: value returned on m// failure
by MarkM (Curate) on Dec 31, 2002 at 04:50 UTC

    m// returns true or false in scalar context. Perl defines:

    NAMEValue as StringValue as NumberPerl Internal Variable
    true"1"1PL_sv_yes
    false""0PL_sv_no

    For evidence, see the source code module perl.c where PL_sv_yes and PL_sv_no are initialized. Also see the pp_match code in pp_hot.c where RETPUSHYES and RETPUSHNO are used to return a boolean value when invoked in scalar context without /g.

      ah, so that is why bitwise AND operation w/ numeric** 1 yields 0 whenever following fails...

      (defined $var && $var =~ m/regex/) & 1

      ...which otherwise would have produced an empty string. thanks mark.

      ** per perlop, an numeric operand implies numeric bitwise operation; per the table mark posted, false value is zero in numeric context.

Re: Re: Re: value returned on m// failure
by pg (Canon) on Dec 31, 2002 at 01:39 UTC
    Let me show you a piece of code first:
    $_ = 123; print 1 + m/^\d+$/, "\n"; # get 2 print "1" . m/^\d+$/, "\n"; #get 11
    What does this piece of code tell us? It clearly tells us that, it is meaningless to ask the question whether m// returned a string or a number. The answer is only meaningful, when you use the returned value for a PURPOSE, either as a number, or as a string.

    This is the flexibility we get from Perl.

    Something not related, yet related:

    Before you look at it, the Schrodinger's cat is both dead and alive. Dead or alive only becomes determined after you looked at the cat.