Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Possible bug with array pointer

by jockel (Beadle)
on Jan 10, 2017 at 16:21 UTC ( [id://1179332]=perlquestion: print w/replies, xml ) Need Help??

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

Hi

Either I'm missing something, or I've found a bug..

The following code should in my opinion print out the same in the first and second loop. But somehow the middle loop actually alters the array.

What is happening?

#!/usr/bin/perl my $arrayref = ['A','_B','C']; foreach my $str (@{$arrayref}) { print "BEFORE: str = $str\n"; } foreach my $str (@{$arrayref}) { $str =~ s/^\_//g; } foreach my $str (@{$arrayref}) { print "AFTER: str = $str\n"; }

OUTPUT:

BEFORE: str = A BEFORE: str = _B BEFORE: str = C AFTER: str = A AFTER: str = B AFTER: str = C
-----BEGIN PERL GEEK CODE BLOCK----- Version: 0.01 P++>*$c--->---P6 > R >++++$M+>+++$O+++>+++$MA->+++$E > PU->++BD->-C+>+$D+>+$S->+++X >+WP >+++MO!PP n?CO--PO!>!(!)o?G!A--OLC--OLCC--OLJ--Ee !Ev-Eon-uL++>*uB!uS!uH-uo!w->!m+ ------END PERL GEEK CODE BLOCK------

Replies are listed 'Best First'.
Re: Possible bug with array pointer
by haukex (Archbishop) on Jan 10, 2017 at 16:32 UTC

    Hi jockel,

    That's the intended behavior. See Foreach Loops (emphasis mine):

    If any element of LIST is an lvalue, you can modify it by modifying VAR inside the loop. Conversely, if any element of LIST is NOT an lvalue, any attempt to modify that element will fail. In other words, the foreach loop index variable is an implicit alias for each item in the list that you're looping over.

    Hope this helps,
    -- Hauke D

      Thank you!

      I've been programming perl for almost 20 years and didn't know that!

      I guess I've never actually modified the assigned foreach-variable or never used the array afterwards! What are the odds ;-)

      The lesson learned: You never stop learning!

      -----BEGIN PERL GEEK CODE BLOCK----- Version: 0.01 P++>*$c--->---P6 > R >++++$M+>+++$O+++>+++$MA->+++$E > PU->++BD->-C+>+$D+>+$S->+++X >+WP >+++MO!PP n?CO--PO!>!(!)o?G!A--OLC--OLCC--OLJ--Ee !Ev-Eon-uL++>*uB!uS!uH-uo!w->!m+ ------END PERL GEEK CODE BLOCK------

        Hello jockel,

        A standard idiom (see Re: Containing 'use lib' statements in modules to their own namespace) for removing (“breaking”) the alias is to assign the aliased variable to a lexical variable inside the loop:

        use strict; use warnings; my $arrayref = ['A','_B','C']; for my $str (@$arrayref) { print "BEFORE: str = $str\n"; } for (@$arrayref) { my $str = $_; $str =~ s/^_//; print "INNER: str = $str\n"; } for my $str (@$arrayref) { print "AFTER: str = $str\n"; }

        Output:

        13:30 >perl 1738_SoPW.pl BEFORE: str = A BEFORE: str = _B BEFORE: str = C INNER: str = A INNER: str = B INNER: str = C AFTER: str = A AFTER: str = _B AFTER: str = C 13:30 >

        BTW, an underscore character has no special meaning in a regex, so it doesn’t need to be escaped. Also, in the absence of an /m modifier, ^ can only ever match once, at the beginning of the string, so the /g modifier is redundant.

        Hope that helps,

        Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

        BTW, you do not need the braces to access a simple scalar reference as an array:
        foreach my $str (@$arrayref) { print "BEFORE: str = $str\n"; }
        will work just as well.
        The extra braces are needed when there is a subscript. @{$arrayref->[2]}
Re: Possible bug with array pointer
by SuicideJunkie (Vicar) on Jan 10, 2017 at 16:30 UTC

    What did you expect s/^_//g to do if not remove the underscore?

    I believe the answer is that the loop values are aliased so that things exactly like what you wrote will DWIM

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1179332]
Approved by haukex
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-04-25 20:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found