[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [FWP] Greenhorn coding question



On Tue, Jul 06, 1999 at 02:09:32PM -0400, Ronald J Kimball wrote:
> On Tue, Jul 06, 1999 at 10:58:03AM -0700, Roderick A. Anderson wrote:
> > my $foo = 'foo';
> > my $bar = 'bar';
> > 
> > # From 'man perlfaq4'
> > $text = 'this has a $foo in it and a $bar';
> > 
> > # Why doesn't this work as advertised with perl 5.004_4 on a stock
> > # RedHat Linux 5.2 system with perl 5.004_4.
> > $text =~ s/\$(\w+)/${$1}/g;
> > 
> > print $text;
> > 
> > # Comes out as this has a  in it and
> 
> You can't have symbolic references to lexical variables.  Lexical variables
> are not stored in the symbol table.

In fact, this is exactly the point being made in that entry of
perlfaq4.  Quoting:

     How can I expand variables in text strings?

     Let's assume that you have a string like:

         $text = 'this has a $foo in it and a $bar';

     If those were both global variables, then this would
     suffice:

         $text =~ s/\$(\w+)/${$1}/g;  # no /e needed

     But since they are probably lexicals, or at least, they
     could be, you'd have to do this:

         $text =~ s/(\$\w+)/$1/eeg;
         die if $@;                  # needed /ee, not /e

==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe