Dear fellow brothers of the misty monastery. I'm sitting in my monk's cell and meditating ...

Preface

Perl has - thanks to sigils - the distinction to many other languages ¹ of separated namespaces for functions, arrays, hashes and scalars

This means for instance: if we want to pass an @arr to a function we need to explicitly reference it \@arr and inside the function we always need to explicitly dereference it. ³

But what do we really gain from separated namespaces if it's considered bad style to reuse the same symbol for different types? ( %INC and @INC being an exception to the rule... but still confusing)

Idea

Would it be feasible and if yes to what cost

There is a already module autobox to allow the latter by e-XS-tending the -> operator.

The former is trickier, let me explain

A simple POC implementation with plugable keywords / keyword simple would introduce new keywords like

which automatically do the assignment. ²

# form 1 mine @arr = (1,2,3); # ==> my $arr = \@arr; # form 2 (optional, and more difficult to implement) mine $arr = [1,2,3]; # ==> my \@arr = $arr; # same according to ours and other datatypes

But this is not "aliasing"

consider

mine @arr = (1,2,3); # and later accidentally $arr = [4,5,6]; # ==> \@arr != $arr

this would lead to ugly bugs because both variants would point to different arrays.

The only way to solve this with pure Perl is either to make

the second form has a similar catches:

mine $arr = [1,2,3]; # and later accidentally $arr = [4,5,6]; # ==> \@arr != $arr # even worse $arr = { a=> 1 } # ==> @arr must be detroyed? or what?

Questions:

Cheers Rolf
(addicted to the Perl Programming Language :)
see Wikisyntax for the Monastery

Footnotes

°) Excuse my lack poetic skills to come up with better names, these temporary names are still better than xmy and xour ... Feel free to suggest better...

¹) In other languages, like JS it's perfectly possible to accidentally overwrite a function-ref with a variable. Everything is a scalar, either a primitive type or a object-ref, and de-referencing happens implicitly.

²) Or even better a new lexical pragma to change my and our to act that way

³) or apply the new feature to do an explicit \@arr = $arr

Replies are listed 'Best First'.
Re: Implicit references? module -> feature -> pragma -> "Perl8" ?
by dave_the_m (Monsignor) on May 12, 2026 at 09:52 UTC
    It's not at all clear to me what you're proposing. Is the intent that
    mine @a = (1,2,3);
    is just syntactic sugar for
    my @a; my $a = \@a; @a = (1,2,3);
    or that at compile time,
    mine @a = (1,2,3);
    makes the parser note that $a is special in this scope, and so any occurrences of $a are treated by the parser as if they had been written as (\@a)?

    Dave.

      I'm meditating about both. The general idea is to have an automatic duality of ref-types

      You know the implementation side better than me, that's why I proposed different implementations/semantics.

      The first version is a naive (but easy) implementation with the pitfall that both variables could later diverge.

      To catch this a naive implementation would either need to make the ref $a readonly or use a tie to update \@a (here are dragons)

      (I "think" a POC for testing could be implemented with Keyword::Simple)

      The second version would be ideal, because diverging is ruled out. But I'm not sure if it's even possible to implement this in a performant way and what the repercussions would be.

      An alternative syntax could be

      mine $a = [1,2,3];

      Of course all of this requires testing many use cases with POCs before going live.

      FWIW:
      Regarding the new keywords, in the long run I'd rather prefer a pragma changing my and our and signatures to act this way.

      Legacy code would continue working and new code become much easier.

      I doubt many people reuse the same symbol name in different sigil/types.

      Style guides require arrays and hashes to have self describing names, so it would be obvious that $dictionary is the reference of %dictionary .

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      see Wikisyntax for the Monastery

        I doubt many people reuse the same symbol name in different sigil/types.

        I do this all the time because if I have an array of @stuff and a hash of the same %stuff I think they should both be called stuff. If I need a variable about stuff I'll call that $stuff too.

        it's considered bad style to reuse the same symbol for different types

        Considered by who?

        if we want to pass an @arr to a function we need to explicitly reference it \@arr and inside the function we always need to explicitly dereference it.

        We don't need to do that at all, unless @INC is huge:

        perl -le 'sub INC { print for @_ } INC(@INC)'
Re: Implicit references? module -> feature -> pragma -> "Perl8" ?
by Tux (Canon) on May 13, 2026 at 09:58 UTC
    "Considered bad style" is subjective. I use it *all the time* to indicate that these are coupled very tightly. $id is the current ID from a list of @id that are worked on and the required data for that ID is in $id{$id}. You do not have to agree, but to my brain that works perfect.

    Enjoy, Have FUN! H.Merijn
      TIMTOWTDI this will still work and nobody is forcing you to use a new pragma

      I personally would have preferred° to have

      for my $id (@ids) { my $data = $datas{$id}; # or $data_of{} ¹ ... }

      or are you planing to later write my $id = $id{$id} when accessing the values?

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      see Wikisyntax for the Monastery

      °) Actually depending on context I would prefer more specific names than id and data unless the surrounding is very clear and small. Like $person_id and $address{$person_id}

      When reading a large code base again and again it's nice to immediately distinguish the hashes in different subs.

      ¹) using plural for hashes is actually not PBP (IIRC?), not sure how best to solve it.

      %id_data is a bit redundant, but clearer than %data_of or %data4

        foreach my $id (sort { $id{$a}{m_time} <=> $id{$b}{mtime} } @id) { my $value = $id{$id}{value} or next; # ... }
        Or similar. Note that @id by no means means that it is the complete set of keys %id

        Enjoy, Have FUN! H.Merijn