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

For some reason, the CGI.pm 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 CGI.pm.

Some (lshatzer and Kanji) have already given ways to use CGI.pm 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!

U28geW91IGNhbiBhbGwgcm90MTMgY
W5kIHBhY2soKS4gQnV0IGRvIHlvdS
ByZWNvZ25pc2UgQmFzZTY0IHdoZW4
geW91IHNlZSBpdD8gIC0tIEp1ZXJk


In reply to Re: How do you replace variables in an external string? by Juerd
in thread How do you replace variables in an external string? by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.