grocery_stocker said:
If perl can pass stuff by reference, then why do you have to deference
it.
No, you can't. Perl only allows to pass scalars to functions and
those are passed by value. The only thing that's a bit special
in Perl is that if you e.g. have an argument that is an array it
automatically gets converted into a list of scalars and the values
of these scalars get passed to the subroutine. (Other languages
have similar ways that may look strange at a first glance, e.g.
in C and C++ if you use an array as the argument of a function
it automatically gets converted into a pointer to the first
element of the array and this value is passed to the function.)
@tailings = popmany ( \@a, \@b, \@c, \@d );
Sorry, but here you pass scalars to Perl whose values are
references. This is not the same as "passing by reference".
"Passing by reference" means that an argument automatically
gets converted to a reference and that within the subroutine
receiving it in all places the argument is also automatically
dereferenced without you having to spell it out explicitely.
Compare the these two C++ functions: that do the same thing:
void foo_by_val( int* i ) {
*i = 3;
}
void foo_by_ref( int& i ) {
i = 3;
}
The first one you would have to call as
int x;
foo_by_val( &x );
and the second as
foo_by_ref( x );
Perl only has an equivalent for the foo_by_val() function but
not for foo_by_val().
sub popmany {
my $aref;
my @retlist = ();
foreach $aref ( @_ ) {
push @retlist, pop @$aref;
}
return @retlist;
}
Wouldn't it be more accurate to say that perl passes the reference by
value?
It's redundant since Perl only passes by value. A reference is
nothing than a scalar (basically what you would call a pointer
in C or C++) and scalars, the only things you can pass to sub-
routines, are always passed by values.
I just find this sort of odd because in both Java (withc uses
pass the reference by value) and C++ (which supports pass by
reference), I never recall having to dereference a reference like what
I have to in perl.
You have to because Perl (like e.g. C but unlike Java or C++)
simply doesn't do "pass by reference", just "pass by value".
Regards, Jens