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

Hi Monks!

I am trying to save to a file the Rendered Source of this JavaScript you can see on my Perl code, I tried all kids of tricks with no success, if you run this code all you can save to a file is the JavaScript and not what you see on the browser. Does anyone know how I could get the rendered source of this code into a text file?
Here is the code:

#!/usr/bin/perl use strict; use CGI qw(:standard); use CGI::Carp; use CGI::Carp qw(fatalsToBrowser); my $q = new CGI; #generate #. my $freaky_number=int(rand(9999)) + 1; my $check_number=rand(9999); print $q->header(); print "Start Testing<br><br>"; my $k_weather = "../weather_/".$freaky_number.".txt"; my $render_code; print "Path: $k_weather<br><br>"; #=comment open(DDATA_IN, ">$k_weather") || warn "Can't open output file $k_we +ather: $!"; print DDATA_IN "<script type=\"text/javascript\" language=\"JavaScr +ipt\"\n"; print DDATA_IN "src=\"http://www.gmodules.com/ig/ifr?url=http://www +.windfinder.com/widget/windfinder-igoogle-gadget/windfinder_igoogle.x +ml&amp;\n"; print DDATA_IN "up_station=North%20America%20\n"; print DDATA_IN "-%20USA%20\n"; print DDATA_IN "-%20Massachusetts%20\n"; print DDATA_IN "-%20East%20of%20Boston%20Buoy%20\n"; print DDATA_IN "-%20%5Bus189%3Beast_of_boston_buoy%5D&amp;\n"; print DDATA_IN "up_wind_speed=1&amp;\n"; print DDATA_IN "up_wind_direction=1&amp;\n"; print DDATA_IN "up_wind_gusts=1&amp;\n"; print DDATA_IN "up_wave_height=1&amp;\n"; print DDATA_IN "up_wave_direction=1&amp;\n"; print DDATA_IN "up_wave_period=1&amp;\n"; print DDATA_IN "up_air_temperature=1&amp;\n"; print DDATA_IN "up_clouds=1&amp;\n"; print DDATA_IN "up_precipitation=1&amp;\n"; print DDATA_IN "up_speed=mph&amp;\n"; print DDATA_IN "up_temperature=fahrenheit&amp;\n"; print DDATA_IN "up_height=ft&amp;\n"; print DDATA_IN "up_precipitationU=inch%2F3h&amp;\n"; print DDATA_IN "synd=open&amp;\n"; print DDATA_IN "w=500&amp;\n"; print DDATA_IN "h=310&amp;\n"; print DDATA_IN "num_days=2&amp;\n"; print DDATA_IN "num_cols=2&amp;\n"; print DDATA_IN "title=test.com+-+Windfinder&amp;\n"; print DDATA_IN "lang=en&amp;\n"; print DDATA_IN "country=ALL&amp;\n"; print DDATA_IN "border=%23ffffff%7C0px%2C1px+solid+%23004488%7C0px% +2C1px+solid+%23005599%7C0px%2C1px+solid+%230077BB%7C0px%2C1px+solid+% +230088CC&amp;\n"; print DDATA_IN "output=js\">\n"; print DDATA_IN "</script>\n"; close DDATA_IN; open(DATA, "$k_weather") || warn "Can't open output file $k_weather +: $!"; undef $/; # Slurp mode $_ = <DATA>; my @render=$_; $render_code=join("",@render); close DATA; print "$render_code<br>"; my @render="<script type=\"text/javascript\" language=\"JavaScript\ +"> document.write('<xmp>'+((document.all) ? window.document +.body.outerHTML :document.getElementsByTagName('html')[0].innerHTML)+ +'</xmp>'); </script>"; open(DATA_OUT, ">$k_weather") || warn "Can't open output file $k_we +ather: $!"; print DATA_OUT $render_code; close DATA_OUT; print "<br><font color=red>I need this saved::<b>Rendered Code::: $ren +der_code</b></font><br><br>";

Thanks!!!

