in reply to Re: Starting foreach at an arbitrary position
in thread Starting foreach at an arbitrary position

The "local *_ = \ $scalar" trick makes $_ become an alias to $scalar (really the value in $scalar). This is equivalent to the effect that foreach() has, with the exception that this method does not work for lexical variables.

What do you mean when you say "this method does not work for lexical variables?" It will work just fine as a way to iterate over lexically declared arrays as well as global arrays. There is nothing wrong with using a typeglob to alias a lexical, is there?

I don't think you meant to imply otherwise but that statement confuses me.

$ perl -Mstrict -wle 'my @lexical = (1..3); for (my $i=0; $i<@lexical; + $i++){ local *_ = \$lexical[$i]; $_ = "foo"} print "@lexical"' foo foo foo

Update: Upon re-reading that, I'm guessing that you simply mean that you can't use a lexical as the iterator variable.

-sauoq
"My two cents aren't worth a dime.";

Replies are listed 'Best First'.
Re: Re: Re: Starting foreach at an arbitrary position
by theorbtwo (Prior) on Jan 19, 2003 at 04:12 UTC

    I think what he meant was that the LHS can't be a lexical variable.


    Warning: Unless otherwise stated, code is untested. Do not use without understanding. Code is posted in the hopes it is useful, but without warranty. All copyrights are relinquished into the public domain unless otherwise stated. I am not an angel. I am capable of error, and err on a fairly regular basis. If I made a mistake, please let me know (such as by replying to this node).

      Well, once you put that little splat on the left hand side, it won't be a lexical. You may be right. I took the "method" he was explaining as an answer to the original question rather than an explanation of aliasing in general. FWIW, I considered the possibility that you suggest and didn't think it was the case. I won't pretend to know though. :-)

      -sauoq
      "My two cents aren't worth a dime.";