On Tue, 2 Jan 2024 at 16:53, Philippe Bruhat (BooK) <book@cpan.org> wrote:
> On Tue, Jan 02, 2024 at 01:15:17PM +0000, Robert Rothenberg wrote:
> > This bit of horror is still allowed as of Perl 5.38:
> >
> > use v5.38;
> > my $x = [(1..10)];
> > my @x = (11..20);
> > say $x->[0];
> > say $x[0];
> >
> > Perhaps it's time to disallow this?
> >
>
> I'm reminded of https://www.cpan.org/misc/perl.purity.test and the
> questions in the "Variables" section.
>
> Myself, I always giggle a little when I find myself using multiple
> variables with the same name... And because of the Perl purity test, I
> sometimes try to find a use for the hash (in addition to the scalar and
> array), before usually switching to a plural name for the @ and a
> singular one for the $ (to the relief of my coworkers).
>
> (By the way, what is the custom for hashes? Plural or singular?)
>
> At the package level, multiple variables with the same name are possible
> because of typeglobs, and I assume this is where this all comes from.
>
I think it is just a side-effect of the sigils being part of what makes a
variable unique and that we only consider it problematic because the sigil
used to access an element of an array is different from that of its base
name. If the rule was to write @foo[0] to access the zeroth element of @foo
then there would be less of an issue or concern with colliding with $foo.
It is only because we write $foo[0] that there is a possible collision with
$foo->[0]. Personally speaking, my brain parses the two var names very
differently based on the arrow, but i can understand that some folks
dislike it, and newbies especially will find it confusing. I have read and
written code that does stuff like:
our @array;
my $array = \@array;
for various reasons, and I dont think it is really a problem. Especially in
English where plurals sometimes look like singular names.
yves
--
perl -Mre=debug -e "/just|another|perl|hacker/"