Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

If I cannot pass the foo parameter in (to be silently ignored if necessary), that's a sign of bad assumptions.

Your assumption that subroutines silently ignore unknown arguments is the bad assumption. Nothing says this is the rule.

If it's for forwards- or backwards-compatibility then it's not unknown extra arguments. They're there for a reason and the module author is aware of this. Future problems won't arise for you adding those arguments. For my issues with this, see Re^2: Use method/function signatures with Perl.

As for dispatches (like your example with rendering engines), you can take care of this with very little code changes. In your dispatch table (if you have one -- otherwise create one) add which parameters that should be passed (or which shouldn't be passsed, or whichever way suits your problem) and then pass just those parameters. This is trivial to implement for someone like you. For example, a simple draft to illustrate my solution code-wise can look like

sub foo { print "foo: @_\n" } sub bar { print "bar: @_\n" } sub safe_dispatch { my ($dispatch, $name, $params) = @_; my ($code, @keys) = @{$dispatch->{$name}}; my @params = map { $_ => $params->{$_} } grep { exists $params->{$_} } @keys; return $code->(@params); } my %dispatch = ( foo => [ \&foo, qw/ foo common / ], bar => [ \&bar, qw/ bar common / ], ); my %params = ( foo => 1, bar => 1, common => 1, ); safe_dispatch(\%dispatch, $_, \%params) for qw/ foo bar /; __END__ foo: foo 1 common 1 bar: bar 1 common 1
All I've changed from a trivial dispatch is added parameter names in the dispatch table, and use a subroutine instead of $dispatch{$name}->(%params) which typically would be used.

This approach could easily be tweaked for most other situations and similar problems. I admit there might be cases where this could be cumbersome but for typical use I think it's trivial to get around your problem.

A little code with a great benefit, the way I see it. (See Re^2: Use method/function signatures with Perl for the benefit.)

ihb

See perltoc if you don't know which perldoc to read!
Read argumentation in its context!


In reply to Re^4: Use method/function signatures with Perl by ihb
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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others wandering the Monastery: (7)
    As of 2014-12-25 01:03 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (159 votes), past polls