Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 21
  1. #1
    New Coder
    Join Date
    Jul 2009
    Posts
    13
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Can't call method "XX" error, please help noob

    Hello Y'all, yes I'm a noob, and yes I'll probably only post a couple of times. I've been working on this perl script called yweather for almost a week now. It didnt work when I downloaded it, and it still doesnt work. I am NOT a programmer but after a week I've decided to turn to those that are. Here is the script:

    Code:
    #!/usr/bin/perl
    
    #################################################################
    # Yahoo Weather Rss Information Atomizer
    # Version 0.7.1
    # Loud-Soft.com
    # Provided As Is
    #################################################################
    
    use strict;
    use XML::XPath;
    use LWP::Simple;
    use XML::XPath::XMLParser;
    use Getopt::Long;
    use File::Copy;
    
    #################################################################
    # Variables
    #################################################################
    # Constants (Change these to localize)
    my $zipcode = "60642";
    my $unit = "F";
    my $scripthome = "/Library/prlprograms/yweather.pl";
    my $icondir = $scripthome."images/";
    my $datadir = $scripthome."data/";
    my $datafile = $datadir."weather.xml";
    my $imagefile = $icondir."weather.png";
    
    # Constants (Do not change these)
    my $pre="yweather";
    my $uri="http://xml.weather.yahoo.com/ns/rss/1.0";
    my $url="http://xml.weather.yahoo.com/forecastrss?p=$zipcode&u=$unit";
    my %data;
    my $xp;
    
    #################################################################
    # Subroutines
    #################################################################
    # Print usage
    sub usage {
    print "Yahoo Weather Information\n\n";
    print "Usage:\n";
    print " ./yweather.pl -ct Displays current temperature\n\n";
    print "Arguments: \n";
    print " -lc City \n";
    print " -lr Region\n";
    print " -lt Country\n";
    print " -cc Weather Code (used for images)\n";
    print " -ct Current Temperature\n";
    print " -cw Current Weather Description\n";
    print " -cd Current Date\n";
    print " -ah Current Humidity\n";
    print " -av Current Visibilty\n";
    print " -ap Current Barometric Pressure\n";
    print " -ar Change in Barometric Pressure\n";
    print " -sr Time of Sunrise\n";
    print " -ss Time of Sunset\n";
    print " -wc Current Wind Chill\n";
    print " -wd Current Wind Direction\n";
    print " -ws Current Wind Speed\n";
    print " -ut Temperature Unit\n";
    print " -ud Distance Unit\n";
    print " -up Pressure Unit \n";
    print " -us Speed Unit\n";
    print " -fd1 Tomorrow's Day\n";
    print " -fg1 Tomorrow's Date\n";
    print " -fl1 Tomorrow's Low Temp\n";
    print " -fh1 Tomorrow's High Temp\n";
    print " -ft1 Tomorrow's Description\n";
    print " -fc1 Tomorrow's Weather Code\n";
    print " -fd2 Day After Tomorrow's Day\n";
    print " -fg2 Day After Tomorrow's Date\n";
    print " -fl2 Day After Tomorrow's Low Temp\n";
    print " -fh2 Day After Tomorrow's High Temp\n";
    print " -ft2 Day After Tomorrow's Description\n";
    print " -fc2 Day After Tomorrow's Weather Code\n";
    print " --copyimage Copy Appropriate Image to Current Image\n";
    print " --update Update xml source file\n" ;
    print " \n";
    print "All data is returned without units. To get data with units,\n";
    print "use a combination of commands.\n\n";
    print "Example: (Displays Current temperature with unit)\n";
    print " ./yweather.pl -ct && ./yweather.pl -ut\n";
    }
    
    # Print data
    sub args{
    my ($arg) = @_;
    print $data{$arg} . "\n";
    }
    
    # Subroutine to update xml data from yahoo
    sub update_weather {
    LWP::Simple::getstore($url,$datafile);
    }
    
    # Subroutine to download images from yahoo
    sub get_images {
    my $imgurl = "http://l.yimg.com/a/i/us/nws/weather/gr/";
    for (0..47) {
    LWP::Simple::getstore($imgurl.$_."d.png",$icondir.$_."d.png");
    LWP::Simple::getstore($imgurl.$_."n.png",$icondir.$_."n.png");
    }
    File::Copy::copy($icondir."0d.png", $imagefile);
    }
    
    # Parse XML
    sub get_data {
    my($element, $attribute, $index) = @_;
    if ($index){$index=1;}
    
    my $nodeset = $xp->find("//yweather:$element");
    my $node = $nodeset->get_node($index);
    $ret = $node->getAttribute($attribute);
    return $ret;
    }
    
    # Copy correct image to the image define in $imagefile
    sub copy_image {
    my ($second, $minute, $hour, $dayOfMonth, $month,
    $yearOffset, $dayOfWeek, $dayOfYear, $daylightSavings) = localtime();
    my $night = $data{'ss'};
    my $morning = $data{'sr'};
    # my $condition = $data{'cc'};
    my $imagesub;
    if ($hour % 12){
    if(($hour-12) < int(substr($night,0,1))){
    $imagesub = "d";
    }elsif(($minute) < int(substr($night,2,3))){
    $imagesub = "d";
    }else{
    $imagesub = "n";
    }
    } else {
    if(($hour) < int(substr($morning,0,1))){
    $imagesub = "n";
    }elsif(($minute) < int(substr($morning,2,3))){
    $imagesub = "n";
    }else{
    $imagesub = "d";
    }
    }
    File::Copy::copy($icondir.$data{'cc'}.$imagesub.".png", $imagefile)
    or die "File ".$icondir.$data{'cc'}.$imagesub.".png"." cannot be copied. ".$data{'lr'};
    }
    
    #################################################################
    # Check that files exist
    #################################################################
    #ensure directories exist
    unless(-d $datadir){
    mkdir $datadir;
    }
    unless(-d $icondir){
    mkdir $icondir;
    }
    
    # Check if weather.xml exists
    if (!(-e $datafile)){update_weather()}
    $xp = XML::XPath->new(filename => $datafile);
    $xp->set_namespace($pre, $uri);
    
    # Check if images exist
    if (!(-e $icondir."0d.png")){get_images()}
    
    # Check if image exist
    if (!(-e $imagefile)){copy_image()}
    
    
    #################################################################
    # Data Setup
    #################################################################
    # Location Information
    $data{'lc'} = get_data("location","city");
    $data{'lr'} = get_data("location","region");
    $data{'lt'} = get_data("location","country");
    
    # Current Weather Information
    $data{'cc'} = get_data("condition","code");
    $data{'ct'} = get_data("condition","temp");
    $data{'cw'} = get_data("condition","text");
    $data{'cd'} = get_data("condition","date");
    
    # Current Atmosphere Information
    $data{'ah'} = get_data("atmosphere","humidity");
    $data{'av'} = get_data("atmosphere","visibility");
    $data{'ap'} = get_data("atmosphere","pressure");
    $data{'ar'} = get_data("atmosphere","rising");
    
    # Todays Sunrise and sunset
    $data{'sr'} = get_data("astronomy","sunrise");
    $data{'ss'} = get_data("astronomy","sunset");
    
    # Current wind information
    $data{'wc'} = get_data("wind","chill");
    $data{'wd'} = get_data("wind","direction");
    $data{'ws'} = get_data("wind","speed");
    
    # Unit information
    $data{'ut'} = get_data("units","temperature");
    $data{'ud'} = get_data("units","distance");
    $data{'up'} = get_data("units","pressure");
    $data{'us'} = get_data("units","speed");
    
    # Forecast (Tomorrow)
    $data{'fd1'} = get_data("forecast[1]","day");
    $data{'fg1'} = get_data("forecast[1]","date");
    $data{'fl1'} = get_data("forecast[1]","low");
    $data{'fh1'} = get_data("forecast[1]","high");
    $data{'ft1'} = get_data("forecast[1]","text");
    $data{'fc1'} = get_data("forecast[1]","code");
    
    # Forecast (Day after tomorrow)
    $data{'fd2'} = get_data("forecast[2]","day");
    $data{'fg2'} = get_data("forecast[2]","date");
    $data{'fl2'} = get_data("forecast[2]","low");
    $data{'fh2'} = get_data("forecast[2]","high");
    $data{'ft2'} = get_data("forecast[2]","text");
    $data{'fc2'} = get_data("forecast[2]","code");
    
    #################################################################
    # Parse arguments
    #################################################################
    if(($#ARGV + 1) == 1){
    my $arg = substr($ARGV[0],1);
    if ($data{$arg}){
    args($arg);
    } elsif($arg eq "-update"){
    update_weather();
    } elsif($arg eq "-copyimage"){
    copy_image();
    } else {
    usage();
    }
    } else {
    usage();
    }
    I've colored the problem area. I keep getting Can't call method "getAttribute" on an undefined value at ./yweather.pl line 114

    I have no idea how to fix and make this a working code
    PLEASE HELP
    thanks
    Last edited by squeakers; 07-16-2009 at 04:52 PM.

  • #2
    New Coder
    Join Date
    Jul 2009
    Posts
    13
    Thanks
    4
    Thanked 0 Times in 0 Posts
    this is the link to what i'm trying to do.

    http://lifehacker.com/5261449/yweath...r-os-x-desktop

    Maybe this will provide some insight on whats wrong with the code

  • #3
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,871
    Thanks
    2
    Thanked 164 Times in 159 Posts
    The error message is telling you that $node is not defined, which means the line that assigned $node failed.

    i.e., this line failed
    Code:
    my $node = $nodeset->get_node($index);
    I have not used the XML::XPath module, but I suspect the problem might be due to the fact that $index is not being assigned a value. You might need to look at the source code of the module to see if the get_node() method requires a parameter and what does it return if it doesn't receive a parameter.

  • Users who have thanked FishMonger for this post:

    squeakers (07-18-2009)

  • #4
    New Coder
    Join Date
    Jul 2009
    Posts
    13
    Thanks
    4
    Thanked 0 Times in 0 Posts
    For a noob/nonprogrammer can you explain what you just said, or how to check.

    Also the error comes up "getAttribute", but you mentioned it failed 1 lines above that

    Sorry for being a noob

    Thanks for the help

  • #5
    New Coder
    Join Date
    Jul 2009
    Posts
    13
    Thanks
    4
    Thanked 0 Times in 0 Posts
    fixed title, sorry

  • #6
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,871
    Thanks
    2
    Thanked 164 Times in 159 Posts
    This is Object Orientated programming which can be difficult to understand, and even more difficult for me to try and explain.

    $node is an object and "getAttribute" is one of its methods (also known as subroutines). However in this case, the assignment of $node failed so there is no "getAttribute" method/subroutine available.

    Are you ready for some reading?

    perlboot - Beginner's Object-Oriented Tutorial
    http://perldoc.perl.org/perlboot.html

    perltoot - Tom's object-oriented tutorial for perl
    http://perldoc.perl.org/perltoot.html

  • Users who have thanked FishMonger for this post:

    squeakers (07-18-2009)

  • #7
    New Coder
    Join Date
    Jul 2009
    Posts
    13
    Thanks
    4
    Thanked 0 Times in 0 Posts
    i understand and i will read those links.

    Are there any code additions or deletions to make this work (while I'm reading)??

  • #8
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,871
    Thanks
    2
    Thanked 164 Times in 159 Posts
    You could try changing (line 110):
    Code:
    if ($index){$index=1;}
    to:
    Code:
    $index=1;

  • Users who have thanked FishMonger for this post:

    squeakers (07-18-2009)

  • #9
    New Coder
    Join Date
    Jul 2009
    Posts
    13
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by FishMonger View Post
    You could try changing (line 110):
    Code:
    if ($index){$index=1;}
    to:
    Code:
    $index=1;
    attempted this and got and error, program terminated

  • #10
    Senior Coder
    Join Date
    Mar 2006
    Posts
    1,274
    Thanks
    2
    Thanked 39 Times in 38 Posts
    I did not take a long hard look at your code, but it seems to me that $index is never defined and that is the source of your problem. You only ever send two arguments to the get_data subroutine but it expects three. So you check if $index has a true value:

    Code:
    if ($index){$index=1;}
    which it never does so it remains an empty string. Then you use it to create an object:

    Code:
    my $node = $nodeset->get_node($index);
    but evidently because $index has no value, the $node object never gets created, ipso-facto you can't call its method getAttribute.
    But since I don't know which module the getAttribute method is imported from I can't look it up to see what value $index should have when you use it to create your $node object. It seems to me Fishmongers suggestion should have worked if a value of 1 is a legal value to send to the getAttribute method.

    You can try:

    Code:
    if (!$index) {$index = 1;}
    But I doubt it will work since it really does the same thing Fishmonger already suggested.

  • Users who have thanked KevinADC for this post:

    squeakers (07-18-2009)

  • #11
    New Coder
    Join Date
    Jul 2009
    Posts
    13
    Thanks
    4
    Thanked 0 Times in 0 Posts
    sorry double post by accident, cant delete

    see below
    Last edited by squeakers; 07-18-2009 at 05:19 AM.

  • #12
    New Coder
    Join Date
    Jul 2009
    Posts
    13
    Thanks
    4
    Thanked 0 Times in 0 Posts
    I believe we are getting somewhere. I had the same thought, that some piece of information was not being created. Can you take a closer look at the code?

    You also mention: "I don't know which module the getAttribute method is imported from I can't look it up to see what value $index should have when you use it to create your $node object" (sorry for not quoting). I dont know what you mean by module (noob)

    I was trying to find a way to determine what information it is looking for or what information is/isnt being sent. Again, I didnt write the code, and I am not a programmer so this is turning out to be harder than I had anticipated.

    Your help is greatly appreciated, thanks to all that contribute to getting this to work
    Last edited by squeakers; 07-18-2009 at 05:25 AM.

  • #13
    New Coder
    Join Date
    Jul 2009
    Posts
    13
    Thanks
    4
    Thanked 0 Times in 0 Posts
    you are correct, i made the changes suggested in still get the same erro

  • #14
    New Coder
    Join Date
    Jul 2009
    Posts
    13
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Could this be an issue with not having the correct URL to pull the rss feed from?

  • #15
    Senior Coder
    Join Date
    Mar 2006
    Posts
    1,274
    Thanks
    2
    Thanked 39 Times in 38 Posts
    Quote Originally Posted by squeakers View Post
    I believe we are getting somewhere. I had the same thought, that some piece of information was not being created. Can you take a closer look at the code?

    You also mention: "I don't know which module the getAttribute method is imported from I can't look it up to see what value $index should have when you use it to create your $node object" (sorry for not quoting). I dont know what you mean by module (noob)

    I was trying to find a way to determine what information it is looking for or what information is/isnt being sent. Again, I didnt write the code, and I am not a programmer so this is turning out to be harder than I had anticipated.

    Your help is greatly appreciated, thanks to all that contribute to getting this to work
    These are the modules your script is using:

    use XML::XPath;
    use LWP::Simple;
    use XML::XPath::XMLParser;
    use Getopt::Long;
    use File::Copy;


    one of those is importing the getAttribute method, probably one of the XML modules. I have little to no experience with the XML modules so I can't help with their usage. You might want to take this over to www.perlmonks.com and hopefully someone there can help you out,
    Last edited by KevinADC; 07-18-2009 at 08:44 PM.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •