You haven't shown code actually doing something. We can't explain the output you are getting since we don't know how you used the data structure you are showing. I am actually surprised that:
my %h = ( sub { return sprintf ("0x%x", shift) }, sub { return sprintf ("%b", shift) }, sub { return sprintf ("%d", shift) }, );
actually compiles and does not give you at least a warning about a hash with an odd number of elements.

Now, if you are trying to understand shift and @_, I would suggest you make some tries with examples simpler than subroutine references (especially with faulty examples of dispatch tables). Try simpler things such as:

$result = process_args(1, 2, 3, 4); print "Result is $result \n"; sub process_args { my $c = shift: print "Arg array is: @_ \n"; my $d = shift; print "First argument is $c \n"; print "second argument if $d \n"; print "Arg array is now: @_ \n"; return $c + $d; }
Back to your code, it should be corrected at least as follows to make it a proper dispatch table:
my %h = ( hex => sub { return sprintf ("0x%x", shift) }, bin => sub { return sprintf ("%b", shift) }, dec => sub { return sprintf ("%d", shift) }, );
The subrefs in the dispatch table can now be called as follows:
my $hex_val = $h{hex}->(5);
shift will take the argument (5) and pass it to sprintf and the anonymous sub will return the result of sprintf.

Update: fixed a typo in the following line:

print "Arg array is now: @_ \n";
Many thanks to AnomalousMonk for spotting it. I also applied AnomalousMonk's suggestion to add a couple of arguments to the process_args() function call, so that the printing of @_ after the two shift commands would display more clearly what is going on. AnomalousMonk also pointed another error in the code to call the subrefs in the dispatch table. Now fixed.


In reply to Re: My very confusing questions by Laurent_R
in thread My very confusing questions by ROP

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.