sub record{ ($value, $max, $min)=@_; if($value>=$min && $value<=$max){ return{ #returning an anonymous ha +sh value=>$value, max =>$max, min =>$min, }; }else{ return; } } $myrecord=record(100,1000,10); print $myrecord->{max};
I find this code worrisome because record sometimes returns a hashref and sometimes doesn't, but you don't check which has happened before treating the return value as a hashref. If you haven't use strict'd, you won't hear anything about this; you'll just see corruption later when you try to use $myrecord->{max}. I would strongly recommend
$myrecord = record(100, 1000, 10) or die "That's odd";
(probably with some more informative error message)—as well as use strict'ing, of course.

By the way, the ternary ?: operator allows you to avoid multiple return statements. In particular, if you did always want to return a hashref, then you could write:

return $value >= $min && $value <= $max ? { value => $value, max => $max, min => $min } : {};
Normally I'd recommend this, but, in this case, it would make you lose the ability to test the return value for falsity.

UPDATE: Oops, sorry, <code> ends with </code>, not </c>. :-)


In reply to Re: Simulating user defined data types by JadeNB
in thread Simulating user defined data types by biohisham

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.