in reply to Re: Re (tilly) 2: Calling subroutine in a package from other perl sub.
in thread Calling subroutine in a package from other perl sub.

A bit of a different explanation...

Let's pretend we are in Perl4 so there are no \ and -> operators to deal with references so we can use them to talk about how Perl itself deals with aliases.

Then calling test($x,$y,$z) sets @_ to (\$x,\$y,\$z). So if you do $_[0]= "new val" without having modified @_ then you end up doing ${\$x}= "new val" because Perl will see that $_[0] currently contains a reference to (alias of) $x and so modifying $_[0] also modifies $x.

Well if you do my $x1= shift then you are left with @_ containing (\$y,\$z). Do that two more times and @_ is empty. So $_[0]= "new val" ends up putting a value into @_ where no value was before. So there is no reference (alias) for Perl to implicitly dereference so, of course, $x is not changed in that case. So you end up with @_ having ("new val") or ("x2","y2","z2") and @_ just gets thrown away in the end.

Also, when @_ is empty, doing $_[0]= "x" finds $_[0] not being an alias to anything and so creates a new scalar and sets $_[0] to be an alias to it (the only alias at that point).

I hope that helps some.

Update: And calling test(@v) sets @_ to (\$v[0],\$v[1],\$v[2]) (if @v has three elements) (which could be written as \(@v) in Perl5). But doing @_= @v copies the values rather than making aliases.

        - tye (but my friends call me "Tye")

Replies are listed 'Best First'.
Re: (tye)Re: Calling subroutine in a package from other perl sub.
by zzspectrez (Hermit) on Jan 17, 2001 at 11:20 UTC

    Yes. This is a good explanation of the diferences between shift and accessing @_ directly. After I shift, the alias is gone from @_. So its obvious I cant modify the original value.

    However, things dont exactly work like this though. And an alias is not the same as a reference. Correct? Because if test($x,$y,$z) does @_ = (\$x, \$y, \$z) then in then in the code you would have to dereference the variable after you shift them to access them. They would be references not scalar data. my $x = shift @_; print $$x;

    zzSPECTREz

    NOTE: I appreciate your responses. You allways get me thinking.

      Yes, exactly!

      After some thought, I need better (or just more) terminology.

      Let us say that you use the \ and -> operators with "Perl references". Then let us say that Perl itself makes heavy use of a type of reference internally but we'll call these internal references "pointers" since that is probably less confusing for this discussion. Now a "pointer" isn't what a C programmer would call a pointer since there is at least one more layer of abstraction that isn't important for this discussion (so a "pointer" contains a C pointer plus a few other things).

      So a Perl scalar variable is really a name for a "pointer" to a scalar value (called an SV in the source code). So $x points to some SV structure that contains $x's flags saying what type(s) of value(s) $x currently has and contains those values.

      And a Perl array is an array of "pointers" to SVs. So calling test(@v) copies the "pointers" from @v to @_ so we end up with $v[0] and $_[0] pointing to the same SV structure and changing a value via one also changes the value seen via the other. The two are said to be aliases of each other.

      But doing @_= @v causes new SV structs to be allocated for @_ and the values from @v's SVs are copied to @_'s new SVs.

      And so doing "print $x" involves an "automatic dereference" of $x's "pointer".

      Now a "Perl reference" is a(n) SV that contains a "pointer".

      You can watch aliases in work via:

      sub refd { print join( " ", \(@_) ), $/; } my @v= qw( a b c ); print "@v: ", join( " ", \(@v) ), $/; refd( @v ); refd( $v[0] ); refd( @v[1,2,0] ); refd( "$v[1]" );
      You'll note that you get the same references except in the last case.

              - tye (but my friends call me "Tye")