perl variables from open file doesn't work

Viewed 64

i need some help with a sendmail perl script.

So i have 1 part of html message included from file msg.txt and second part is already in my $html

I have 2 variables into msg.txt(first part) that doesn't work.. in the second part it works fine..

EDIT : So . in the msg.txt file i have the following : $website,$token that is declared in perl script. When i run the script i receive email containing names of variables($token,$website) instead value of variables..

CODE

$website = "domain.com";
$token = 'token123456';

open(IN,$list);
while(chop($email_line=<IN>)) {
    local $/ = undef;
    open MSG, "msg.txt";
    $msg = <MSG>;
    close FILE;

    my @html = "
   <html>
    $msg (!!! variables $website and $token doesn't work in $msg !!!)
    
    <div style='margin-top:5px; text-align: center;>
        Website : $site | Token : $token ( variables are working)
    </div>
</html>
 ";

}

OUTPUT

// this is first part
Website : $website | Token : $token
//this is second part that works
Website : domain.com | Token : token12345
1 Answers

You interpolate the contents of $msg into @html (which should probably be a scalar variable, not an array) and then you expect the Perl compiler to automatically run another level of interpolation to expand the variables that were in $msg and are now in $html. And Perl simply doesn't work like that. It only does a single level of interpolation without you jumping through a few hoops in order to tell it what you want it to do.

If you want to be a successful Perl programmer, then I highly recommend taking a few hours to read through the Perl FAQ. It will make your Perl programming life easier and more productive.

In particular, it includes this question and answer:

How can I expand variables in text strings?

The answer shows a complex method using a double /ee flag on a substitution operator, but it also gives the correct answer which is "use a templating system".

Related