Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Re: How do you replace variables in an external string?

by Juerd (Abbot)
on Apr 04, 2002 at 19:33 UTC ( #156740=note: print w/replies, xml ) Need Help??

in reply to How do you replace variables in an external string?

Hello $cgi->param('name'), how are you today

For some reason, the author didn't realise that hashes are the most natural way of providing key/value-based information. Of course, the object orientation is a laudable approach in general, but CGI scripts often use the values in strings, and not having a hash is actually making the script less readable. It's one of my reasons for not liking

Some (lshatzer and Kanji) have already given ways to use to put everything in a hash, so I'm not going to repeat them. I'll try to explain why what you tried didn't work.

You probably (hopefully) already know that variable interpolation works only with certain quoting operators, of which "" (double quotes, also known as qq//) is the most popular. The problem is that $cgi->param('name') is not a variable. It's a method call on $cgi. $cgi itself is a variable, and because there's no [ or { following the arrow, $cgi is used, and followed by a literal minus, greater than sign and the literal text param('name').

$cgi probably is a CGI object, and because of that, its string representation (Perl's one-way reference-to-string conversion) is CGI=HASH(0x...), with a memory address instead of dots, is used. Before the equal sign is the package name into which the reference was blessed, after it is a normal hash reference string representation. If you don't know what I'm talking about, just ignore the parts you don't get, and add "read perlref and perltoot" to your to-do list.

The following solutions would work:

my $name = $cgi->param('name'); print "Hello $name, how are you today?";
And so would non-interpolating string concatenation:
print 'Hello ' . $cgi->param('name') . ', how are you today?';
As would passing a list to print:
print 'Hello ', $cgi->param('name'), ', how are you today?';
And this trick also works (see also How do I expand function calls in a string?):
print "Hello ${\ $cgi->param('name') }, how are you today?";

Good luck!


Replies are listed 'Best First'.
Re: Re: How do you replace variables in an external string?
by extremely (Priest) on Apr 04, 2002 at 22:05 UTC
    "For some reason, the author didn't realise that hashes are the most natural way of providing key/value-based information."

    Actually, I'm pretty sure Lincoln knew that. I'm also sure that Lincoln wanted the object interface to deal with assignment to the argument set as well as reading it. He tied the hash from Vars later so you could have hash-like access but the first iterations of CGI predate tied hashes.

    And, BTW, he also has this note in the man pages:

    When using this, the thing you must watch out for are mul­-
    tivalued CGI parameters.  Because a hash cannot distin­-
    guish between scalar and list context, multivalued parame-­
    ters will be returned as a packed string, separated by the
    "\0" (null) character.  You must split this packed string
    in order to get at the individual values.  This is the
    convention introduced long ago by Steve Brenner in his module for Perl version 4.

    $you = new YOU;
    honk() if $you->love(perl)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://156740]
Jar. Jar!...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (8)
As of 2017-10-17 20:58 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (238 votes). Check out past polls.