Cookie Notice

As far as I know, and as far as I remember, nothing in this page does anything with Cookies.


Code I Am Not Proud Of

I have a shell script that uses espeak to tell me the time and temperature, which I crontab to run at the top of every hour. I had been using the Google Weather shadow API, the one that's connected to the doomed iGoogle, but it stopped working on me this week.

This broke my code.

So, I've moved to using XML files from NOAA, which I probably should have used from the first.

sub temperature {
    my $parser = XML::DOM::Parser->new() ;
    my $file = '' ;
    my $doc = $parser->parsefile( $file ) ;
    my $nodes = $doc->getElementsByTagName( "temp_f" ) ;
    my $n = $nodes->getLength ;
    my $temp ;

    for ( my $i = 0 ; $i < $n ; $i++ ) {
        my $node = $nodes->item ($i) ;
        my $str  = $node->toString;
        $temp = sprintf '%d' , ( split /[<>]/ , $str )[2] ;
    return $temp ;

I am fine with most of this. I get a parser. I use it to download the file, and get a document. I get an object full of nodes from that document, and I go through each one. Going through each one is where I hit the goofy.

I can get a node that gives me what I want. What I cannot seem to find is the way to get the value from the object. So, I go to regular expressions. There must be a way to get XML::DOM to do the right thing, but I couldn't find it. From there on, that's OK.

It makes me wish, though, that NOAA exported JSON.


  1. Because I don't have it installed.

  2. You might be looking for something like: $node->getFirstChild->getNodeValue;

    I tend to prefer LibXML for this kind of thing. Here's a rewrite of your function with LibXML:

    sub temperature {
    my $file = '' ;
    my $doc = XML::LibXML->load_xml( location => $file ) ;
    return $doc->findvalue('//temp_f');

  3. XML::LibXML! We Have A Winner!