LWP::Simple getstore not working inside of hash :(

P

Perl Mon

I have a question. I am a perl newbie, but I like it a lot so far. I
can get this little Perl file to work as a stand alone perl program:

#!/usr/bin/perl
use strict;
use warnings;
use LWP::Simple;

my $TINI_IP = 'http://192.168.1.6';

my $TiniWeather_LocalHTML_location = 'C:/Program
Files/SlimServer/server/TiniWeatherLocalHTML.txt';

LWP::Simple::getstore($TINI_IP,$TiniWeather_LocalHTML_location) or die
"Copy failed: $!";

But, if I try and insert that code under the my functions hash below in
another program, I can't get it to work. I have worked about 8 hours
on this (incuding reading tons off of the Internet). I was wondering
if anyone had any ideas. Any help would be greatly appreciated.

I have the same use statment in the program that this snippet comes
from as above.

************ start code snippet here *********************

my %functions = (
'left' => sub {
Slim::Buttons::Common::popModeRight(shift);
},
'up' => sub {
my $client = shift;

$context{$client} = Slim::Buttons::Common::scroll(
$client,
-1,
$#weather_list + 1,
$context{$client} || 0,
);
$client->update();
},
'down' => sub {
my $client = shift;

$context{$client} = Slim::Buttons::Common::scroll(
$client,
1,
$#weather_list + 1,
$context{$client} || 0,
);
$client->update();
},
'numberScroll' => sub {
my ($client,undef,$digit) = @_;

$context{$client} = Slim::Buttons::Common::numberScroll(
$client,
$digit,
\@weather_list,
1,
sub { $weather_list[shift]->[0] },
);
$client->update();
},
'add' => sub {
#
# refresh the weather data list from the text file
#
# note that the [add] key is labeled as [rec] on the Sony remotes
#
my @field;
my $client = shift;

Slim::Display::Animation::showBriefly(
$client,
getDisplayName() . ' ' . string('PLUGIN_WEATHER_DATA_UPDATING'),
);


my $TINI_IP = 'http://192.168.1.6';
my $TiniWeather_LocalHTML_location = 'C:/Program
Files/SlimServer/server/TiniWeatherLocalHTML.txt';

getstore($TINI_IP,$TiniWeather_LocalHTML_location) or die "Copy
failed: $!";

open (INPUT, "$TiniWeather_LocalHTML_location") || die "cannot open
file";
# opens the file to read, with the filehandle "INPUT"
# Whenever we write "INPUT" in the program it will look at the file
we're pointing to here.

open (OUTPUT, ">$weather_data_location") || die "cannot open file";
# opens the file to write with the filehandle "OUTPUT"
# The single ">" character before the filename means it's open for
writing

while (<INPUT>) {
# executes what's in the curly brackets on each line of "INPUT"
# until it reaches the end of the file

s/<br>/ /g;
s/ <font size="4"><center>Current Weather Conditions |<html>//g;
s/<TD>|<\/TD>|<td>|<\/td>|<\/tr>|<tr>|<th>|<\/th>|&#176|\t|\n//g;
unshift @field, $_;
# breaks up the file into lines
# assigns the lines to the array "field"
# strips out some of the HTML formatting stuff
}

@field = reverse @field;

select OUTPUT;

print "Local Temperature Today's High Today's Low | $field[29]
$field[30] $field[31]\n";
print "Wind Speed & Direction Today's High Today's Peak | $field[46]
$field[47] $field[48]\n";
print "Humididy Dewpoint | $field[63]
$field[80]\n";
print "Barometric Pressure Trend | $field[97]
$field[100]\n";
print "Rainfall YTD | $field[114]\n";
print "Data is updated every 10 minutes. Last updated: | $field[10]";

select STDOUT;

close INPUT;
close OUTPUT;
# close the two files that were used

read_textfile();
},
);

********************* End code snippet here ********************
Thanks :)
 
J

Jim Keenan

Perl said:
I have a question. I am a perl newbie, but I like it a lot so far. I
can get this little Perl file to work as a stand alone perl program:

#!/usr/bin/perl
use strict;
use warnings;

Good so far!
use LWP::Simple;

my $TINI_IP = 'http://192.168.1.6';

my $TiniWeather_LocalHTML_location = 'C:/Program
Files/SlimServer/server/TiniWeatherLocalHTML.txt';

errr ... Couldn't you shorten the name of that variable? Such a long
name courts typing errors.
LWP::Simple::getstore($TINI_IP,$TiniWeather_LocalHTML_location) or die
"Copy failed: $!";

But, if I try and insert that code under the my functions hash below in
another program, I can't get it to work. I have worked about 8 hours
on this (incuding reading tons off of the Internet). I was wondering
if anyone had any ideas. Any help would be greatly appreciated.

I have the same use statment in the program that this snippet comes
from as above.

************ start code snippet here *********************

my %functions = (
'left' => sub {
Slim::Buttons::Common::popModeRight(shift);
},
'up' => sub {

Are we supposed to know what this Slim::Buttons ... module is? It's
clearly not on CPAN.

Jim Keenan
 
P

Perl Mon

I finally got this to work by using this code:

sub UpdateWeather {
my $LocalCopy = 'C:\\Program
Files\\SlimServer\\server\\TiniWeatherLocalHTML.txt';
my $TINI_IP = get('http://192.168.1.6');
open (OUTPUT, ">$LocalCopy");
select OUTPUT;
print "$TINI_IP";
select STDOUT;
close OUTPUT;
}

It looks as if something may be wrong with the code that SlimServer
(www.slimdevices.com) has in it (it is written in Perl and installs a
lot of modules so users don't have to install Perl separately. I have
notified them of the situation. Thanks for your help.
 
T

Tad McClellan

my $LocalCopy = 'C:\\Program
Files\\SlimServer\\server\\TiniWeatherLocalHTML.txt';


This will work fine too you know:

my $LocalCopy = 'C:/Program
Files/SlimServer/server/TiniWeatherLocalHTML.txt';

No need for silly slashes.

open (OUTPUT, ">$LocalCopy");


You should always, yes *always*, check the return value from open():

open (OUTPUT, ">$LocalCopy") or die "could not open '$LocalCopy' $!";

select OUTPUT;
print "$TINI_IP";


Why bother with the select()?

Why bother with the useless use of quotes?


print OUTPUT $TINI_IP;

will do exactly the same thing as those 2 lines,
with 8 less characters to type and read.
 
P

Perl Mon

Thanks for the tips. I still have tons to learn, as if that isn't
obvious. I will revise my code. I really appreciate you taking the
time to help :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top