Re^3: Opinion: where Perl5 wasn't attractive for me
by SuicideJunkie (Vicar) on Nov 19, 2014 at 15:23 UTC
|
Yeah, bad idea. Consider the two proposed automatic mys in the following code. It avoids an obvious compile time fault ($line), while causing a silent run time fault ($indux).
use strict;
use warnings;
my $index = 0;
while ($line = <>)
{
frobnicate($indux++, $line);
}
Appended Re: LanX's comment
If only applying the my to assignments initializations, then consider this code with the same issues:
use strict;
use warnings;
my $faultsInARow = 0;
while ($line = <>)
{
if (frobnicate($line))
{
$faultsInaRow = 0; #Initializing a new variable by mistake.
}else{
$faultsInARow ++;
}
}
| [reply] [d/l] [select] |
|
|
The now appended example demonstrates a case where (like mentioned before) block scoping matters.
Python has no blockscoping, such that the second assignment is not considered an declaration cause the variable has already been intialized in the "wider" scope (file or function)
Perl has blockscoping such that a command like nonlocal would be needed far too often ... or more "headache" (sic) would be needed figure out a workaround.
Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
| [reply] [d/l] |
|
|
Considering that this is a question of adding the 'feature' to Perl, the fact that Python doesn't have block scoping doesn't really factor into it.
Also, note that the second assignment is a typo, and IS actually the first (and only) instance of that specific variable name. (I've added a comment to point it out)
| [reply] |
|
|
|
|
|
|
Unless Python also requires that all variable initializations happen before any other statements in a method/function, its lack of block scoping is absolutely irrelevant. It doesn't matter whether the scope of $faultsInaRow is the block, the loop or the method. It should have been caught as a typo, because it was not mean as an initialization. It should have been merely an assignment.
Jenda
Enoch was right!
Enjoy the last years of Rome.
| [reply] |
|
|
This wouldn't compile when applying Python semantics, cause $indux hasn't been initialized.
So $indux=0 or my $indux would be needed beforehand to avoid compilation errors.
There are for sure more cases of uncaught typos in Python than in Perl, which blow at runtime.
But unfortunately this example doesn't demonstrate them well. :)
Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
update
Update outsorced into proper post... :)
| [reply] [d/l] [select] |
Re^3: Opinion: where Perl5 wasn't attractive for me
by ww (Archbishop) on Nov 19, 2014 at 15:32 UTC
|
Bad idea!
Who does the work on an editor (which editor(s)?) to:
- "prepend" the "my" to vars
- to make the programmer's intended choice among "my," "our" and "local"
... and
- to distinguish between a new global $var and one in a sub in which a $var gets localized?
Most of us find it possible to write code without too much agita over Perl's (sometimes idiosyncratic) tools that help us achieve our intent in/with the finished script.
This is a no-whining zone (well, we'd like it to be a no-whining zone!
| [reply] [d/l] |
Re^3: Opinion: where Perl5 wasn't attractive for me
by RonW (Parson) on Nov 19, 2014 at 15:33 UTC
|
my and our declare new variables in Perl much like int, char, float and others declare new variables in C++ (and C). Though Perl's my and our are, technically, scope specifiers while C++'s int, char, float and others are type specifiers. Still, the concept is the same: Declare new variables before you use them.
Special variables neither need to nor should be declared in your Perl code.
| [reply] [d/l] [select] |
Re^3: Opinion: where Perl5 wasn't attractive for me
by LanX (Saint) on Nov 19, 2014 at 16:00 UTC
|
That is what Python2+ does (autodeclaring at first assignment), with side effects on closures, which forced Guido to introduce 'nonlocal' in Python3.
Apart from this does Python not use blockscoping¹ (IIRC) like Perl does, but only function scoping, so far less edge cases to be considered.
I wouldn't say an automine pragma would be necessarily bad, but it would cause considerable headaches to avoid incompatibilities and extra thoughts to catch typos blowing up at runtime.
Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
¹) i.e. block in the sense of e.g. the body of a loop | [reply] [d/l] |
|
|
I think that, in Perl, an automine pragma would be very confusing. Currently, in the absence of strict, Perl auto-declares variables as package (aka our) variables on first use. An automine pragma would turn that on its side by auto-declaring them as local1 (aka my) variables.
Whatever the merits of defaulting variables to local scope might be, doing so in Perl would likely be too confusing.
---
1 as in "local scope", not "localized package variables".
| [reply] [d/l] [select] |
|
|
I don't expect anyone to implement it ...
And it's far more complicated than use vars cause scoping and closures come into play.
At best an IDE might help auto expanding my.
Anyway I'll better stop now feeding this thread =)
Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
| [reply] [d/l] |