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

Sometimes i get a piece of html code ( such as a banner ad) which i wish to add to my script, these are often many lines long and often contain ugly looking javascript also. To add this code i normally add the following to my script
print "a target=\"top\" ......... </noscript> ";
But in order for this to work i need to go through the piece of code and format it, e.g. replacing " with \" for all " in the code. Is there anyway method of printing in perl which would allow me to add the code without reformatting it, as i always make mistakes when im reformatting.

Replies are listed 'Best First'.
Re: printing html and javascript from a cgi doc
by davorg (Chancellor) on Jan 03, 2002 at 21:41 UTC

    There are a couple. Either:

    print qq(a target="top" ......... </noscript>);

    or

    print <<END_HTML; a target="top" ......... </noscript> END_HTML

    will both work.

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: printing html and javascript from a cgi doc
by grep (Monsignor) on Jan 03, 2002 at 21:59 UTC
    You might also want to look a a templating system like Template Toolkit or Mason. Not only do not have to worry about escaping your quotes, but you move your HTML out of your code. If you have ever tried to debug inline HTML in your perl code you'll understand why that is so great.

    grep
    grep> cd pub grep> more beer
Re: printing html and javascript from a cgi doc
by Corion (Patriarch) on Jan 03, 2002 at 21:53 UTC

    There would be two ways for you to go :

    • Use an external file for your banner ad.
      This will be the easiest method to add to your code, as you simply check if there is a file (called, say, banner.ad), read it in and print it to the user. The problem with this approach is that webserver permissions and the concept of current directory are obstacles to be circumvented. Here is some (untested) example code that should get you started in this direction :
      function getAd { my ($filename) = @_; my $result = "<!-- No HTML ad -->; # the HTML use FindBin; $filename = $FindBin::Bin . "/" . $filename; if ( -f $filename ) { { local $/; open F, "< $filename" or die "Couldn't open '$filename' : $!"; $result = <F>; close F; }; return $result; };
    • Use Here-Documents for your banner ad.
      This is not so easy to set up, as you will have to modify your CGI every time the ad changes, but if you got your CGI running, you also get the ad running. A here-doc means that Perl will read all the text from <<'MARKER' until it encounters the end-marker MARKER at the start of a line all by itself. Some code :
      function getAd { return <<'END_OF_AD' <a href="http://www.bar.com/gotcha"><img src="http://www.bar.com/foo.g +if" alt="Foo"> </a> END_OF_AD };

    perl -MHTTP::Daemon -MHTTP::Response -MLWP::Simple -e ' ; # The $d = new HTTP::Daemon and fork and getprint $d->url and exit;#spider ($c = $d->accept())->get_request(); $c->send_response( new #in the HTTP::Response(200,$_,$_,qq(Just another Perl hacker\n))); ' # web
Re: printing html and javascript from a cgi doc
by gav^ (Curate) on Jan 03, 2002 at 23:24 UTC
    I'll have to add my $0.02 and pimp for HTML::Template which works a treat for simple templating.
    This is in my template and we'll put <TMPL_VAR NAME=TEXT> here.
    my $t = HTML::Template->new(filename => 'blah.tmpl'); $t->param(TEXT => 'some text'); print $t->output;
    :)
Re: printing html and javascript from a cgi doc
by Juerd (Abbot) on Jan 04, 2002 at 00:05 UTC
    Moving the html out of your script using some templating tool is obviously the best solution.
    Many have suggested the use of here-docs (<<EOF).

    I dislike heredocs. They make me feel old, they're ugly (no offense).

    However, I do like being able to put literal linebreaks in string constants.
    my $foo = 'First line Second line Third line';
    But that's even worse than a here-doc. Fortunately, 90% of all cases where I want multiline string constants, it's about a piece of HTML. And HTML parsers don't really care about whitespace.
    my $foo = ' First line<br> Second line<br> Third line<br> ';
    That looks a lot better. But there's another problem:
    my $foo = " First line<br> $second_line<br> <a href=\"foo.html\">Third line</a> ";
    Some people love backslash escapes, but I hate to escape my delimiter. But perl can handle alternative delimiters. '' is q// and "" is qq//. Instead of /, any non-whitespace character can be used (note: if the character is alphanumeric, whitespace is _required_ after the operator's letters (q mfoom eq 'foo'). You can use grouping characters (() [] {} [] <>) and perl will keep track of nesting.
    my $foo = qq{ First line<br> $second_line<br> <a href="foo.html">Third line</a> };


    Read about heredocs, quote-like operators, backslash escapes and DWIM (Do What I Mean) in perlop.

    2;0 juerd@ouranos:~$ perl -e'undef christmas' Segmentation fault 2;139 juerd@ouranos:~$

Re: printing html and javascript from a cgi doc
by jonjacobmoon (Pilgrim) on Jan 03, 2002 at 22:05 UTC
    Grep said what I was going to say. Damn him : ) I do have to add that embedding html (or any print stmts like that) within the Perl code is a MAJOR pet peeve of mine. Avoid it when possible. I also have to add a vote for Template Toolkit. It is most useful.