[PATCH] Ensure that for loops always have an array to work on even if there is only one episode.

Mike Crowe mac at mcrowe.com
Thu May 26 13:54:46 EDT 2011


Before applying this fix I quite often saw errors like:

 Not an ARRAY reference at ./get_iplayer line 5611.

with PIDs like b00lv1k1 and b00hg8dq.

Signed-off-by: Mike Crowe <mac at mcrowe.com>
---
 get_iplayer |   12 ++++++++----
 1 files changed, 8 insertions(+), 4 deletions(-)

diff --git a/get_iplayer b/get_iplayer
index b173205..a56d97c 100755
--- a/get_iplayer
+++ b/get_iplayer
@@ -5572,7 +5572,10 @@ sub get_pids_recursive {
 	return @pids;
 }
 
-
+sub ensure_array {
+	my ($in) = @_;
+	return ref $in eq 'ARRAY' ? @$in : $in;
+}
 
 # Gets the episode data from a given episode pid
 sub parse_rdf_episode {
@@ -5611,7 +5614,7 @@ sub parse_rdf_series {
 	my $spid = extract_pid( $rdf->{'po:Series'}->{'rdf:about'} );
 	main::logger "INFO:    Series: '".$rdf->{'po:Series'}->{'dc:title'}."' ($spid)\n";
 	main::logger "INFO:      From Brand PID '".$rdf->{'po:Brand'}->{'rdf:about'}."'\n" if $opt->{debug};
-	for my $episode_element ( @{ $rdf->{'po:Series'}->{'po:episode'} } ) {
+	for my $episode_element (ensure_array($rdf->{'po:Series'}->{'po:episode'})) {
 		my $pid = extract_pid( $episode_element->{'po:Episode'}->{'rdf:about'} );
 		main::logger "INFO:      Episode '".$episode_element->{'po:Episode'}->{'dc:title'}."' ($pid)\n";
 		push @pids, $pid;
@@ -5637,8 +5640,9 @@ sub parse_rdf_brand {
 		main::logger "INFO: With Series pid '".$series_element->{'rdf:resource'}."'\n" if $opt->{debug};
 		push @pids, parse_rdf_series( $ua, $series_element->{'rdf:resource'} );
 	}
-	main::logger "INFO:    Series: <None>\n" if $#{ $rdf->{'po:Brand'}->{'po:episode'} };
-	for my $episode_element ( @{ $rdf->{'po:Brand'}->{'po:episode'} } ) {
+	my @episodes = ensure_array($rdf->{'po:Brand'}->{'po:episode'});
+	main::logger "INFO:    Series: <None>\n" if @episodes;
+	for my $episode_element ( @episodes ) {
 		main::logger "INFO:      Episode pid: ".$episode_element->{'rdf:resource'}."\n" if $opt->{debug};
 		push @pids, extract_pid( $episode_element->{'rdf:resource'} );
 		parse_rdf_episode( $ua, $episode_element->{'rdf:resource'} );
-- 
1.7.2.5




More information about the get_iplayer mailing list