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

Hello,

I am trying to expand a scalar variable in my cgi script and can not seem to get it to work correctly. Any help would be appreciated. I have written a simple script below to demonstrate my problem:

#!/usr/bin/perl use HTML::Template; use CGI qw(:standard); use CGI::Carp qw(warningsToBrowser fatalsToBrowser); $|=1; print "Content-type: text/html\n\n"; print '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">'."\n"; print "<html>\n"; print "<head>\n"; print "<title>TESTING</title>\n"; print "</head>\n"; print "<body>\n"; $download = "download"; $link = "http://129.107.52.101/cgi_data/result2007623204259.pdf"; print '<input type="button" value="Download PDF Report" onClick=$link, + $download); return false;">'; print "</body>\n"; print "</html>\n";

When I clicked on the download button to download the pdf file, it did not pop up a download window as I expected it to. I used the browser view source option and found out that $link and $download did not get expanded. Any thought on this?

Code tags added by GrandFather

Replies are listed 'Best First'.
Re: Expanding Scalar Variable
by Corion (Patriarch) on Jul 24, 2007 at 07:41 UTC

    See perlop about quotes. Single quotes perform no variable expansion. Double quotes do. Since you are already using double quotes inside of your string, you might want to use qq{} or sprintf or, as you do at the top of your script, HTML::Template to perform the variable substitution:

    print qq{<input type="button" value="Download PDF Report" onClick=$lin +k, $download); return false;">}; # or printf '<input type="button" value="Download PDF Report" onClick=%s, % +s); return false;">', $link, $download;
Re: Expanding Scalar Variable
by FunkyMonk (Bishop) on Jul 24, 2007 at 07:46 UTC
    Your using single quotes and Perl doesdoesn't "expand" variables inside them. You could use double quotes, but that would lead to lots of escaping of the quotes you've already used. Better to us the qq{} operator. See Quote and Quote-like Operators in perlop.
    print qq{<input type="button" value="Download PDF Report" onClick=$lin +k, $download); return false;">};

    Why do you go to the trouble of using those modules and then not using them?

    update: s/does/doesn't/

      Thank you all, you guys are the best