in reply to Improving lexical scoping outside subs
1 sub foo 2 { 4a my $self = shift; 4b my () = @_; 3 }
The numbers are the order I type the lines in. (4a is used only if I'm writing a method.) By doing this, I guarantee that I have a my () = @_; line. I suspect that if you had had that line in your code, you would've remembered to put the variable in there.
Also, I try very hard to not use the same name in a sub as I would outside the sub. There's a good reason for it, too. In a sub, you're dealing with an abstract flow, so a name like $filename or $fh is useful. Outside, you're dealing with concrete things, so names like $out_filename or $data_fh are much better. So, what you would have is
my $data_fh = IO::File->new($data_filename) || die "Cannot open '$data_filename' for reading: $!\n"; my $stuff = read_file_with_format($data_fh); ... my $second_fh = IO::File->new($second_filename) || die "Cannot open '$second_filename' for reading: $!\n"; my $other_stuff = read_file_with_format($second_fh); ... sub read_file_with_format { my ($fh) = @_; ... }
------
We are the carpenters and bricklayers of the Information Age.
The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6
Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.
|
|---|