Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   Strange behavoiur when passing $1 to a sub (http://www.velocityreviews.com/forums/t911569-strange-behavoiur-when-passing-1-to-a-sub.html)

Heinrich Mislik 10-12-2009 12:31 PM

Strange behavoiur when passing $1 to a sub
 
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


smallpond 10-12-2009 01:38 PM

Re: Strange behavoiur when passing $1 to a sub
 
On Oct 12, 8:31*am, Heinrich.Mis...@univie.ac.at (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.


Heinrich Mislik 10-15-2009 11:40 AM

Re: Strange behavoiur when passing $1 to a sub
 
In article <3b76d68a-980c-4889-9c68-6df9fee8b56f@s31g2000yqs.googlegroups.com>, smallpond@juno.com 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


Ilya Zakharevich 10-15-2009 10:46 PM

Re: Strange behavoiur when passing $1 to a sub
 
On 2009-10-12, smallpond <smallpond@juno.com> 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

Heinrich Mislik 10-16-2009 09:58 AM

Re: Strange behavoiur when passing $1 to a sub
 
In article <slrnhdf9it.qpf.nospam-abuse@chorin.math.berkeley.edu>, nospam-abuse@ilyaz.org 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


Ilya Zakharevich 10-17-2009 07:23 AM

Re: Strange behavoiur when passing $1 to a sub
 
On 2009-10-16, Heinrich Mislik <Heinrich.Mislik@univie.ac.at> wrote:
> In article <slrnhdf9it.qpf.nospam-abuse@chorin.math.berkeley.edu>, nospam-abuse@ilyaz.org 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


All times are GMT. The time now is 08:00 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57