[svn:perlfaq] r7954 - perlfaq/trunk

[email protected] Mon, 16 Oct 2006 13:07:59 -0700 (PDT)
Newsgroups perl.cvs.perlfaq
Message-ID <[email protected]>
Author: comdog
Date: Mon Oct 16 13:07:58 2006
New Revision: 7954

Modified:
   perlfaq/trunk/perlfaq4.pod

Log:
* How can I expand variables in text strings?
	+ replaced answer that incorrectly asserted that s///eeg
	need another eval {} to catch stricture violations


Modified: perlfaq/trunk/perlfaq4.pod
==============================================================================
--- perlfaq/trunk/perlfaq4.pod	(original)
+++ perlfaq/trunk/perlfaq4.pod	Mon Oct 16 13:07:58 2006
@@ -1004,29 +1004,45 @@
 
 =head2 How can I expand variables in text strings?
 
-Let's assume that you have a string that contains placeholder
-variables.
-
-	$text = 'this has a $foo in it and a $bar';
+(contributed by brian d foy)
 
-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.
-
-	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:
-
-	%user_defs = (
-		foo  => 23,
-		bar  => 19,
+For example, I'll use a string that has two Perl scalar variables
+in it. In this example, I want to expand C<$foo> and C<$bar> to
+their variable's values.
+
+	my $foo = 'Fred';
+	my $bar = 'Barney';
+	$string = 'Say hello to $foo and $bar';
+
+One way I can do this involves the substitution operator and a double
+C</e> flag.  The first C</e> evaluates C<$1> on the replacement side and
+turns it into C<$foo>. The second /e starts with C<$foo> and replaces
+it with its value. C<$foo>, then, turns into 'Fred', and that's finally
+what's left in the string.
+
+	$string =~ s/(\$\w+)/$1/eeg; # 'Say hello to Fred and Barney'
+	
+The C</e> will also silently ignore violations of strict, replacing
+undefined variable names with the empty string.
+
+I could also pull the values from a hash instead of evaluating 
+variable names. Using a single C</e>, I can check the hash to ensure
+the value exists, and if it doesn't, I can replace the missing value
+with a marker, in this case C<???> to signal that I missed something:
+
+	my $string = 'This has $foo and $bar';
+	
+	my %Replacements = (
+		foo  => 'Fred',
 		);
-	$text =~ s/\$(\w+)/$user_defs{$1}/g;
-
+		
+	# $string =~ s/\$(\w+)/$Replacements{$1}/g;
+	$string =~ s/\$(\w+)/
+		exists $Replacements{$1} ? $Replacements{$1} : '???'
+		/eg;
+		
+	print $string;
+	
 =head2 What's wrong with always quoting "$vars"?
 
 The problem is that those double-quotes force