For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > March 2005 > Appending constant string to variable inside a here-document









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Appending constant string to variable inside a here-document
Harold Castro

2005-03-07, 3:58 pm

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

2005-03-07, 3:58 pm

Harold Castro <mailto:b0ydaem0n@yahoo.com> 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
254 968-8328


David Storrs

2005-03-08, 8:56 am

On Sun, Mar 06, 2005 at 11:21:23PM -0600, Charles K. Clarkson wrote:
> Harold Castro <mailto:b0ydaem0n@yahoo.com> 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



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com