note
georgecarlin
As I said, I'm a self-taught perl user and best considered beginner level =)<br>
<p>
I tried explaining my reasoning behind how I accessed the args in subs.<br>
I've been looking into oop because in this script there's a lot of expect usage and I want to create re-uasable and well structured subs. (currently that'snot the case)<br>
Below is a case where I have used shift.
</p>
<code>
sub set_timeout {
my $self = shift;
my $to = $self->{timeout};
if (@_){
$self->{timeout} = shift;
}
return $to;
}
</code>
In this case shift makes sense imo, although it could have just as well been written like this.<br>
<code>
sub set_timeout {
my $self = $_[0];
my $to = $self->{timeout};
if ($_[1]){
$self->{timeout} = $_[1];
}
return $to;
}
</code>
The reason I didn't use the index based assignment is that shift feels better here and in the documentation I read about oop they exclusively used shift accessing.
However in a different scenario like below I don't think using shift in the if clause makes sense.<br>
<code>
sub set_stuff {
my $self = shift;
if (@_){
$self->{stuff1} = shift; #3 lines for one task
$self->{stuff2} = shift;
$self->{stuff3} = shift;
($self->{stuff1},$self->{stuff2},$self->{stuff3}) = @_; #1 line, same result regardless of stuff presence
}
return 1;
}
</code>
So what I'm saying is I used shift when I felt like it without having a clue whether it's faster or better than index based assignment. =)<br>
1054837
1060507