Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

How do you replace variables in an external string?

by Anonymous Monk
on Apr 04, 2002 at 18:42 UTC ( #156728=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

If I were to read the contents of a file into a scalar. Who's contents were:

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

And print out that scalar, the $cgi->param('name') would not be replaced with the actual value in my cgi object.

Is there a function that can be used on the scalar to process its contents, replacing all occurences of variables with their actual values, without having to use the replace function and pattern matching?

Comment on How do you replace variables in an external string?
Download Code
Re: How do you replace variables in an external string?
by lshatzer (Friar) on Apr 04, 2002 at 19:10 UTC
    Two quick ideas, are you using single quotes to assign that variable, such as?
    my $line = 'Hello $cgi->param("name"), how are you today?';
    If not, I believe you can't use method calls inside a string, and expect it to interpolate, use an temp variable, such as:
    my $name = $cgi->param('name');
    and then:
    my $line = "Hello $name, how are you today?";
    If making an temp variable for each param, then a nice hash will do: (Untested)
    my %cgiform; for my $key ($cgi->param) { $cgiform{$key} = $cgi->param($key); }
    And then you can just use $cgiform{name}.

    Update: If you have more than one value with each form input, follow Kanji's suggestion bellow.
      And then you can just use $cgiform{name}.

      Unfortunately, this method only works if you're expecting there to be only one value associated with name.

      However, if you use CGI's import_names('FORM') method, you can get the first value using $FOO::name or get all the values with @FOO::name.

          --k.


Re: How do you replace variables in an external string?
by Juerd (Abbot) on Apr 04, 2002 at 19:33 UTC

    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
    

      "For some reason, the CGI.pm 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
      cgi-lib.pl module for Perl version 4.

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

Re: How do you replace variables in an external string?
by Amoe (Friar) on Apr 05, 2002 at 14:07 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://156728]
Approved by VSarkiss
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (12)
As of 2014-12-21 08:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (104 votes), past polls