JupiterCrash has asked for the wisdom of the Perl Monks concerning the following question:

Doesn't Perl have a fancy word for its behavior where you can iterate through an array/list, and modifying the iterator variable modifies the array that you are iterating through? eg.

my @ar = (1, 2, 3); foreach my $a (@ar) { $a = $a + 1; } print join ", ", @ar; # prints 2, 3, 4

I mentioned to someone how Perl has a big fancy word for this behavior and can't remember (or find through google) what it is. Now I want to know.

This is described in http://modernperlbooks.com/books/modern_perl_2014/03-perl-language.html, but it didn't have a fancy name: "The for loop aliases the iterator variable to the values in the iteration such that any modifications to the value of the iterator modifies the iterated value in place."

Am I imagining that there is a fancy word for this behavior?

Thanks!

Replies are listed 'Best First'.
Re: A specific term for a Perlism
by choroba (Cardinal) on Mar 19, 2015 at 16:11 UTC
    What's so unfancy about the words "alias" or "aliasing"?
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      "Alias" and "Aliasing" are descriptive and used perfectly in the Modern Perl example to describe this behavior, but not what I had in mind.

      By "fancy" I supposed I meant a large, and overly-complex-sounding single word that represented this specific behavior of auto-aliasing the iterator within a loop to allow in-place modifications of the thing being iterated on.

      My memory might be completely wrong on this, maybe no such word exists. I sure can't find it. I just have this memory...of something... and it's been driving me crazy for a week. It's been about 10 years since I've logged into PerlMonks and this has driven me to dig up my password and login to ask! :)

        pointer
Re: A specific term for a Perlism (alias types)
by LanX (Saint) on Mar 19, 2015 at 18:28 UTC
    Terminology has to be logical, consistent and (ideally) non redundant, but not "fancy".¹ :)

    Aliasing is the correct term, you might differentiate between

    • loop aliasing ( foreach ) ,
    • argument aliasing ( @_ ) and
    • (explicit) symbol table aliasing (typeglobs)

    , but that's hardly needed.

    See perlglossary#alias for more.

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)

    PS: Je suis Charlie!

    Update

    ¹) Perl might be often magic but it's not a J.K.Rowling novel with "Gryffindor" loops. ;)

Re: A specific term for a Perlism
by jeffa (Bishop) on Mar 19, 2015 at 16:42 UTC

    "...and modifying the iterator variable modifies the array that you are iterating through ..."

    foreach my $a (@ar) ...

    You are not using the "traditional" C style for loop. You are using the Perl style foreach loop. From perlsyn:

    The "foreach" loop iterates over a normal list value and sets the scalar variable VAR to be each element of the list in turn.

    Big difference. You might be comparing apples to oranges here. Consider this code which actually changes the iterator variable instead of the element.

    my @ar = (1, 2, 3); for my $i (0 .. $#ar) { $i = $i + 1; } print join ", ", @ar; # prints 1, 2, 3
    See the difference?

    UPDATE:
    I was attempting to point out that the OP mentioned "modifying the iterator variable" when they are actually modifying the "loop index variable" as perlsyn documents.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
      Consider this code which actually changes the iterator variable instead of the element.

      my @ar = (1, 2, 3);
      for my $i (0 .. $#ar) {
        $i = $i + 1;
      }

      I understand you to be saying that in the quoted code, a change to $i does not affect the loop list element from which the value of $i is drawn, i.e, $i is not an alias.

      But consider the failure of the following:

      c:\@Work\Perl\monks>perl -wMstrict -le "print 'runtime'; for my $i (1, 2, 3) { $i = $i + 1; print $i; } print 'done'; " runtime Modification of a read-only value attempted at -e line 1.

      I think that in both cases $i is being aliased to elements of an anonymous loop list.

      In the first (quoted) case, the  (0 .. $#ar) expression creates a mutable anonymous list, the elements of which can be changed freely.

      In the second case,  (1, 2, 3) creates an immutable loop list. In this case, the statement
          $i = $i + 1;
      is equivalent to the meaningless statement
          1 = 1 + 1;
      for the first element of the loop list because $i is aliased to a literal 1, and the interpreter throws up its hands.


      Give a man a fish:  <%-(-(-(-<

      He didn't say he was using a C-style for loop, did he?

      Might you be trying to answer a question that no one asked?

Re: A specific term for a Perlism
by AppleFritter (Vicar) on Mar 19, 2015 at 17:56 UTC

    "iteration by reference"?

    No, I have no idea. I'd simply call it "aliasing" as well.

Re: A specific term for a Perlism
by JupiterCrash (Monk) on Mar 19, 2015 at 18:28 UTC