in perl, get() works with string, but not with a variable containing the string

47 Views Asked by At

Why can't I pass the URL string to "get" in a variable?

#use strict; use warnings;
use LWP::Simple; 
my $hxUrl;
my $Page;

$hxUrl="https://finance.yahoo.com/quote/SPY/history?period1=1653177600&  period2=1685059200&interval=1d&filter=history&frequency=1d&includeAdjustedClose=true";

#using the variable, the page is not read in
$Page = get($hxURL);
print "at 10: length(Page) =", length($Page), "\n";  # length($Page) = null

#using the string directly, the page is accessed properly
$Page = get("https://finance.yahoo.com/quote/SPY/history?period1=1653177600&  period2=1685059200&interval=1d&filter=history&frequency=1d&includeAdjustedClose=true");
print "at 14:length(Page) =", length($Page), "\n";  # length($Page) = 1399524

I have commented out 'use strict' since it won't compile and insists that: Global symbol "$hxURL" requires explicit package name (did you forget to declare "my $hxURL"?) at test.pl line 9.

NO, I didn't forget - I declared it at line 3.

I get the same message if I put the 'my' on line 6

if I put 'my' inside the parens on line 9, it complains: Use of uninitialized value in print at test.pl line 10

There is no way to satisfy it!

I have used this pattern, without 'my' and without strict in dozens of other programs without any problem. I can't understand why I'm having a problem even in this toy example.

Error messages out of LWP might help, but I get none.

1

There are 1 best solutions below

0
Steffen Ullrich On

... did you forget to declare "my $hxURL"?) at test.pl line 9.

NO, I didn't forget - I declared it at line 3.

The error is correct and solving the error also solves the problem you have. You've declared and assigned $hxUrl, but used $hxURL (different spelling!!) instead. Due to this the variable $hxURL used inside get is empty, i.e. you basically called get("") which surely does not achieve the result you expected.