
FishMonger
Veteran
Sep 12, 2009, 2:11 PM
Post #2 of 12
(1200 views)
|
|
Re: [savo] Advise for a newbie
[In reply to]
|
Can't Post
|
|
It's good to see that you used the warnings and strict pragmas. It's rare to see a Perl beginner use those pragmas. The rest of the code is on the right track, but could be improved. You need to add proper vertical white space so that your code is easier to read/understand. Your indentation is inconsistent and in some areas wrong. You should keep the line lengths below 80 chars, if possible. The 'or die' statement on the open calls won't work as written when opening a pipe. You need to check the return code, which should be 0 on success. The redundant looping over the data and creating 2 arrays is inefficient and unnecessary. You only need 1 array and it should contain only the desired results. The if/else block used to assign $chan would be better done with the ternary operator. Here's a reworked (untested) version.
#!/usr/bin/perl use warnings; use strict; use 5.010; $|++; my @shows; #List of all abailable shows say "Please enter the show you would like to find.\n", "This can take a while as it will download all listings.\n", "To exit please leave a blank line."; chomp (my $lookup = <>); say "Goodbye" and exit if $lookup =~ /^\s*$/; # Downlad itv program list unless( open(ITV, '-|', "get_iplayer --type='podcast,itv'") == 0 ) { die "fork: $!"; } while (<ITV>) { push @shows, $_ if /$lookup/i; } #Download bbc list unless( open(BBC, '-|', "get_iplayer") ==0 ) { die "fork: $!"; } while (<BBC>){ push @shows, $_ if /$lookup/i; } my $c; #used to count the printed shows say ++$c, ") $_" for @shows; # choose the program you want to download from results of search say "Please select a show to download.\n", "This could take a while as it will download the show selected.\n", "Type exit or leave a blank line to exit"; chomp (my $num = <>); say "Goodbye" and exit if $num =~ /\D/; my @download = split /:/,$shows[$num -1]; my $chan = ($download[1] =~ /itv/i) ? "ITV" : "tv"; unless( open(DOWN, '-|', "get_iplayer --type=$chan --get $download[0]") == 0) { die "fork: $!"; } print <DOWN>;
|