in reply to Re^8: Summing numbers in a file
in thread Summing numbers in a file
My original argument was (intended to be) that global file handles and file-scope lexical file handles are equivalent, and the choice between them is a neutral matter of style.
But they're not equivalent, I've already named two disadvantages (no typo protection and potential clashes with other globals like package names), and I have yet to hear an advantage to bareword filehandles.
Even in the limited case that you name (file-scope lexical file handles), there is yet another difference: if there ever is a clash in names, with lexicals it's incredibly easy to limit the scope of the issue: simply place the statements in a block, and you've limited the scope of the lexical, including a visual scope that allows one to glance at the code and know with certainty that this filehandle is contained within that scope. If one were using bareword filehandles instead, their scope is the entire package, beyond the bounaries of any blocks, so either you'd have to go through the entire package, renaming the filehandles to eliminate any name clashes, or you'd have to use bare blocks and the local *FH "hack", which has its own disadvantages. I see this as yet another disadvantage to bareword filehandles, again with no advantage.
... a file-scope lexical can possibly have wider scope than a global if the same file defines multiple packages ...
That's true, but again easily solved by placing the package in a block, and Perl 5.14 introduced the package NAMESPACE BLOCK syntax to make this look even nicer. Some people even argue against multiple packages in one file. And even with the (admittedly sometimes confusing) issue of lexicals potentially crossing package boundaries, the scope of the lexical is still "visually" limited to the file; I would argue that name clashes because a global of the same name was used in a different file is a much more tricky issue.
My concern is opposing unthinking "never use globals!" policies that then result in using file-scope lexicals in exactly the same way as globals ...
Yes, a valid concern, but like you, I would argue that file-scope lexicals used in exactly the same way as globals are globals too. But that kind of dogmatic policy is not what I meant (or said). Instead, IMHO "globals can and should nearly always be avoided" is intended to cause people to think about what would be a better solution, which is usually a change in design.
To someone who knows what they're doing, it may be acceptable to sometimes use globals, but again, this thread is in the context of giving advice to a beginner.
... (as far as I know) are almost impossible to debug because there is no way to look inside a closure.
I'm far from an expert with the debugger, but that doesn't appear to be correct.
$ cat x.pl use warnings; use strict; sub x { my $y = 123; sub { $y += shift; print "$y\n"; print shift->(), "\n"; } } my $z = x; my $foo = "abc"; $DB::single=1; $z->(111, sub { return $foo."def"; }); $ perl -d x.pl ... main::(x.pl:11): my $z = x; DB<1> c main::(x.pl:16): }); DB<1> s main::CODE(0x543ed97f20a0)(x.pl:6): 6: $y += shift; DB<1> y 0 $y = 123 DB<2> s main::CODE(0x543ed97f20a0)(x.pl:7): 7: print "$y\n"; DB<2> y 0 $y = 234 DB<3> s 234 main::CODE(0x543ed97f20a0)(x.pl:8): 8: print shift->(), "\n"; DB<3> s main::CODE(0x55d4d6a31da8)(x.pl:15): 15: return $foo."def"; DB<3> y 0 $foo = 'abc' $z = CODE(0x543ed97f20a0) -> &main::__ANON__[x.pl:9] in x.pl:5-9
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^10: Summing numbers in a file
by jcb (Parson) on Jun 03, 2020 at 23:56 UTC | |
by haukex (Archbishop) on Jun 04, 2020 at 19:59 UTC | |
by jcb (Parson) on Jun 05, 2020 at 02:34 UTC | |
by haukex (Archbishop) on Jun 06, 2020 at 15:18 UTC |