Replies are listed 'Best First'.
Re: Rendered Source with Perl
by NetWallah (Canon) on Oct 27, 2008 at 20:54 UTC
    It generated a usable page for me (test.com Windfinder), after I replaced all occurrences of
    &amp; with &
    and changed the output file extension from .txt to .htm.

    Note: I tested this from a command line , not under a "real" CGI.

         Have you been high today? I see the nuns are gay! My brother yelled to me...I love you inside Ed - Benny Lava, by Buffalax

      Here, if you run this code in IE or FireFox you will see the code I am trying to save in red.
      #!/usr/bin/perl use strict; use CGI qw(:standard); use CGI::Carp; use CGI::Carp qw(fatalsToBrowser); my $q = new CGI; #generate #. my $freaky_number=int(rand(9999)) + 1; my $check_number=rand(9999); print $q->header(); print "Start Testing<br><br>"; my $k_weather = "../weather/weather_".$freaky_number.".txt"; my $render_code; print "Path: $k_weather<br><br>"; #exit; #=comment open(DDATA_IN, ">$k_weather") || warn "Can't open output file $k_we +ather: $!"; print DDATA_IN "<script type=\"text/javascript\" language=\"JavaScr +ipt\"\n"; print DDATA_IN "src=\"http://www.gmodules.com/ig/ifr?url=http://www +.windfinder.com/widget/windfinder-igoogle-gadget/windfinder_igoogle.x +ml&amp;\n"; print DDATA_IN "up_station=North%20America%20\n"; print DDATA_IN "-%20USA%20\n"; print DDATA_IN "-%20Massachusetts%20\n"; print DDATA_IN "-%20East%20of%20Boston%20Buoy%20\n"; print DDATA_IN "-%20%5Bus189%3Beast_of_boston_buoy%5D&amp;\n"; print DDATA_IN "up_wind_speed=1&amp;\n"; print DDATA_IN "up_wind_direction=1&amp;\n"; print DDATA_IN "up_wind_gusts=1&amp;\n"; print DDATA_IN "up_wave_height=1&amp;\n"; print DDATA_IN "up_wave_direction=1&amp;\n"; print DDATA_IN "up_wave_period=1&amp;\n"; print DDATA_IN "up_air_temperature=1&amp;\n"; print DDATA_IN "up_clouds=1&amp;\n"; print DDATA_IN "up_precipitation=1&amp;\n"; print DDATA_IN "up_speed=mph&amp;\n"; print DDATA_IN "up_temperature=fahrenheit&amp;\n"; print DDATA_IN "up_height=ft&amp;\n"; print DDATA_IN "up_precipitationU=inch%2F3h&amp;\n"; print DDATA_IN "synd=open&amp;\n"; print DDATA_IN "w=500&amp;\n"; print DDATA_IN "h=310&amp;\n"; print DDATA_IN "num_days=2&amp;\n"; print DDATA_IN "num_cols=2&amp;\n"; print DDATA_IN "title=test.com+-+Windfinder&amp;\n"; print DDATA_IN "lang=en&amp;\n"; print DDATA_IN "country=ALL&amp;\n"; print DDATA_IN "border=%23ffffff%7C0px%2C1px+solid+%23004488%7C0px% +2C1px+solid+%23005599%7C0px%2C1px+solid+%230077BB%7C0px%2C1px+solid+% +230088CC&amp;\n"; print DDATA_IN "output=js\">\n"; print DDATA_IN "</script>\n"; close DDATA_IN; open(DATA, "$k_weather") || warn "Can't open output file $k_weather +: $!"; undef $/; # Slurp mode $_ = <DATA>; my @render=$_; $render_code=join("",@render); close DATA; print "$render_code<br>"; my $code="<script type=\"text/javascript\" language=\"JavaScript\"> document.write('<xmp>'+((document.all) ? window.document +.body.outerHTML :document.getElementsByTagName('html')[0].innerHTML)+ +'</xmp>'); </script>"; open(DATA_OUT, ">$k_weather") || warn "Can't open output file $k_wea +ther: $!"; print DATA_OUT $render_code; close DATA_OUT; print "<br><font color=red>I need this saved::<b>Rendered Code::: $cod +e</b></font><br><br>";

      Thanks for looking!
      Were you able to save to a file the text rendered in red?
Re: Rendered Source with Perl
by Anonymous Monk on Oct 28, 2008 at 07:24 UTC
    You should read perlintro, and start over, that code is awful.
    #!/usr/bin/perl -- use strict; use warnings; use CGI(); use CGI::Carp qw(fatalsToBrowser); my $html = <<'__HTML__'; all your html , exactly as you want it __HTML__ my $freaky_number=int(rand(9999)) + 1; my $check_number=rand(9999); my $k_weather = "../weather_/".$freaky_number.".txt"; if( open DDATA, ">$k_weather" ){ print DDATA $html; close DDATA; } else { warn "Can't open output file $k_weather: $!"; } my $q = new CGI; print $q->header(), $html;
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Rendered Source with Perl
by Jenda (Abbot) on Oct 29, 2008 at 00:45 UTC

    OK, so if I understand you correctly you want to generate some JavaScript by your Perl script, then execute the JavaScript and then save somewhere the results, right? Why?!?

    In either case you'll need something that can actually run the JavaScript. And looking at the code you have there, it would have to run the JavaScript in the context of a web page ... that is it would have to be a web browser. You might be able to control some browser to load the stuff your Perl script produced, run the JavaScript and maybe even save the results instead of the original HTML, but WHY?!? What is it you really want to do? You are asking about one step, but most likely the whole path is wrong.