in reply to Re^2: Passing Variables
in thread Passing Variables

Your comments are appropriate, thanks!
My goal was to refactor the OP's code in a straight-forward way, with the intent of being instructive - there are of course imperfections .
When coding this myself, I would be thinking about functions in List::Util and passing references to array instead of an array itself. But that is not what I thought my job here was. My intent was to present easy to understand code. But even so, I figure that the OP will have some questions about it.

Replies are listed 'Best First'.
Re^4: Passing Variables
by GrandFather (Saint) on Feb 28, 2021 at 23:29 UTC

    Example code that skips error handling, unless that is made clear in the accompanying text, is an example of how to do something badly. I'm sure you didn't intend it like that, but error handling and considering edge cases are the key to writing robust reliable code.

    Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
      Well, jdporter says: "handle the arg list being empty. You probably don't want to throw a divide-by-zero exception."

      I am actually fine with that error code!

      I have written Perl code that intercepts various warnings in situations where I can actually fix the problem. I remember one sort routine where a numeric comparison didn't work and the code defaulted back to an alpha comparison. There is no "fix" for a "divide by zero".

      I added comments about this in further code. Grandfather is right about that. Document limitations.
      In the code, allowing Perl to throw a "divide by zero" exception is fine.
      The code is short. Detecting and printing an error that "will happen" is unnecessary, because there is no "fix" for that error.

        Detecting an error condition and printing a usage message however is vastly nicer than expecting the user to dig through the code enough to understand why something blew up, possibly miles away from the actual error.

        The "error" message then does two things, it provides run time documentation for unexpected states, and it subtly provides usage information during maintenance or casual reading of the code. As I said before, error handling and considering edge cases are the key to writing robust reliable code. The "consideration" part needs to be expressed in the code.

        Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond