Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^2: How to pass two lists to a sub?

by hmerrill (Friar)
on Nov 05, 2004 at 12:36 UTC ( [id://405478]=note: print w/replies, xml ) Need Help??


in reply to Re: How to pass two lists to a sub?
in thread How to pass two lists to a sub?

A few things - mostly just personal preference.

I prefer to use the "shift" function, like this:

sub process_something { my $s1 = shift; my $s2 = shift; my $arrayref1 = shift; my $arrayref2 = shift; # And instead of creating new hashes from the # references passed in... #my @array1 = @$arrayref1; #my @array2 = @$arrayref2; # You can use the references themselves... # like $arrayref->[i] below... print "Subroutine process_something: \$s1 = $s1\n"; print " \$s2 = $s2\n"; my $count_array1_elements = @$arrayref1; for ($i = 0; $i < $count_array1_elements; $i++) { printf "Array1 Element %d = %s\n", $i, $arrayref->[i]; } } process_something($scalar1, $scalar2, \@array1, # pass a *reference* to the array \@array2); # same
HTH.

Replies are listed 'Best First'.
Re^3: How to pass two lists to a sub?
by Prior Nacre V (Hermit) on Nov 05, 2004 at 13:11 UTC

    Personal coding preferences aside, I did consider something along the lines you have here but decided against it for two reasons.

    1. YAFZ asked specifically for arrays: "All I need is a simple construct in which I'll have two correct @l1 and @l2 at the end."
    2. Some benchmarking I carried a few months ago indicated that evaluating $array[$i] was faster than evaluating $arrayref->[$i]. (As one might expect given the additional dereference operation.)

    (Minor point: You have $arrayref->[i] in a number of places.)

    Regards,

    PN5

      Some benchmarking I carried a few months ago indicated that evaluating $array[$i] was faster than evaluating $arrayref->[$i].

      I am not trying to say your benchmark is wrong, and I don't want to say there are no applications where this would make a difference, but using this as a general rule of thumb does not strike me as a good idea. I would be very surprised if most, or even many, applications would benefit from this kind of nano-optimization.

      More generally, this type of benchmarking is almost always pointless. Using it in isolation to determine the semantics of your code is, frankly, silly. There may be cases where these types of small optimizations add up to real savings, but those are the exception, not the rule. Contorting your code for the exception, without knowing if you are truly hitting that exception, is a waste of time and effort.

Re^3: How to pass two lists to a sub?
by revdiablo (Prior) on Nov 05, 2004 at 19:51 UTC
    I prefer to use the "shift" function

    I think using shift to pull in arguments is occasionally warranted, but it should be avoided most of the time. My reasons are as follows:

    • It's less maintainable. To add a new argument, you either have to copy and paste the shift line, and modify the variable name, or you have to retype essentially the same thing each time. These are both error-prone processes. Error-prone processes should be avoided whenever possible.
    • Each shift modifies the @_ array. My concern with this is not about performance (see a related post), but about semantics. Sometimes it makes sense to modify the array, and in those cases I use shift. But usually you are just trying to copy the arguments into more convenient variable names, and that does not necessitate the destruction of the array.

    Accessing @_ directly does not have these problems. Using the common construction my ($foo, $bar, $baz) = @_; makes it easy to add new arguments, and it does not mangle @_ as a side-effect. I would be interested to hear your reasons in favor of shift-by-default, because, to me, the reasons against it are pretty convincing.

      The main reason I use shift over @_ is to easily set up defaults and quickly get some error checking out of the way.
      Consider:
      my $var1 = shift || 'default'; my $var2 = shift || return 0;
      I respectfully disagree with both points.

      I disagree that shift makes code less maintainable. I agree that you are forced to change the code when a parameter is added, but I'm hard-pressed to come up with any situations where adding parameters _shouldn't_ warrant changing the code. If you are changing what is coming in to a subroutine, are you not changing the function of the subroutine??? Being explicit about exactly what parameters a subroutine takes in is just being clear - in my mind that makes the code _more_ maintainable.

      For what reason would you want to maintain the original @_ array? I can't see any reason that maintaining the original argument array has any advantage over individual arguments created by shift. I don't typically (ever?)change the values of parameters taken into a subroutine anyway (does anyone else?) so maintaining the original parameter list is really no different with shift than it is with assigning variables from @_.

      Feel free to disagree with me - for now I can't see any merit to your exceptions to using "shift'.

        I'm not sure if this discussion still interests you. If not, feel free to ignore this reply. I am not trying to prove you wrong, I just don't want my point of view to be misunderstood.

        I agree that you are forced to change the code when a parameter is added, but I'm hard-pressed to come up with any situations where adding parameters _shouldn't_ warrant changing the code.

        It's not the fact that you have to change the code, it's how you have to change the code. Using a series of shift lines, there is a lot more code overhead than using a list assignment from @_. Overhead often causes problems, especially when modifying existing code. When it can be avoided, I think it should.

        Being explicit about exactly what parameters a subroutine takes in is just being clear

        How is a list of variable names not explicit? Granted, the shift lines are more spread out, and perhaps more visible. But a list of variables in the first few lines of a subroutine is not exactly obfuscation. I think both are pretty clear. The shift lines may be marginally more clear, but not enough that it matters.

        For what reason would you want to maintain the original @_ array? I can't see any reason that maintaining the original argument array has any advantage over individual arguments created by shift.

        As I said before, I tend to look at it from the other point of view: for what reason would you want to destroy the original @_ array? I just get an uneasy feeling clobbering something unnecessarily. So, when there is an alternative way of doing something, and the alternative is better or equal in other ways, then I'll use it.

        Keep in mind, I am not saying shifting parameters is never warranted. I do it frequently. There are many cases where destroying @_ makes perfect sense, and in those cases, I have no problem whatsoever. It's just in the "default case", when there is no reason to destroy it, I don't.

        Feel free to disagree with me - for now I can't see any merit to your exceptions to using "shift'.

        Hopefully you see it as a friendly disagreement. I do not mean to be confrontational, I just do not prefer the shift-by-default method of accessing subroutine arguments. I'm a bit disappointed that you don't see any merit to my reasons. I certainly see merit in yours, I just don't agree that it's the right thing to use most of the time.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2024-06-21 03:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.