Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^4: RFC: beginner level script improvement (various comments)

by smls (Friar)
on Oct 31, 2013 at 03:20 UTC ( #1060507=note: print w/ replies, xml ) Need Help??


in reply to Re^3: RFC: beginner level script improvement (various comments)
in thread RFC: beginner level script improvement

"I read the unshift and sub documentation again and I still don't understand your hint."

Ah I meant shift of course, apologies for the confusion.


As far as I understand the @_ is a list that contains all arguments the sub was called with and consequently $_[x] would contain the element of that array with index x.

Correct.


while I think shift only makes sense if something might or might not be present at a specific index after 0
...
I want to continue processing @_ and am unsure about indexes and/or presence

Not sure what you mean by that. shift removes the first value off the list, so all remaining elements of the list move one index to the left (that's why it's called "shift"):

my @array = (2, 4, 6, 8); my $first = shift @array; # $first is 2 # @array is (4, 6, 8) my $second = shift @array; # $second is 4 # @array is (6, 8) # $array[0] is 6 # $array[1] is 8

It works exactly the same when used on @_ (which is the default when shift is called without argument).


my ($bar_ref) = @_; #same as above but I can not process anything else from the list

Not sure what you mean by that, either. Unlike shift, the assignment does not modify @_. Its effect is identical to that of:
my $bar_ref = $_[0];
It's just a more "stream-lined" way of writing it.

When both sides of an assignment are lists, Perl goes from left to right, assigning the first element of the RHS ("right-hand-side") to the first element of the LHS, then the second RHS element to the second LHS element, and so on, for as many elements as there are on the LHS:

my ($first) = (2, 4, 6, 8); my ($first, $second) = (2, 4, 6, 8); # an array on the LHS will "eat up" all remaining elements: my ($first, $second, @remaining) = (2, 4, 6, 8); # undef can be used to "skip" elements you don't want to assign # to anything: my (undef, undef, $third) = (2, 4, 6, 8);


Comment on Re^4: RFC: beginner level script improvement (various comments)
Select or Download Code
Re^5: RFC: beginner level script improvement (various comments)
by georgecarlin (Acolyte) on Nov 04, 2013 at 13:25 UTC
    As I said, I'm a self-taught perl user and best considered beginner level =)

    I tried explaining my reasoning behind how I accessed the args in subs.
    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)
    Below is a case where I have used shift.

    sub set_timeout { my $self = shift; my $to = $self->{timeout}; if (@_){ $self->{timeout} = shift; } return $to; }
    In this case shift makes sense imo, although it could have just as well been written like this.
    sub set_timeout { my $self = $_[0]; my $to = $self->{timeout}; if ($_[1]){ $self->{timeout} = $_[1]; } return $to; }
    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.
    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 lin +e, same result regardless of stuff presence } return 1; }
    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. =)

      Yes, you should of course use whatever you feel most comfortable with.

      FYI, here are two even shorter ways to write that if clause... ;)

      # using a loop: if (@_) { $self->{$_} = shift for qw(stuff1 stuff2 stuff3); } # using a hash slice (probably the fastest alternative): @$self{qw( stuff1 stuff2 stuff3 )} = @_ if @_;

      (The @ sigil in front of the $self hashref is not a typo - see Slices for more info on that.)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1060507]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (11)
As of 2014-10-24 18:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (134 votes), past polls