Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!uakari.primate.wisc.edu!ra!emory!gatech!udel!haven!mimsy!mojo!ziegast From: ziegast@eng.umd.edu (Eric W. Ziegast) Newsgroups: comp.lang.perl Subject: Re: Puzzle Message-ID: <1991Apr3.021455.27260@eng.umd.edu> Date: 3 Apr 91 02:14:55 GMT References: <1991Apr1.224210.28384@uvaarpa.Virginia.EDU> Sender: news@eng.umd.edu (C-News) Organization: College of Engineering, Maryversity of Uniland, College Park Lines: 44 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)