# [tpm] Pub discussion (1)

Abram Hindle abram.hindle at softwareprocess.us
Thu Aug 28 21:11:47 PDT 2008

```I think you're totally correct. I think we were in the wrong here.

as this:

sub c {  "   lolcakes    whatwhat " }
c() =~ /([a-z]+)\s*([a-z]+)/g;
print "t1 ".t1(\$1,\$2),\$/;
c() =~ /([a-z]+)\s*([a-z]+)/g;
print "t2 ".t2(\$1,\$2),\$/;
c() =~ /([a-z]+)\s*([a-z]+)/g;
print "t3 ".t3(\$1,\$2),\$/;
c() =~ /([a-z]+)\s*([a-z]+)/g;
print "t4 ".t4(\$1,\$2),\$/;

sub t1 {
(\$a,\$b) = @_;
warn "\$a \$b";
\$b =~ /(a+)/;
return \$a;
}

sub t2 {
\$a = shift;
\$b = shift;
warn "\$a \$b";
\$b =~ /(a+)/;
return \$a;
}

sub t3 {
\$a = \$_[0];
\$b = \$_[1];
warn "\$a \$b";
\$b =~ /(a+)/;
return \$a;
}

sub t4 {
warn "\$_[0] \$_[1]";
\$_[1] =~ /(a+)/;
return \$_[0];
}

produces this:
lolcakes whatwhat at ref.pl line 14.
t1 lolcakes
lolcakes whatwhat at ref.pl line 22.
t2 lolcakes
lolcakes whatwhat at ref.pl line 30.
t3 lolcakes
lolcakes whatwhat at ref.pl line 36.
t4 a

We were warning against using @_ directly
since it is aliased. If you look in the last example
\$_[0] has changed.

And for review form perlsub:

Any arguments passed in show up in the array @_. Therefore, if you
called a function with two arguments, those would be stored in \$_[0]
and \$_[1]. The array @_ is a local array, but its elements are aliases
for the actual scalar parameters. In particular, if an element \$_[0]
is updated, the corresponding argument is updated (or an error occurs
if it is not updatable). If an argument is an array or hash element
which did not exist when the function was called, that element is cre‐
ated only when (and if) it is modified or a reference to it is taken.
(Some earlier versions of Perl created the element whether or not the
element was assigned to.) Assigning to the whole array @_ removes that
aliasing, and does not update any arguments.

So we were wrong about case of using shift.

abram

Alex Beamish wrote:
> Greetings,
>
> After Madison's presentation on Net::DBus tonight, we retreated to
> Burgundy's where a number of interesting technical discussions popped
> up. Among them were discussions as to whether
>
>     my \$self = shift;
>     my \$value = shift;
>
> would mean changes to \$self would be reflected the same way than if
> \$self and \$value were collected in one fell swoop using
>
>     my ( \$self, \$value ) = @_;
>
> My experiments consisted of three files, Obj1.pm:
>
> package Obj1;
>
> sub new
> {
>     my \$class = shift;
>     my \$self = {};
>
>     bless ( \$self, \$class );
>     return ( \$self );
> }
>
> {
>     my \$self = shift;
>     my \$value = shift;
>
>     \$self->{value} = \$value;
> }
>
> sub value
> {
>     my \$self = shift;
>     return ( \$self->{value} );
> }
>
> 1;
>
> And the almost identical Obj2.pm:
>
> package Obj2;
>
> sub new
> {
>     my \$class = shift;
>     my \$self = {};
>
>     bless ( \$self, \$class );
>     return ( \$self );
> }
>
> {
>     my ( \$self, \$value ) = @_;
>
>     \$self->{value} = \$value;
> }
>
> sub value
> {
>     my \$self = shift;
>     return ( \$self->{value} );
> }
>
> 1;
>
> And finally the test script:
>
> #!/usr/bin/perl -w
> #
> #  Test creating Obj1 and Obj2 to see if methods that access arguments
> #  differently affect whether changes propogate back to the caller.
> #  Specifically, does
> #
> #    my \$self = shift;
> #    my ( \$vars ) = @_;
> #
> #  produce a different result than
> #
> #    my ( \$self, \$vars ) = @_;
>
> use Obj1;
> use Obj2;
>
> {
>     my \$obj1 = Obj1->new();
>     \$obj1->add("This is object one");
>     print "Obj1 value is " . \$obj1->value() . "\n";
>
>     my \$obj2 = Obj2->new();
>     \$obj2->add("This is object two");
>     print "Obj2 value is " . \$obj2->value() . "\n";
> }
>
> The resulting output of running test12.pl is
>
> [alex at foo tpm-August2008]\$ perl -w test12.pl
> Obj1 value is This is object one
> Obj2 value is This is object two
>
> This suggests that both methods (two shifts, or a single pull from @_)
> produce the same results.
>
> Have I misunderstood, or coded something incorrectly? I believe that
> both approaches mean pass by reference, meaning that changes are
> reflected.
>

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 252 bytes
Desc: OpenPGP digital signature
URL: <http://mail.pm.org/pipermail/toronto-pm/attachments/20080829/18f54216/attachment-0001.bin>
```