Help - Search - Member List - Calendar
Full Version: Appending constant string to variable inside a here-document
WorkTheWeb Forums > Webmaster Resources > Perl Beginner Help
Support our Sponsors!
Harold Castro
Hi,

I'm trying to loop into printing a series of lines so
I used here document. Substitution of variables, e.g.,
$_ works inside the HERE-Document but appending a
string won't work

For example:

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

Any idea how to append string constants to a variable
inside a here document?

Thanks!






__________________________________
Celebrate Yahoo!'s 10th Birthday!
Yahoo! Netrospective: 100 Moments of the Web
http://birthday.yahoo.com/netrospective/

Charles K. Clarkson
Harold Castro <mailto:[Email Removed]> wrote:

: I'm trying to loop into printing a series of lines so
: I used here document. Substitution of variables, e.g.,
: $_ works inside the HERE-Document but appending a
: string won't work
:
: For example:
:
: 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


HTH,

Charles K. Clarkson
--
Mobile Homes Specialist


David Storrs
On Sun, Mar 06, 2005 at 11:21:23PM -0600, Charles K. Clarkson wrote:
QUOTE
Harold Castro <mailto:[Email Removed]> 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


PHP Help | Linux Help | Web Hosting | Reseller Hosting | SSL Hosting
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2005 Invision Power Services, Inc.