Gerard has asked for the wisdom of the Perl Monks concerning the following question:

Normally I only use here documents for html and I have never had any problems evaluating variables. i.e. Their value is always printed. However with the following piece of code
my $ret = $smtp->data(<<'EOM'); From: $email To: $unsubscribe Subject: EOM
$email and $unsubscribe seem to be printed instead of what is contained within them. I am sure there is a way around this, just not sure what it is.

Any help appreciated

Regards,

Gerard

Replies are listed 'Best First'.
Re: Evaluating variables in here documents
by Paladin (Vicar) on Jan 12, 2003 at 21:49 UTC

    Since you put the EOM in single quotes, the here doc gets treated like a single quoted string (no interpolation). If you put the EOM in double quotes, it gets treated like a double quoted string.

    Further Explaination: The quotes you use around EOM determine how it will treat the text.

    • Single quotes '' treat it like a single quoted string
    • Double quotes "" or no quotes treat it like a double quoted string
    • Back quotes `` treat it like commands to execute in the shell
      • Single quotes '' treat it like a single quoted string
      ... with small differences in handling backslashes:
      my $var1 = <<'END'; \' $new \' \\ \/ END my $var2 = q/\' $new \' \\ \/ /; print $var1, $var2; # outputs \' $new \' \\ \/ \' $new \' \ /

      -- Hofmator

      Aha!!! ++ to you. Excellent thanks. Works like a charm.
Re: Evaluating variables in here documents
by tall_man (Parson) on Jan 12, 2003 at 22:21 UTC
    You might also want to look into Text::Template, which provides a cleaner interface for filling in variables into text without having to use here documents.
Re: Evaluating variables in here documents
by osama (Scribe) on Jan 13, 2003 at 05:06 UTC
    I've seen people use double quotes for everything, I personally started using double quotes only for strings with $variables, and using single quotes for everything else.