I'm working on an existing CGI application. This app has defined a helper function for retrieving CGI parameters, which performs parameter specific validation. This function, named gParam, also uses wantarray(), and conditionally returns an array of values, or a scalar of the first requested value.

My problem occurs when this function is used to initialize hash values. For example:

my %hash = ( 'formval1' => gParam('formval1'), 'formval2' => gParam('formval2') );

If there is only one instance each of formval1 and formval2 passed, everything works fine. However, if more than one copy of, say, formval1 is passed in by a mischevious user or a bug on a preceding form, the returned array has multiple elements, and the contents of the hash are shifted and useless.

The problem seems to me to be with the usage of wantarray. Strictly speaking, it's correct in that the function is being called in a list context. Unfortunatelly, in the list context of hash initialization, I don't want it to ever return anything other than exactly one scalar value.

So the question, then, is there any way to tell in gParam when the caller is calling it from a hash initialization context? Or does anyone perhaps have some better, more generic way of handling this?


In reply to Hash equivalent of wantarray? by fs

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.