Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: print content in cgi and html

by barrd (Canon)
on Sep 26, 2003 at 13:20 UTC ( [id://294407]=note: print w/replies, xml ) Need Help??


in reply to print content in cgi and html

Hi Anonymous Monk,

TIMTOWTDI

You can also use "HERE documents", e.g:

print <<_EOF; <html><head> <title>My page title</title> </head> <body> ... blah blah </body></html> _EOF

The line containing _EOF must not contain any white space at the beginning or end or it will not work!

However I would highly recommend looking at a templating system such as the excellent HTML::Template module which will allow you to extract your HTML code from your perl code. This makes both far easier to maintain.

HTH ~ barrd

Replies are listed 'Best First'.
Re: Re: print content in cgi and html
by davido (Cardinal) on Sep 26, 2003 at 17:16 UTC
    HERE documents are good advice even if you do use CGI.pm, for those situations where the nested function structure that CGI.pm imposes proves to be too unwieldly (for example, constructing a table where you might need a foreach loop to enumerate the elements). Yes, it can always be done with CGI.pm, but sometimes it is easier to look at and manipulate with a simple HERE document.

    If you want your HERE documents to be indented you assign the document to a scalar, and then perform a substitution on leading whitespace for each line.

    The Perl Cookbook provides a couple of good solutions. Here's an adaptation of one version.

    ($html = <<HERE_DOC) =~ s/^[^\S\n]//gm; your text goes here. Notice how indentation doesn't matter because the regexp bound to the HERE doc strips leading whitespace. HERE_DOC print $html;

    This works by substituting any amount of whitespace (space, tab, form feed) at the beginning of the line with "nothing" unless the "whitespace" is a newline character (ie, it preserves newlines). Cool huh?

    Enjoy!

    Dave

    "If I had my life to do over again, I'd be a plumber." -- Albert Einstein

Re: Re: print content in cgi and html
by jasonk (Parson) on Sep 26, 2003 at 13:45 UTC

    The line containing _EOF must not contain any white space at the beginning or end or it will not work!

    This isn't true, you can have all the white space you want, as long as it's the same on both ends:

    print <<" _ EOF"; Blah _ EOF

    We're not surrounded, we're in a target-rich environment!
      Hiya jasonk,
      This isn't true, you can have all the white space you want...
      OK, hands up, you're right using the technique you supplied with double quotes, but the example I gave wouldn't have accepted spaces as it wasn't quoted... so it is true.

      Thanks for the poke though. ;)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://294407]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (8)
As of 2024-04-18 12:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found