[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] LWP woes



At 12.53 -0500 2000.03.10, Eric Rainey wrote:
>Basically, the first time I make the LWP and HTTP calls, everything
>works just peachy, and it prints out the file I'm trying to get.  Any
>and all subsequent calls however generate an error, which I'm
>assuming is coming from the server 500 - # Illegal content in request
>() but I realize isn't necessarily server generated.

It worked for me.  Every request worked.  Some code notes are below, though.


>#! perl

 #!perl -w

This turns warnings on.  It is Good.

>
>use LWP::UserAgent;
>use HTTP::Request;
>use HTTP::Response;
>#use strict;

No, leave it in!  Please!  :)


>my @theURL = ("http://www.live365.com/scripts/footer-ec.js",
>
>	"http://www.live365.com/scripts/subnav-broadcast-ec.js",
>				"http://www.live365.com/scripts/navbar-ec.js",
>
>	"http://www.live365.com/scripts/iseverything.js",
>				"http://www.live365.com/scripts/genredata.js",
>				"http://www.live365.com/scripts/genre.js",
>
>	"http://www.live365.com/scripts/cookiemonster.js",
>				"http://www.live365.com/scripts/djpanel.js",
>				"http://www.live365.com/scripts/bitrates.js",);
>
>print $#theURL . "\n\n";
>print ($#theURL +1) . "\n\n";
>
>
>
>for ($i = 0;$i != ($#theURL + 1);$i++) {

 for my $url (@theURL) {

You've done too much C programming.  Just putting the array in there
iterates over the loop, putting each value into $url.



>
>  	print ":::the original: $i ::: " . $theURL[$i] . " :::\n";

  	print ":::the original: $i ::: $url :::\n";


>#
># 	my $URLToPass = $theURL[$i];
>#
># 	print ":::theURLToPass::: " . $URLToPass . " :::\n";
>
>	getTheJavascript($theURL[$i]);

	getTheJavascript($url);



>}
>
>
>sub getTheJavascript {
>
>	my $myPal = @_[0];

	my $myPal = $_[0];

@_[0] is deprecated syntax.  For a single value, use $_[0].  You can also do:

	my($myPal) = @_;

This forces @_ into list context (because $myPal is in parens) and so @_
returns the first value in its list.


>
>	print $myPal . "::: \n\n";
>
>	my $ua = new LWP::UserAgent;
>
>	my $request = new HTTP::Request("GET", $myPal);
>
>	my $response = $ua->request($request);
>
>	print "\n\n :::-" . $request->as_string() . "-:::\n\n";
>
>	if ($response->is_success) {
>
>		my $theContent = $response->content;
>		while ($theContent =~ s/\012/\n/) {};

		$theContent =~ s/\015?\012/\n/g;

No need to use a while loop, just use the /g modifier.  Also, some servers
will return CRLF, not simply LF.  It is best to capture all of CRLFs or
LFs, with something like \015?\012.


>		print $theContent;
>
>	} else {
>
>		print $response->error_as_HTML;
>	}
>
>		my $myPal = @_[0];

	$myPal = $_[0];

You do not need my(), since you already put my() in front of this variable
in the same scope above.  Also, again, $_[0] instead of @_[0].

Why do you repeat the code at the top of getTheJavascript again?  It does
the same thing, except with new variable names.  Maybe you did something
different with it in the larger code?


>
>	print $myPal . "::: \n\n";
>
>	my $uaToo = new LWP::UserAgent;
>
>	my $requestToo = new HTTP::Request("GET", $myPal);
>
>	my $responseToo = $ua->request($requestToo);
>
>	print "\n\n :::-" . $requestToo->as_string() . "-:::\n\n";
>
>	if ($responseToo->is_success) {
>
>		my $theContentToo = $responseToo->content;
>		while ($theContentToo =~ s/\012/\n/) {};

		$theContentToo =~ s/\015?\012/\n/g;

>		print $theContentToo;
>
>	} else {
>
>		print $responseToo->error_as_HTML;
>	}
>
># 	open THESCRIPZ, ">>:scripz:thescripts.txt" or die ("Can't
>open scripts!");
>#
># 	print THESCRIPZ $theContent;
>#
># 	close THESCRIPZ;
>
>}
>
># ===== Want to unsubscribe from this list?
># ===== Send mail with body "unsubscribe" to macperl-request@macperl.org


-- 
Chris Nandor          mailto:pudge@pobox.com         http://pudge.net/
%PGPKey = ('B76E72AD', [1024, '0824090B CE73CA10  1FF77F13 8180B6B6'])

# ===== Want to unsubscribe from this list?
# ===== Send mail with body "unsubscribe" to macperl-request@macperl.org