in reply to value returned on m// failure

  1. Try this. You will find it is wrong when you said m// returns undef for non-integers, in fact the return value is empty string.
    $a = 123; print $a =~ m/^\d+$/, "\n"; $a = "abc"; $ret = ($a =~ m/^\d+$/); if (defined($ret)) { print "=$ret=\n";#print out == } else { print "undef";#not going this loop }
  2. Also try this, which tells you what is "true", what is "false" in Perl:
    sub true_false { if (shift()) { print "true\n"; } else { print "false\n"; } } true_false("a"); true_false(1); true_false(""); true_false(0); if (undef) { #I don't want to use true_false for this case, so it is g +uranteed that shift() does not play any magic print "true\n"; } else { print "false\n"; }
  3. Go perlop, and look under m//, there is a clear answer for the return value from m//, under different situations. Not just the context, but also the modifiers changes the meaning of the return value from m//

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

    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?

      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.

      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.

      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.