Sometimes I'll write a function that accepts either a hash, hashref or single scalar. The hash and hashref are for the long-form  foo( name => 'bar', hat => 'funny') and  foo( { name => 'baz', hat => 'hard' } ) forms and the single scalar is for the short lazy form  foo('antelope').

I check if the length of @_ is greater than 1, I treat it as a hash. If the length of @_ is 1, I use ref to determine if it's a hash ref or a scalar.

Notice I'm not trying to handle arrays and array refs. While you can tell the difference between an array ref and a hash ref, you can't really tell the difference between an array and a hash, unless you have other weird constrains, like your arrays always have an odd number of elements or your hashes allways have the same keys etc... etc...

So, to sum up, unless you know how to tell the difference between a hash and an array in your context, you can't get there from here.

--Pileofrogs


In reply to Re: Function that accepts both hashes/arrays and hashrefs/arreyrefs by pileofrogs
in thread Function that accepts both hashes/arrays and hashrefs/arreyrefs by pshangov

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.