Rajesh Dorairajan wrote:
> I've had this problem for a long time and I am in a sort of a dead-end and
> could use some help. When I make a get request using the LWP UserAgent
> inside a function and I pass a URL to the function as a string I get a
> response. However, when I pass a variable that contains the URL (typically
> a hash value) I get a return code of 501. I'm just not able to figure out
> why. I'm giving the example code of the working code and Non-working code.
> Any help will be deeply appreciated.
>
> Thanks in advance.
>
> -----------------------
> #Working code
>
> #!/usr/bin/perl
> use strict;
> use LWP::UserAgent;
> my $url = "http://rajeshd";
>
> print_page ( $url );
>
> sub print_page{
>
> my($host,$port) = @_;
>
> my $url = $host;
>
> $url .= ":$port" if($port);
>
> my $ua = LWP::UserAgent->new(env_proxy => 0,
> keep_alive => 0,
> timeout => 30);
>
> my $r = $ua->request(HTTP::Request->new(GET => $url));
>
> print $r->content if($r->is_success);
> print $r->code,': ',$r->status_line unless($r->is_success);
> print "\n";
> }
> -----------------------
> #Failing code
>
> #!/usr/bin/perl
> use strict;
> use LWP::UserAgent;
> use XML::Simple;
>
> my $config = XMLin( "C:/config.xml", VarAttr => 'name',
> ContentKey => '-content' );
>
> my $url = $config->={URL_1}; #$config->{URL_1} equals http://rajeshd at
> run-time
>
> print_page ( $url );
>
> sub print_page{
>
> my($host,$port) = @_;
>
> my $url = $host;
>
> $url .= ":$port" if($port);
>
> my $ua = LWP::UserAgent->new(env_proxy => 0,
> keep_alive => 0,
> timeout => 30);
>
> my $r = $ua->request(HTTP::Request->new(GET => $url));
>
> print $r->content if($r->is_success);
> print $r->code,': ',$r->status_line unless($r->is_success);
> print "\n";
> }
http 1.1 define 501 as not implemented. your working version and failing
version is absolutely the same except the source of $url in which the
working version hard codes it while the failing version gets it from the
$config hash reference returned the XMLin function. you extract $url from
$config with:
my $url = $config->={URL_1};
which is a syntax error:
[panda]# perl -e '$c->={hi}'
syntax error at -e line 1, near "->="
Execution of -e aborted due to compilation errors.
[panda]#
david
--
sub'_{print"@_ ";* \ = * __ ,\ & \}
sub'__{print"@_ ";* \ = * ___ ,\ & \}
sub'___{print"@_ ";* \ = * ____ ,\ & \}
sub'____{print"@_,\n"}&{_+Just}(another)->(Perl)->(Hacker)
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>