Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!crdgw1!uunet!rbj From: rbj@uunet.UU.NET (Root Boy Jim) Newsgroups: comp.lang.perl Subject: Re: Puzzle Message-ID: <127421@uunet.UU.NET> Date: 3 Apr 91 19:52:23 GMT References: <1991Apr1.224210.28384@uvaarpa.Virginia.EDU> <1991Apr3.021455.27260@eng.umd.edu> Organization: UUNET Communications Services, Falls Church, VA Lines: 55 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