[svn:perlfaq] r10126 - perlfaq/trunk

[email protected] Sat, 27 Oct 2007 12:29:22 -0700 (PDT)
Newsgroups perl.cvs.perlfaq
Message-ID <[email protected]>
Author: comdog
Date: Sat Oct 27 12:29:20 2007
New Revision: 10126

Modified:
   perlfaq/trunk/perlfaq.pod
   perlfaq/trunk/perlfaq4.pod
   perlfaq/trunk/perlfaq5.pod
   perlfaq/trunk/perlfaq6.pod

Log:
* perlfaq5: Peter Holzer added "How can I open a filehandle to a string?"


Modified: perlfaq/trunk/perlfaq.pod
==============================================================================
--- perlfaq/trunk/perlfaq.pod	(original)
+++ perlfaq/trunk/perlfaq.pod	Sat Oct 27 12:29:20 2007
@@ -747,6 +747,10 @@
 
 =item *
 
+How can I open a filehandle to a string?
+
+=item *
+
 How can I output my numbers with commas added?
 
 =item *

Modified: perlfaq/trunk/perlfaq4.pod
==============================================================================
--- perlfaq/trunk/perlfaq4.pod	(original)
+++ perlfaq/trunk/perlfaq4.pod	Sat Oct 27 12:29:20 2007
@@ -1006,12 +1006,15 @@
 (contributed by brian d foy)
 
 If you can avoid it, don't, or if you can use a templating system,
-such as C<Text::Template> or C<Template> Toolkit, do that instead.
+such as C<Text::Template> or C<Template> Toolkit, do that instead. You
+might even be able to get the job done with C<sprintf> or C<printf>:
+
+	my $string = sprintf 'Say hello to %s and %s', $foo, $bar;
 
 However, for the one-off simple case where I don't want to pull out a
 full templating system, 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.
+to their variable's values:
 
 	my $foo = 'Fred';
 	my $bar = 'Barney';
@@ -1021,17 +1024,22 @@
 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.
+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:
+undefined variable names with the empty string. Since I'm using the
+C</e> flag (twice even!), I have all of the same security problems I 
+have with C<eval> in its string form. If there's something odd in
+C<$foo>, perhaps something like C<@{[ system "rm -rf /" ]}>, then
+I could get myself in trouble.
+
+To get around the security problem, 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';
 	

Modified: perlfaq/trunk/perlfaq5.pod
==============================================================================
--- perlfaq/trunk/perlfaq5.pod	(original)
+++ perlfaq/trunk/perlfaq5.pod	Sat Oct 27 12:29:20 2007
@@ -521,8 +521,27 @@
 =head2 How can I write() into a string?
 X<write, into a string>
 
-See L<perlform/"Accessing Formatting Internals"> for an swrite() function.
+See L<perlform/"Accessing Formatting Internals"> for an C<swrite()> function.
 
+=head2 How can I open a filehandle to a string?
+X<string>, X<open>, X<IO::Scalar>, X<filehandle>
+
+(contributed by Peter J. Holzer, [email protected])
+
+Since Perl 5.8.0, you can pass a reference to a scalar instead of the
+filename to create a file handle which you can used to read from or write to
+a string:
+
+	open(my $fh, '>', \$string) or die "Could not open string for writing";
+	print $fh "foo\n";
+	print $fh "bar\n";	# $string now contains "foo\nbar\n"
+
+	open(my $fh, '<', \$string) or die "Could not open string for reading";
+	my $x = <$fh>;	# $x now contains "foo\n"
+
+With older versions of Perl, the C<IO::String> module provides similar
+functionality.
+    
 =head2 How can I output my numbers with commas added?
 X<number, commify>
 

Modified: perlfaq/trunk/perlfaq6.pod
==============================================================================
--- perlfaq/trunk/perlfaq6.pod	(original)
+++ perlfaq/trunk/perlfaq6.pod	Sat Oct 27 12:29:20 2007
@@ -153,9 +153,8 @@
 X<$/, regexes in> X<$INPUT_RECORD_SEPARATOR, regexes in>
 X<$RS, regexes in>
 
-Up to Perl 5.8.0, $/ has to be a string.  This may change in 5.10,
-but don't get your hopes up. Until then, you can use these examples
-if you really need to do this.
+$/ has to be a string.  You can use these examples if you really need to 
+do this.
 
 If you have File::Stream, this is easy.