add.pl
$dbentry = "BPM-$info.txt"; #add DB prefix
if (-e $dbentry) { #check if file already exists (it should't)
open(DBENTRY, ">>$dbentry") || die "cannot open file $dbentry\n";
+#open w/ error handling
print DBENTRY "$info\n "; #append Album info (shouldn't have to ap
+pend)
close(DBENTRY); #make sure we're all neat
} else { #we should be creating a new file
open(DBENTRY, ">$dbentry") || die "cannot create file $dbentry\n";
+ #open w/ error handling
print DBENTRY "$info\n "; #add the Album info
close(DBENTRY); #make sure we're all neat
}
You do realize that append mode will create the file if it does not already exist? So that should simply be:
$dbentry = "BPM-$info.txt"; #add DB prefix
open(DBENTRY, ">>$dbentry") || die "cannot open file $dbentry: $!"; #o
+pen w/ error handling
print DBENTRY "$info\n "; #append Album info
close(DBENTRY); #make sure we're all neat
bpm.pl
@files = (@files, $name); #add it!
Using push would be more efficient (and idiomatic.)
push @files, $name; #add it!
You should at least test your code with warnings enabled!
$ perl -Wc bpm.pl
Found = in conditional, should be == at bpm.pl line 33.
That line should be:
if ($flag eq "yes") { #if the flag is on
But you could simplify that a lot and there is no need to store all of the files lines in an array, just store the ones that match /$bpm/:
foreach my $filename ( @files ) { #for each filename in the array
open(PHILE, '<', $filename) || die "cannot open $filename: $!"; #o
+pen file w/ error handling
my @lines = scalar <PHILE>; #get the Artist/Album info
while ( <PHILE> ) {
push @lines, $_ if /$bpm/;
}
if ( @lines > 1 ) {
print @lines;
}
else {
print "sorry, no matching BPMs found."; #deliver the bad news
}
close PHILE; #finish up
}
|