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

I know I'm missing something but I can't see why I'm getting the error, Can't call method "proxy" on an undefined value at. Here's my code

open(my $fh, '<', 'cities.txt') or die $!; print $fh; open(my $prx, '<', 'proxies.txt') or die $!; print $prx; mkdir 'Bing', 0755; mkdir 'Bing/1Parsed/', 0755; mkdir 'Bing/1Parsed/Html/', 0755; chomp(my @cities = <$fh>); close($fh); chomp(my @prx = <$prx>); close($prx); open($fh, '<', 'keywords.txt') or die $!; for my $city (@cities) { seek($fh, 0, 0); while (my $keywords = <$fh>) { chomp($keywords); print "$city $keywords\n"; my $xml1 = $link1 . $city ."+". $keywords . $link2 . $city ."+". $ +keywords . $link3; #my $xmla = $link3 . $row . ".com"; #my $xmlx = $link4 . $row; my $filename1 = "Bing/".($city)."_". ($keywords) . ".html"; open my $fh1, ">", $filename1 or die("Could not open file. $!"); #toggle proxy selection my $prx=$_; my $ua->proxy('http', $prx); $ua->agent('Mozilla/8.0'); print "using:"; print "proxy"; print $prx;

The above is a piece of a sub script in a bigger script. Any guidance is greatly appreciated

Replies are listed 'Best First'.
Re: Can't call method "proxy" on an undefined value at
by 1nickt (Canon) on Feb 09, 2016 at 04:14 UTC

    You're trying to call the method proxy() on the object $ua (with $ua->proxy('http', $prx)) before you've initialized the object.

    You've conflated what should be two separate lines:

    my $ua = ... ; # constructor $ua->proxy( ... ) ; # subsequent method call
    See the docs for LWP::UserAgent's proxy attributes.

    Also see the docs for my(), which is used to declare variables.

    my $foo->bar('baz'); # will never compile

    Hope this helps!

    The way forward always starts with a minimal test.

      OK, you've got me on the right track. Working but not pulling the proxy value from the file. Here's the modified code

      open(my $fh, '<', 'cities.txt') or die $!; print $fh; open(my $prx, '<', 'proxies.txt') or die $!; print $prx; mkdir 'Bing', 0755; mkdir 'Bing/1Parsed/', 0755; mkdir 'Bing/1Parsed/Html/', 0755; chomp(my @cities = <$fh>); close($fh); chomp(my @prx = <$prx>); print $prx; close($prx); open($fh, '<', 'keywords.txt') or die $!; for my $city (@cities) { seek($fh, 0, 0); while (my $keywords = <$fh>) { chomp($keywords); print "$city $keywords\n"; my $xml1 = $link1 . $city ."+". $keywords . $link2 . $city ."+". $ +keywords . $link3; #my $xmla = $link3 . $row . ".com"; #my $xmlx = $link4 . $row; my $filename1 = "Bing/".($city)."_". ($keywords) . ".html"; open my $fh1, ">", $filename1 or die("Could not open file. $!"); #toggle proxy selection my $prx=$_; my $ua = LWP::UserAgent->new; $ua->proxy(['http'], $prx); print $prx; $ua->agent('Mozilla/8.0');
        Your code does not compile (Missing brackets).

        You are redefining "my $prx", which was previously a file handle.

        You are assigning an un-initialized $_ to $prx.

        Perhaps you meant it to look something like this:

        my $ua = LWP::UserAgent->new; $ua->agent('Mozilla/8.0'); for my $p (@prx){ print "Current proxy:$p\n"; $ua->proxy(['http'], $p); # Use this UA/Proxy to fetch something.... }

                ...The words of the Prophets are written on the Facebook wall.