From: Andrew Ruthven Date: Mon, 19 Nov 2007 20:32:24 +0000 (+1300) Subject: Lazy load the XML and MythTV Perl module. X-Git-Tag: 0.5~16 X-Git-Url: http://git.etc.gen.nz/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3fe0e1783b0e1b18306660b9657db0e9a6cbcca4;p=mythtv-status.git Lazy load the XML and MythTV Perl module. --- diff --git a/bin/mythtv-status b/bin/mythtv-status index bf67288..9fa9f24 100755 --- a/bin/mythtv-status +++ b/bin/mythtv-status @@ -174,36 +174,15 @@ my @blocks = ( ); -### -### Fetch the XML status from the backend. -### -my $url = "http://$host:$port/xml"; -my $status = get($url); - -die "Sorry, failed to fetch $url.\n" - unless defined $status; - -### -### Parse the XML -### -my $parser = XML::LibXML->new(); -my $xml = eval { $parser->parse_string( $status ) }; - -### -### Prep the Perl MythTV API if available. -### -my $myth = undef; -eval { require MythTV }; -if (! $@) { - eval { $myth = new MythTV() }; -} - ### ### Set some useful variables, and print the header ### our $today = substr(ParseDate('today'), 0, 8); our $tomorrow = substr(ParseDate('tomorrow'), 0, 8); +# A couple of global variables +my ($xml, $myth); + my $title = "MythTV status for $host"; print "\n$title\n"; print '=' x length($title) . "\n"; @@ -230,9 +209,13 @@ for my $block (@blocks) { my $output = undef; if ($block->{'type'} eq 'xpath') { + $xml ||= load_xml(); + $output = process_xml($block, $xml); } elsif ($block->{'type'} eq 'Perl MythTV API') { + $myth ||= load_perl_api(); + $output = process_perl($block, $myth); } elsif ($block->{'type'} eq 'sub') { @@ -247,6 +230,32 @@ for my $block (@blocks) { exit 0; +# Fetch the XML status from the backend. +sub load_xml { + my $url = "http://$host:$port/xml"; + my $status = get($url); + + die "Sorry, failed to fetch $url.\n" + unless defined $status; + + # Parse the XML + my $parser = XML::LibXML->new(); + return eval { $parser->parse_string( $status ) }; +} + + +# Prep the Perl MythTV API if available. +sub load_perl_api { + my $myth = undef; + + eval { require MythTV }; + if (! $@) { + eval { $myth = new MythTV() }; + } + + return $myth; +} + sub process_xml { my ($block, $xml) = @_;