Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

include statement in perl?

by stonecolddevin (Parson)
on May 31, 2003 at 02:39 UTC ( #262014=perlquestion: print w/replies, xml ) Need Help??

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

Hi everyone

I am doing some "massive" work with CGI.pm, and I needed to know if there is a statement that works the same as the SSI directive <!--#include virtual="files/my.html"-->?

Let me know if what I am asking is understandable.

hoss

Replies are listed 'Best First'.
Re: include statement in perl?
by Zaxo (Archbishop) on May 31, 2003 at 02:46 UTC

    This is not specific to CGI.pm

    sub include { my $file = shift; open my $fh, '<', $ENV{'DOCUMENT_ROOT'}.$file or return; while (<$fh>) { print } close $fh or return; return 1; }
    Taint mode will require that you set $ENV{'DOCUMENT_ROOT'} for yourself.

    After Compline,
    Zaxo

      It should be noted that as DOCUMENT_ROOT is set by the webserver and not subject to user input, it's safe to trust.

      Makeshifts last the longest.

        'safe to trust' ne 'untainted' :)

        Greetz
        Beatnik
        ... I'm belgian but I don't play one on TV.
      Ohhh, so you basically just open up the file, read it, and then print it out? That sounds easy enough.
Re: include statement in perl?
by BrowserUk (Pope) on May 31, 2003 at 02:48 UTC

    Update Corrected @ARGV to *ARGV to correct the error noted by Aristotle++ below.

    use strict; use warnings; use CGI; .... print do{ local (*ARGV,$/), @ARGV='files/my.html'; <> }; # Hic! ....

    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller


      Just be careful that the ARGV filehandle is not currently open on another file, otherwise it won't quite do what you expected.

      Update: localizing the entire *ARGV glob works, but doesn't work in less than recent Perls - it breaks on less than 5.8, IIRC 5.6 - see (tye)Re: Cheap idioms.

      Makeshifts last the longest.

        If it's good enough for tye, it's good enough for me:)


        Examine what is said, not who speaks.
        "Efficiency is intelligent laziness." -David Dunham
        "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller


Re: include statement in perl?
by BUU (Prior) on May 31, 2003 at 04:59 UTC
    If you're doing "massive" work with CGI, it sounds like your outputting lots and lots of html. In which case templates are your friends. Personally I prefer this one because I feel it does the best job at seperating the data from the layout and all those other fun buzzwords we use for templating. It even has an include statement for you.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2021-11-28 21:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?