Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

saving cgi output as html

by martymart (Deacon)
on May 30, 2003 at 12:33 UTC ( [id://261827]=perlquestion: print w/replies, xml ) Need Help??

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

Fellow Monks,

I'm using activestate perl on windows 2000, and using IIS webserver, and cgi.pm. I have a couple of cgi scripts, and they work fine. From my browser I can save these as html files which is nice, because these scripts automate some tedious tasks for me, and saving the output is useful to me. What I was wondering is:
Is is possible to save this output as a html file on the actual web server (saving me the effort of doing it in a browser), and if so, what would be the best way to do this?

Many Thanks,
Martymart

Replies are listed 'Best First'.
Re: saving cgi output as html
by cbro (Pilgrim) on May 30, 2003 at 12:53 UTC
    You could run your CGI script via command line and redirect the output to a file.
    perl myscript.cgi name='user name' dob='September 2003' > need.html
    Where 'name' and 'dob' are the names of the form entities you'd normally fill out/check/click and the strings are the values that would be assigned if you had filled out/checked/clicked said form objects.
    UPDATE:
    Another monk advised me that this would be a little too much to type on the command line if you had several form elements. So, if you do, put them into a file:
    $cat input.txt name='user name' dob='Sep 2003' state='IL' city='Chicago' county='cook'
    So the script could be run as:
    perl myscript.cgi < input.txt > output.html
(jeffa) Re: saving cgi output as html
by jeffa (Bishop) on May 30, 2003 at 13:03 UTC
    I am surprised no one has offered WWW::Mechanize to you yet. Just write a bot that queries the web form and save the resulting $agent->{content} to a file. No need to rewrite the original script. :)

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
      Of course if it's one sole request, you can just stick to LWP::UserAgent or even LWP::Simple. If you can directly run the CGI script, the easier yet route would be to just call it from the command line..

      Makeshifts last the longest.

Re: saving cgi output as html
by arthas (Hermit) on May 30, 2003 at 12:48 UTC
    Well, you can have you script print to a file instead of (or in addition to) the standard output. I.e.:
    $doc = ''; open (MYOUT, ">/mypath/output.html") or die $!; $doc .= "<p>Mydata....</p>"; $doc .= "<p>Myotherdata....</p>"; print $doc; # This goes to the web browser print MYOUT $doc; # This goes to a files close (MYOUT);
    Hope this helps!

    Michele.
Re: saving cgi output as html
by nite_man (Deacon) on May 30, 2003 at 12:53 UTC
    One way do it like this:
    use CGI; my $q = new CGI; open(OUT, '/tmp/out.html') or die "Couldn't open out file: !$"; print OUT $q->header, $q->start_html('hello world'), $q->h1('hello world'), $q->end_html; close OUT or "Couldn't close out file: $!";
    or you can redirect STDOUT in the your file:
    use CGI; my $q = new CGI; open(SAVE, ">&STDOUT"); open(STDOUT, '/tmp/out.html') or die "Couldn't open out file: !$"; print $q->header, $q->start_html('hello world'), $q->h1('hello world'), $q->end_html; open(STDOUT, ">&SAVE");
          
    --------------------------------
    SV* sv_bless(SV* sv, HV* stash);
    
Re: saving cgi output as html
by katgirl (Hermit) on May 30, 2003 at 12:59 UTC
    What arthas said, except you may want to change the name of the file if the data is different each time and you want to keep it. Like so:
    my $filename = time(); my $doc = ''; open (MYOUT, ">/mypath/$filename.html") or die $!; $doc .= "<p>Mydata....</p>"; $doc .= "<p>Myotherdata....</p>"; print $doc; # This goes to the web browser print MYOUT $doc; # This goes to a files close (MYOUT);
    You may also want to add a piece of code to clear out the old files after a specified number of days:
    my(@files, $file); opendir(DATADIR, "datadir"); @files = grep(/[0-9].html/,readdir(DATADIR)); closedir(DATADIR); foreach $file (@files) { if (-M "data/$file" > 30) { unlink("data/$file"); } }
Re: saving cgi output as html
by WhiteBird (Hermit) on May 30, 2003 at 12:46 UTC
    Do you need the HTML output in the saved file, or are you just interested in the data? If you don't need the HTML, then couldn't you just write the information to a text file on your server?
      Actually in this case I do need to save the output as html, as well as have it passed out to a browser. Otherwise yes, it would have been handy to just save the relevant info to a text file. It just seems handier to me to allow certain scripts to be saved as html (on the webserver) because that was what I was doing at the browser end anyway.
      Martymart

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (6)
As of 2024-04-19 08:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found