From 3fe0e1783b0e1b18306660b9657db0e9a6cbcca4 Mon Sep 17 00:00:00 2001 From: Andrew Ruthven Date: Tue, 20 Nov 2007 09:32:24 +1300 Subject: [PATCH] Lazy load the XML and MythTV Perl module. --- bin/mythtv-status | 57 +++++++++++++++++++++++++++-------------------- 1 file changed, 33 insertions(+), 24 deletions(-) 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) = @_; -- 2.30.2