in reply to Access a global variable from a subroutine when used as "for" iterator

Hi vitoco. I'm afraid you have unwittingly sewn great confusion with your question. Usually when we speak of an "iterator" we mean an object (rather than a simple variable) that is used to return some result each time it is used. Very often this is achieved using things called "closures" which is why there has been all this talk of closures in most of the answers you received. Closures are very strange things until you understand them. Let me show you:

use strict; use warnings; use feature 'say'; my $it = MakeCounter(); say $it->() for 1 .. 3; sub MakeCounter { my $count = 0; return sub {return ++$count;}; }

Prints:

1 2 3

Each time MakeCounter is called a new $count variable is created. The anonymous sub that is returned then holds on to and uses that specific instance of $count. The anonymous subroutine "closes" over the $count variable. The thing being closed over need not be a simple scalar variable. Often it will be an array or hash or file handle or database statement handle or even an iterator.

This doesn't help solve your problem, but maybe helps understand where the "closure" comments are coming from.

The more usual term for the variable used in a for loop (both Perl and C types) is "loop variable".

Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond

Replies are listed 'Best First'.
Re^2: Access a global variable from a subroutine when used as "for" iterator
by vitoco (Hermit) on Dec 22, 2023 at 00:24 UTC
    I'm afraid you have unwittingly sewn great confusion with your question. Usually when we speak of an "iterator" we mean an object (rather than a simple variable) that is used to return some result each time it is used. ... The more usual term for the variable used in a for loop (both Perl and C types) is "loop variable".

    I'm sorry about that. English is not my mother language and I use what I've read somewhere else. There are some sources that names the loop variable as the "iterator", like here.

    Thanks about the "closures" explanation. That is something I've never tried...

      I suspect the author of that site may not have English as their first language either. In itself that is not any sort of issue. However the site has very worrying omissions that could cause rather unhappy days. For example the mentions of sigils ('$', '@', '%' ...) are in relation to variables, but that is misleading. Sigils are used to denote the type of a result so when they are used with variables they denote the type the of the variable in an expression. So far so good, but consider:

      my @array = (1 .. 5); my $arrayRef = \@array; say "$arrayRef: @$arrayRef";

      which prints for me (your result will be different):

      ARRAY(0x7bc278): 1 2 3 4 5

      In the say @ returns the contents of the scalar variable arrayRef as an array.

      The much worse omission is that context plays a vital part in understanding what many Perl statements mean. Context seems not to be mentioned at all by the site. Consider:

      my @array = (1, 2, 3, 4, 5); my $count = (1, 2, 3, 4, 5); say "$count: @array";

      which prints (the first three lines are warnings btw):

      Useless use of a constant (2) in void context at D:\Scratch~~\PerlScra +tch\noname.pl line 6. Useless use of a constant (3) in void context at D:\Scratch~~\PerlScra +tch\noname.pl line 6. Useless use of a constant (4) in void context at D:\Scratch~~\PerlScra +tch\noname.pl line 6. 5: 1 2 3 4 5

      The site is a nice light weight introduction to Perl, but it misses important concepts and is somewhat dated. For example it discusses the "given statement" which is now deprecated and will be removed in future versions of Perl. Treat the material on the site with caution!

      Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
        my @array = (1, 2, 3, 4, 5); my $count = (1, 2, 3, 4, 5); say "$count: @array";

        That's very confusing. It breaks two rules:

        1. Don't use lying variable names.
        2. Don't use numbers that correspond to counts.

        The correct example should have been

        my @array = (11, 12, 13, 14, 15); my $last = (11, 12, 13, 14, 15); my $count = @array; say "$last, $count, @array";

        map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
      There are some sources that names the loop variable as the "iterator"

      Indeed and that's probably an unfortunate choice of term in that article also but context is an important thing.

      FWIW, many years ago when I was learning to code we were taught that the variable which increments on each turn through a loop was termed the "control variable". That is the name I still use today. You may see it used for other meanings but always (so I've found) where its value performs some equivalent mechanism. Nobody has complained to me about this name when I've used it. Up to now, anyway. :-)


      🦛

        I think it's most technically known as "the variable i", or more rarely, "the variable j and/or k"?

        (Yes, I have just been porting some Fortran 77 to C then PDL::PP, why do you ask?)