Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

SSI problem

by akm2 (Scribe)
on May 14, 2001 at 16:49 UTC ( [id://80190]=perlquestion: print w/replies, xml ) Need Help??

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

I my day of true foolishness, I wrote the following script to process existing HTML files.

#HTML Document Parsing Library (Version: .011) #By Andrew Kenton Mitchell, SSE (Andrew@AndrewKMitchell.com) #Created: February 26, 2001 #Last Modified: February 27, 2001 15:13 Eastern Time # #This library will read an existing HTML document and replace most sca +lars with the correct values. sub DisplayHTML() { $HTMLFileName = @_[0]; open(HTMLFNH,"<$HTMLFileName") || die "ERROR: Can't open $HTMLFile +Name: $!"; print "Content-type: text/html\n\n"; while ($HTMLLine=<HTMLFNH>) { chomp $HTMLLine; $HTMLLine =~ s/(\$[\w\[\]{}']+)/'"'.$1.'"'/gee; #Allows printi +ng of variable content. print "$HTMLLine\n"; } close(HTMLFNH); } sub DisplayHTMLOnly() { $HTMLFileName = @_[0]; open(HTMLFNH,"<$HTMLFileName") || die "ERROR: Can't open $HTMLFile +Name: $!"; while ($HTMLLine=<HTMLFNH>) { chomp $HTMLLine; $HTMLLine =~ s/(\$[\w\[\]{}']+)/'"'.$1.'"'/gee; #Allows printi +ng of variable content. print "$HTMLLine\n"; } close(HTMLFNH); } return 1;

Now, I been requested to make the script handle an SSI directives similar to:

#exec cmd="perl /usr/local/apache/cgi-bin/toolbar.cgi fastship"

Though I have suggested many times, I be allowed to take the script out, the powers at be refuse.

My fellow monks, I need your help. What is the easiest/best way to accomplish my goal? How can I make my code process an SSI like above?

Thanks,

akm2

Replies are listed 'Best First'.
Re: SSI problem
by Masem (Monsignor) on May 14, 2001 at 17:03 UTC
    I see CGI::SSI over at CPAN, which appears to do exactly what you want; in your case, you'd want to process the CGI to do that variable inclusion that you appear to have, then pass that parsed text to this module to handle the CGI processing.

    Mind you, this is quickly desending into the realm where a templating system would be better, but I doubt your specificiations allow for that at this time.


    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
Re: SSI problem
by Daddio (Chaplain) on May 14, 2001 at 19:31 UTC

    Looking at this code, it already seems that you handle variables. So, let's see if we can get a SSI 'exec' in there, too.

    Here is the code I just tested. It is a little different from the original (I removed the prototyping '()' from the function name, and changed the @_[0] to shift) just to make it work in my test.

    sub DisplayHTML { $HTMLFileName = shift; open(HTMLFNH,"<$HTMLFileName") || die "ERROR: Can't open $HTMLFileName: $!"; print "Content-type: text/html\n\n"; while ($HTMLLine=<HTMLFNH>) { chomp $HTMLLine; $HTMLLine =~ s/(\$[\w\[\]{}']+)/'"'.$1.'"'/gee; if ($HTMLLine =~ /(.*?)<!--\s*#exec cmd=\"?(.*?)\"? -->(.*)/) { $HTMLLine = $1 . `$2` . $3; } print "$HTMLLine\n"; } close(HTMLFNH); }

    It took a little work (on my part - still coming up to speed myself!), but this did test out in my scenario. Let me know if it works for you. I don't know if this is the 'best' way, but it is probably the easiest.

    One thing I think I really have to say is that these SSI 'exec's can open a whole world of trouble, so be careful with the code the SSI is running.

    D a d d i o

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2024-03-28 17:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found