cvs commit: perlfaq perlfaq4.pod

[email protected] (brian d foy) 3 Jan 2005 18:22:17 -0000
Newsgroups perl.cvs.perlfaq
Message-ID <[email protected]>
cvsuser     05/01/03 10:22:17

  Modified:    .        perlfaq4.pod
  Log:
  * How can I expand variables in text strings?
  
     + removed an example that used a symbolic reference.  The other
     example covers that case just fine
  
     + wrap the s///ee in an eval.  If we try to evaluate an undeclared
     variable under strict, strict does what it does and stops the
     program
  
  * How do I expand function calls in a string?
  
     + removed reference to "How can I expand variables in text strings?"
     They are different beasts.
  
  Revision  Changes    Path
  1.57      +10 -17    perlfaq/perlfaq4.pod
  
  Index: perlfaq4.pod
  ===================================================================
  RCS file: /cvs/public/perlfaq/perlfaq4.pod,v
  retrieving revision 1.56
  retrieving revision 1.57
  diff -u -r1.56 -r1.57
  --- perlfaq4.pod	3 Nov 2004 22:47:56 -0000	1.56
  +++ perlfaq4.pod	3 Jan 2005 18:22:17 -0000	1.57
  @@ -1,6 +1,6 @@
   =head1 NAME
   
  -perlfaq4 - Data Manipulation ($Revision: 1.56 $, $Date: 2004/11/03 22:47:56 $)
  +perlfaq4 - Data Manipulation ($Revision: 1.57 $, $Date: 2005/01/03 18:22:17 $)
   
   =head1 DESCRIPTION
   
  @@ -598,9 +598,6 @@
   
       print "My sub returned @{[mysub(1,2,3)]} that time.\n";
   
  -See also ``How can I expand variables in text strings?'' in this
  -section of the FAQ.
  -
   =head2 How do I find matching/nesting anything?
   
   This isn't something that can be done in one regular expression, no
  @@ -949,20 +946,19 @@
   
   =head2 How can I expand variables in text strings?
   
  -Let's assume that you have a string like:
  +Let's assume that you have a string that contains placeholder
  +variables.
   
       $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
  +You can use a substitution with a double evaluation.  The
  +first /e turns C<$1> into C<$foo>, and the second /e turns
  +C<$foo> into its value.  You may want to wrap this in an
  +C<eval>: if you try to get the value of an undeclared variable
  +while running under C<use strict>, you get a fatal error.
   
  -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
  +    eval { $text =~ s/(\$\w+)/$1/eeg };
  +    die if $@;
   
   It's probably better in the general case to treat those
   variables as entries in some special hash.  For example:
  @@ -973,9 +969,6 @@
       );
       $text =~ s/\$(\w+)/$user_defs{$1}/g;
   
  -See also ``How do I expand function calls in a string?'' in this section
  -of the FAQ.
  -
   =head2 What's wrong with always quoting "$vars"?
   
   The problem is that those double-quotes force stringification--