Quoth Rohit said:
Yupe, thanks everyone. I have replace entirely replaced my code..
OK, 'round we go again...
(please don't be offended by this: the aim
is solely to help you learn better Perl practices).
for ($fileCount = 0; $fileCount < 3; $fileCount++) {
Do you have
use strict;
use warnings;
at the top of your script? If not, you should have, and you should fix
all the error messages you get.
Using a C-style for loop is nearly always a bad idea: Perl has better
ways of iterating over things. In this case, over a list of numbers:
for my $fileCount (0..2) {
The 'my' means the variable doesn't exist outside the for loop, and also
keeps strict happy.
The only reason you need the index at all is because you have two
parallel arrays. This is usually a sign you should have a data
structure, something like
my @symbolFile = (
{
Breakpad => "breakpad #1",
Plist => "plist #1",
},
{
Breakpad => "breakpad #2",
Plist => "plist #2",
},
);
and then you could simply iterate over the array
for my $symbolFile (@symbolFile) {
# use $symbolFile->{Breakpad} and $symbolFile->{Plist}
}
but changing that will require changing the rest of your program, so you
may want to leave it for now.
$symbolFile = $symbolBreakpadFile[$fileCount];
This needs a 'my'.
open SYMBFILEHANDLE, "< $symbolFile" or die $!;
You should use three-arg open, nowadays. You should also keep your
filehandles in variables, and give sensible error messages.
open my $SYMBFILEHANDLE, '<', $symbolFile
or die "can't read '$symbolFile': $!";
@funcRec= <SYMBFILEHANDLE>;
open FHANDLE, "> $SymbolPlistFile[$fileCount]" or die $!;
myMain();
Oh dear, you are passing data to a function by modifying a global value
(in this case, a filehandle). This is a very bad idea: it makes it very
hard to see where values come from when something goes wrong. If you
switch to keeping filehandles in variables you can pass it into the
function as a parameter:
open my $PLIST, '>', $SymbolPlistFile[$fileCount]
or die "can't write '$SymbolPlistFile[$fileCount]': $!";
myMain($PLIST);
and then in myMain:
sub myMain {
my ($PLIST) = @_;
# write to $PLIST instead of FHANDLE
}
Also note that one of the advantages of scoping your variables with my
is you no longer need such long names: you should be able to easily see
the piece of code a variable name is valid over, and see that it is
unique.
If you use lexical filehandles (filehandles in variables) they close
automatically when the variable goes out of scope. This is a saving if
you're not going to check the return value of close anyway (not
generally necessary on files you are reading, but worth doing on files
you are writing, as an error can be delayed until then).
Thanks again for your time and pointing out my mistakes, which I could
have never identified.
That's quite alright
.
Ben