[tpm] Detecting failed matches

Fulko Hew fulko.hew at gmail.com
Mon Nov 21 10:00:03 PST 2011


On Mon, Nov 21, 2011 at 12:54 PM, Antonio Sun <antoniosun at lavabit.com> wrote:
> Hi,
>
> What would $1 be when there is no match found?
> How to detect failed pattern matches?

exit unless (/stuff/);

the match operator returns as a scalar, the number of matches.
so what I might have is:

if ($a, $b) = ($src_str =~ /(.*);(.*)/) {
   $x = "the first string is '$a'";
   $y = "the second string is '$b'";
} else {
   print "no match found\n";
}

Thats better than looking at the defined-ness of the extracted pattern.

But to answer the question... I believe $1 retains its previous value
on 'no match'.

> I want the program exit code to reflect the fact whether the pattern has
> been found or not.
> I tried to detect undef or empty string '', but all seemed doesn't work for
> me.
>
>         $ret = $1;
>     exit (defined($ret) ? 0 : 1);
>     exit (defined($1) ? 0 : 1);


More information about the toronto-pm mailing list