Hawkes, Mick I <mailto:[EMAIL PROTECTED]> wrote:
: But back to the original question. I need to put a lot of
: re-occurring code into subroutines (as you do) however I am
: hampered to a problem with passing shift I think. This is a
: very simplified test code where I have tried passing shift
: as an argument and also as a call (see commented out bits)
: both give the same error:
One problem I think you are having is defining what 'shift' does.
'shift' is short for 'shift @_'. You are not passing 'shift' around,
you are passing arguments around in @_.
When a subroutine is called, the arguments passed to the sub are
placed in @_ by perl. When an object method is called @_ is prepended
with the calling object. For example, if $self is an object and
method() is a valid method of that object, these two statements are
the same.
$self->method( 'foo' ); # $self is prepended to @_
method( $self, 'foo' ); # The previous call is preferred
Inside the method() subroutine we write this.
sub method {
my $self = shift;
.
.
.
}
Which is short for this.
sub method {
my $self = shift @_;
.
.
.
}
Since @_ is equal to '( $self, 'foo' )', this is also equivalent
(except 'shift' doesn't work on lists).
sub method {
my $self = shift ( $self, 'foo' );
.
.
.
}
We are not passing around 'shift', we are passing around @_.
'shift' shifts the first element off @_. Blow, GetOfficers() does
not pass anything into the GetOfficers() method.
my $loop = GetOfficers();
sub GetOfficers {
my $self = shift; # $self is not defined because @_ is empty.
}
---------
my $loop = $self->GetOfficers();
sub GetOfficers {
my $self = shift; # $self is defined because @_ has $self in it.
}
HTH,
Charles K. Clarkson
--
Mobile Homes Specialist
254 968-8328
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>