On Sun, Mar 06, 2005 at 11:21:23PM -0600, Charles K. Clarkson wrote:
> Harold Castro <mailto:[EMAIL PROTECTED]> wrote:

> : if the current content of $word is the word "big", and
> : I would want to print a here document appending
> : something to "big" like "foot":
> : 
> : print <<EOF;
> : 
> : $word."foot"     -> won't work, display it as is.
> : "$word"."foot"   -> neither this one
> : $wordfoot        -> error. it becomes a single
> :  variable named $wordfoot
> : 
> : EOF
> 
> my $word = 'big';
> 
> print <<EOF;
> 
> ${word}foot     -> Works Fine.
> 
> EOF
> 
> Charles K. Clarkson

Harold,

It looks like Charles already answered your question, but I wanted to
offer a couple of other thoughts that you might find useful.


1) A here-doc is really just a long string.  It can be either single-
   or double-quoted, but it defaults to double-quoted, like so:
  
      print <<'SINGLE';  # single quoted
      print <<"DOUBLE";  # double quoted
      print <<DOUBLE;    # double quoted


2) Because it's a string, you can do variable interpolation in it if
   it's double-quoted, but not if it's single-quoted. For example:

      my $word = 'big';
      print <<"EOF";  # double quotes, allows interpolation
      $word."foot"   
      EOF

   is effectively the same as:

      print "$word.\"foot\""   # big."foot"

   While:
 
      my $word = 'big';
      print <<'EOF';   # single quotes; no interpolation
      $word."foot"   
      EOF

   is effectively the same as:

      print '$word."foot"'   # $word."foot"


3) Because it's a string, you cannot interpolate in a call to a
   function or operator (except see the next point).  You saw this
   when you tried to use the '.'  concatenation operator.


4) (You may want to skip this item if you aren't familiar with
   references.  Or, you can read perldoc perlref to find out about
   them.)

   If you really, really, need to interpolate something other than a
   variable into a double-quoted heredoc (or any other double-quoted
   string), you can.  It isn't pretty, but it works:

      print <<"EOT";  # Must be double quoted (explicitly or by default)
      @{[ some_func() ]}
      EOT

   What this does is to create a reference to an anonymous array
   (that is, an array that does not have an explicit name) and then
   populate that array with the value(s) returned from
   C<some_func()>.  The reference to this array is then dereferenced
   into an actual array, which (just like any other array variable)
   will be interpolated into the double-quoted string that is your
   heredoc.  Whew!

   Note that you can replace C<some_func()> with any arbitrary Perl
   code and it will work.  So, your C< $word . "foot" > from above
   would have worked if you had done it this way...but the way that
   Charles suggested is /much/ better.

Hope this helps,

--Dks


      

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to