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

Discussion in 'Perl Misc' started by Perl Mon, Jan 4, 2005.

  1. Perl Mon

    Perl Mon Guest

    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 :)
     
    Perl Mon, Jan 4, 2005
    #1
    1. Advertising

  2. Perl Mon

    Jim Keenan Guest

    Perl Mon wrote:
    > 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
     
    Jim Keenan, Jan 4, 2005
    #2
    1. Advertising

  3. Perl Mon

    Perl Mon Guest

    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.
     
    Perl Mon, Jan 8, 2005
    #3
  4. Perl Mon <> wrote:


    > 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.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Jan 9, 2005
    #4
  5. Perl Mon

    Perl Mon Guest

    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 :)
     
    Perl Mon, Jan 11, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rp
    Replies:
    1
    Views:
    537
    red floyd
    Nov 10, 2011
  2. Dan
    Replies:
    3
    Views:
    134
    Brian Wakem
    Aug 19, 2005
  3. Larry Snider

    LWP::Simple Not Working

    Larry Snider, Dec 30, 2003, in forum: Perl Misc
    Replies:
    20
    Views:
    314
    Matt Garrish
    Jan 4, 2004
  4. Replies:
    2
    Views:
    128
    Peter J. Holzer
    Jul 21, 2007
  5. emrefan
    Replies:
    6
    Views:
    347
    Peter J. Holzer
    Jun 16, 2008
Loading...

Share This Page