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

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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.
"But you should never overestimate the ingenuity of the sceptics to come up with a counter-argument." -Myles Allen
"Think for yourself!" - Abigail        "Time is a poor substitute for thought"--theorbtwo         "Efficiency is intelligent laziness." -David Dunham
"Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (2)
As of 2024-04-24 18:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found