It is a little easier to modify the arguments independently of the subroutine to be called, at the cost of a more complex data structure and more complex subroutine calls.

One way this could be done, as the subroutine is called, is as follows:

$menu_hash{1}{sub}->({ %{$menu_hash{1}{args}}, # default arguments/values ifc_default => 'overridden', # override default value ifc_color => 'green', # add an argument/value });

There would be no point including the arguments hash from $menu_hash if, as in this case, it included only one element which was being overridden, but it could have other elements, only some of which are overridden in a given call.

The arguments could be set or changed independently of either setting or calling the subroutine to be called.

Whether these are advantages or not will depend on the situation. The name 'ifc_default' suggests that there might be situations where non-default arguments might be passed.

But I think I like the method LanX suggested better than my own, now that I think about it.

update: and then I realized that what your suggested is very similar to what LanX suggested, and additional/override arguments can easily be included in your approach.

"1" => sub { get_ifc_name({'ifc_default' => 'test_me', %{shift} }) },

after which

$menu_hash{1}->({ifc_default => 'overridden', color => 'green'});

So, after all, I like my own suggestion least of all. Thanks for questioning it.


In reply to Re^3: Subroutine references inside of a hash with arguments. by ig
in thread Subroutine references inside of a hash with arguments. by shift9999

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.