[comp.lang.perl] pattern match strangeness

scott@sctc.com (Scott Hammond) (04/11/91)

When $s is set to "", the program below says:

match, s=
no match

Both matches work for a non-null $s.

Am I doing this wrong?


#!/usr/local/bin/perl

$s = "";

$pat = "/est/";

if ($s =~ eval $pat)

    { print "match, s= $s\n"; }

else

    { print "no match\n"; }

if ($s =~ /est/)

    { print "match, s= $s\n"; }

else

    { print "no match\n"; }

$Header: perly.c,v 3.0.1.10 91/01/11 18:22:48 lwall Locked $
Patch level: 44

tchrist@convex.COM (Tom Christiansen) (04/11/91)

From the keyboard of scott@sctc.com (Scott Hammond):
:When $s is set to "", the program below says:
:
:match, s=
:no match
:
:Both matches work for a non-null $s.
:
:Am I doing this wrong?

i think so.

:#!/usr/local/bin/perl
:
:$s = "";
:
:$pat = "/est/";
:
:if ($s =~ eval $pat)

right here is the error: /est/ evals to null or 1, depending on 
what $_ is set to.  you mean:

    if (eval "\$s =~ $pat")

or better:

    $pat = 'est';
    if ($s =~ /$pat/)

:    { print "match, s= $s\n"; }
:
:else
:
:    { print "no match\n"; }
:
:if ($s =~ /est/)
:
:    { print "match, s= $s\n"; }
:
:else
:
:    { print "no match\n"; }

--tom