[comp.lang.perl] Puzzle

worley@compass.com (Dale Worley) (04/02/91)

The following program:

    $f = '/usr';
    $a = ($f =~ m#/usr($|/)#);
    $b = ($f =~ m#${f}($|/)#);
    print "\$a = '$a'\n";
    print "\$b = '$b'\n";

produces the output:

    $a = '1'
    $b = ''

Why are the two apparently similar pattern matches doing different
things?  Is ${f} absorbing the following parenthesized pattern as a
subscript?  If so, how is it succeeding, since "$|/" is not a valid
expression?  Am I going crazy?

Dale

Dale Worley		Compass, Inc.			worley@compass.com
--
If you live to a ripe old age and get what you want, you probably
aren't a fool.	-- Walter M. Miller, Jr.

ziegast@eng.umd.edu (Eric W. Ziegast) (04/03/91)

worley@compass.com writes:
>The following program:
>
>    $f = '/usr';
>    $a = ($f =~ m#/usr($|/)#);
>    $b = ($f =~ m#${f}($|/)#);
>    print "\$a = '$a'\n";
>    print "\$b = '$b'\n";
>
>produces the output:
>
>    $a = '1'
>    $b = ''
>
>Why are the two apparently similar pattern matches doing different
>things?  Is ${f} absorbing the following parenthesized pattern as a
>subscript?  If so, how is it succeeding, since "$|/" is not a valid
>expression?  Am I going crazy?

Variables, as far as I know, are *not* evaluated in expressions.
Note what $ is really used for in expressions, end of expression.

The ${f} is what causes a problem for $b.  The $| is not evaluated
either, but it is not clear in what context you are using it.

In order to evaluate variables inside your regex, you have to use
'eval' to evaluate the variables first before the expression.
Note that variable that you don't want evaluated by eval are
preceded with a '\'.

The following:
    $f = '/usr';
    $a = ($f =~ m#/usr($|/)#);
    eval "\$b = (\$f =~ m#${f}(\$|/)#)";
    print "\$a = '$a'\n";
    print "\$b = '$b'\n";

produces:

    $a = '1'
    $b = '1'
________________________________________________________________________
Eric W. Ziegast, University of Merryland, Engineering Computing Services
ziegast@eng.umd.edu - Eric@(301.405.3689)

rbj@uunet.UU.NET (Root Boy Jim) (04/04/91)

ziegast@eng.umd.edu (Eric W. Ziegast) writes:
>worley@compass.com writes:
>>The following program:
>>
>>    $f = '/usr';
>>    $a = ($f =~ m#/usr($|/)#);
>>    $b = ($f =~ m#${f}($|/)#);
>>    print "\$a = '$a'\n";
>>    print "\$b = '$b'\n";
>>
>>produces the output:
>>
>>    $a = '1'
>>    $b = ''
>>
>>Why are the two apparently similar pattern matches doing different
>>things?  Is ${f} absorbing the following parenthesized pattern as a
>>subscript?  If so, how is it succeeding, since "$|/" is not a valid
>>expression?  Am I going crazy?
>
>Variables, as far as I know, are *not* evaluated in expressions.
>Note what $ is really used for in expressions, end of expression.

Of course they are! That's what variables are for!
I assume you meant "vars are not eval'ed in patterns (m,s,tr,split)".

Well, the answer is different for each one. Split and m// treat
patterns as if they were double quoted, that is, interpolation
occurs. tr/// treats them as single quoted, making them literal.
Last but most, s/// will do either, depending on whether or not
the slash is a single quote or anything else, except when
backquotes are used, in which case command substitution occurs,
except on Tuesday or at night if perl is installed in /phis/bin.

For your added pleasure, a variety of flags are available.
Perhaps someday Larry will unify these a bit more.

>The ${f} is what causes a problem for $b.  The $| is not evaluated
>either, but it is not clear in what context you are using it.

In regular expressions, $| and $) are not treated as variable
references, but as separate characters; the $ means end of line
and the (|) are used for alternation of patterns.

>In order to evaluate variables inside your regex, you have to use
>'eval' to evaluate the variables first before the expression.

Please check your facts before you post.

BTW, while talking to Merlyn on the phone I heard him mumble
"perl -de 0" in order to get an interactive debugger. Since I
haven't seen this tip in the book, I thought I'd pass it on.
-- 
		[rbj@uunet 1] stty sane
		unknown mode: sane