Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
XP is just a number

Comment on


I'm not sure if this is a useful idea, but have you considered using wantarray or Want to allow dispatch on return context also?

I was in the process of trying to hand code a multi-dispatch method for a function that takes 3 args. Two of which can be either strings, or array refs, or hash refs, or globs. The third is a simple scalar.

The function applies the value or values (which can be a single scalar, or an array of scalars, the keys of a hash, or each of the lines from an open file handle) of the second argument, to each of values (a scalar, and array of scalars, or the keys of a hash, or lines from a file) of the first argument.

I want to use separate subs for each of the variations for two reasons:

  1. Having conditional code in the body of the sub to handle all these variations would horrible complicate the code.
  2. It would also slow the code by a big margin for something that needs to be fast.

I think your module would be of great value here.

However, the way the function would report it's results would (could) also depend on the context in which it is called. In a void context, it would write the results straight to STDOUT, thereby avoiding accumulating large volumes of data in memory and passing it, or a reference to it, back to the caller to write to a file.

If the first parameter is a hash ref, then the results of applying the function to a key, would be stored back into the hash as a value and a total count of results return in a scalar context, or a list of counts for the keys in a list context (maybe?).

If the first parameter is an array, a reference to an array of results would be returned in a scalar context and a list of results in a list context.

For two scalars, an array ref in a scalar context, or the results themselves in a list context.

With Want, other useful variations would be possible (I think, I only just downloaded it a coupleof days ago). I can put code in each of the subs to make this determination, but again, it complicates and slows down the code.

Any chance you might be able to incorporate caller context in the dispatch also?

Examine what is said, not who speaks.

In reply to Re: Use method/function signatures with Perl by BrowserUk
in thread Use method/function signatures with Perl by Ovid

and:  <code> code here </code>

  • Please read these before you post! —
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others chanting in the Monastery: (17)
    As of 2014-03-17 13:52 GMT
    Find Nodes?
      Voting Booth?

      Have you used a cryptocurrency?

      Results (349 votes), past polls