[tpm] Pub discussion (1)
Alex Beamish
talexb at gmail.com
Thu Aug 28 20:46:59 PDT 2008
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 );
}
sub add
{
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 );
}
sub add
{
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.
--
Alex Beamish
Toronto, Ontario
aka talexb
More information about the toronto-pm
mailing list