Strange behavoiur when passing $1 to a sub

Discussion in 'Perl Misc' started by Heinrich Mislik, Oct 12, 2009.

  1. Hello,

    this program

    #!/usr/bin/perl

    use warnings;
    use strict;

    'a' =~ m/(.)/;
    warn $1;
    unter($1);
    warn $1;

    sub unter
    {
    warn $_[0];
    'b' =~ m/(.)/;
    warn $_[0];
    }
    __END__

    outputs:

    a at ./demo.pl line 7.
    a at ./demo.pl line 13.
    b at ./demo.pl line 15.
    a at ./demo.pl line 9.

    This means, the value of $_[0] changes during evaluation of the regex
    in the subroutine. Even considering $_[0] is an alias to $1, this should
    not happen. This looks like entering the sub happens as follows:

    1.) Make local copy of $1.
    2.) Make $_[0] an alias to $1 (the local copy of $1)

    This should be the other way round:

    1.) Make $_[0] alias to $1 (the outer $1)
    2.) Make local copy of $1.

    perl -v

    This is perl, v5.10.0 built for cygwin-thread-multi-64int
    (with 6 registered patches, see perl -V for more detail)

    Cheers

    Heinrich

    --
    Heinrich Mislik
    Zentraler Informatikdienst der Universitaet Wien
    A-1010 Wien, Universitaetsstrasse 7
    Tel.: (+43 1) 4277-14056, Fax: (+43 1) 4277-9140
    Heinrich Mislik, Oct 12, 2009
    #1
    1. Advertising

  2. Heinrich Mislik

    smallpond Guest

    On Oct 12, 8:31 am, (Heinrich Mislik)
    wrote:
    > Hello,
    >
    > this program
    >
    > #!/usr/bin/perl
    >
    > use warnings;
    > use strict;
    >
    > 'a' =~ m/(.)/;
    > warn $1;
    > unter($1);
    > warn $1;
    >
    > sub unter
    > {
    >     warn $_[0];
    >     'b' =~ m/(.)/;
    >     warn $_[0];}
    >
    > __END__
    >
    > outputs:
    >
    > a at ./demo.pl line 7.
    > a at ./demo.pl line 13.
    > b at ./demo.pl line 15.
    > a at ./demo.pl line 9.
    >
    > This means, the value of $_[0] changes during evaluation of the regex
    > in the subroutine. Even considering $_[0] is an alias to $1, this should
    > not happen. This looks like entering the sub happens as follows:
    >
    > 1.) Make local copy of $1.
    > 2.) Make $_[0] an alias to $1 (the local copy of $1)
    >
    > This should be the other way round:
    >
    > 1.) Make $_[0] alias to $1 (the outer $1)
    > 2.) Make local copy of $1.
    >
    > perl -v
    >
    > This is perl, v5.10.0 built for cygwin-thread-multi-64int
    > (with 6 registered patches, see perl -V for more detail)
    >
    > Cheers
    >
    > Heinrich
    >


    perldoc perlvar
    $<digits> "These variables are all read-only and dynamically
    scoped to the current BLOCK."

    So $_[0] is an alias to the $1 in the current block whose
    initial value was your read-only argument. There is no need
    for a sub, you can see it just as well here:


    'a' =~ m/(.)/;
    warn $1;

    {
    warn $1;
    'b' =~ m/(.)/;
    warn $1;
    }

    warn $1;

    a at demo.pl line 7.
    a at demo.pl line 10.
    b at demo.pl line 12.
    a at demo.pl line 15.
    smallpond, Oct 12, 2009
    #2
    1. Advertising

  3. In article <>, says...

    >perldoc perlvar
    >$<digits> "These variables are all read-only and dynamically
    >scoped to the current BLOCK."
    >
    >So $_[0] is an alias to the $1 in the current block


    That's the point: why is $_[0] an alias to the $1 in th current block?
    It really shoud be an alias to the $1 that exists outside of the sub
    and so the value of $_[0] shouldn't change when a regex in the sub is used.

    Cheers

    Heinrich

    --
    Heinrich Mislik
    Zentraler Informatikdienst der Universitaet Wien
    A-1010 Wien, Universitaetsstrasse 7
    Tel.: (+43 1) 4277-14056, Fax: (+43 1) 4277-9140
    Heinrich Mislik, Oct 15, 2009
    #3
  4. On 2009-10-12, smallpond <> wrote:
    > perldoc perlvar
    > $<digits> "These variables are all read-only and dynamically
    > scoped to the current BLOCK."


    As usual with Perl docs, this is complete BS.

    > So $_[0] is an alias to the $1 in the current block


    There is no "$1 in the current block". There is exactly one $1. (Its
    VALUE is RESTORED when a block ends.)

    One should never pass $N variables to subroutines any other way than

    f("$3")

    Hope this helps,
    Ilya
    Ilya Zakharevich, Oct 15, 2009
    #4
  5. In article <>, says...

    >There is no "$1 in the current block". There is exactly one $1. (Its
    >VALUE is RESTORED when a block ends.)
    >
    >One should never pass $N variables to subroutines any other way than
    >
    > f("$3")
    >
    >Hope this helps,


    Thanks, yes, things get clear now. Maybe the text for $<digits> in
    perldoc perlvar should point to "Temporary Values via local()" in
    perldoc perlsub. Thats where "dynamic scoping" is explained in full.

    Cheers Heinrich

    --
    Heinrich Mislik
    Zentraler Informatikdienst der Universitaet Wien
    A-1010 Wien, Universitaetsstrasse 7
    Tel.: (+43 1) 4277-14056, Fax: (+43 1) 4277-9140
    Heinrich Mislik, Oct 16, 2009
    #5
  6. On 2009-10-16, Heinrich Mislik <> wrote:
    > In article <>, says...
    >
    >>There is no "$1 in the current block". There is exactly one $1. (Its
    >>VALUE is RESTORED when a block ends.)
    >>
    >>One should never pass $N variables to subroutines any other way than
    >>
    >> f("$3")
    >>
    >>Hope this helps,

    >
    > Thanks, yes, things get clear now. Maybe the text for $<digits> in
    > perldoc perlvar should point to "Temporary Values via local()" in
    > perldoc perlsub. Thats where "dynamic scoping" is explained in full.


    Will not work too. The semantic of $1 is different from two other
    types of localization: via `local *foo' and via `local $foo'. Both
    latter variants produce "new VARIABLES". $N have "new VALUES" (IIRC).

    IIRC, one of the checks is printing out references to the variables...

    Yours,
    Ilya
    Ilya Zakharevich, Oct 17, 2009
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jim Higson

    How to fix odd IE behavoiur?

    Jim Higson, Sep 13, 2004, in forum: HTML
    Replies:
    1
    Views:
    358
    Jim Higson
    Sep 13, 2004
  2. Gutek
    Replies:
    9
    Views:
    429
    Pete Becker
    Apr 23, 2005
  3. Ben
    Replies:
    2
    Views:
    867
  4. Lawrence D'Oliveiro

    Death To Sub-Sub-Sub-Directories!

    Lawrence D'Oliveiro, May 5, 2011, in forum: Java
    Replies:
    92
    Views:
    1,980
    Lawrence D'Oliveiro
    May 20, 2011
  5. Peter Otten

    Re: odd behavoiur seen

    Peter Otten, Jul 22, 2013, in forum: Python
    Replies:
    0
    Views:
    75
    Peter Otten
    Jul 22, 2013
Loading...

Share This Page