biohisham has asked for the wisdom of the Perl Monks concerning the following question:

Ok, the only data types in Perl are scalars, arrays and hashes from what I understand, but it is possible to simulate a user-defined data type using an anonymous hash. I don't think I have understood this concept in its entirety...

my resources are so limited. With resources I mean examples, so I am a bit shaky when it comes to holding a grip over simulating user-defined data types.

The code I have is presented underneath but I think I don't fully understand what a user-defined-data type is in terms of (why would it be there if the arrays, hashes, scalars and Perl overall have an intelligence capability) due to the concise nature of the code.

this code creates a data type called record that holds a value, its minimum possible and its maximum possible, I understood the mechanism of its working including the return of the anonymous hash and its reference thereof, but the concept is what I lack, so can a generous wiser monk make me understand why data types present themselves and what causes they do serve and how to get better at coming up with them.

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};
Excellence is an Endeavor of Persistence. Chance Favors a Prepared Mind

Replies are listed 'Best First'.
Re: Simulating user defined data types
by Your Mother (Archbishop) on Aug 07, 2009 at 18:07 UTC

    There isn't an anonymous hash per se and there are no user defined data types. There are references to data holders (scalar, hash, array).

    So your sample code is just a hash reference. Nothing special. What you describe as "data type called record" is a hash reference in a variable named record. You could rewrite the code like this to help clarify-

    my $hash_ref = record(100,1000,10); my %record = %$hash_ref; # De-reference/copy the hash. print $record{max};

    I know this is just a snippet but you are using strict and warnings in all your code, right? :)

      I am a bit confused by the statement “there isn't an anonymous hash per se”. Do you mean that the concept doesn't exist in Perl, or that it isn't evidenced in this code? Both seem false to me. (The docs explicitly call hashrefs created via { ... } anonymous hashes, and there's certainly one of those in the OP's code.) Do you just mean that it's not useful to think of anonymous hashes as different from hashrefs?

        Yeah, you've got it. I was wrong/sloppy.

        My fumbled point was that what is being used is a hash ref and whatever anonymity existed in its creation is irrelevant and only confusing. I'd only want to bring anonymity into the discussion if the code looked like this: record(100,1000,10)->{max}

Re: Simulating user defined data types
by JadeNB (Chaplain) on Aug 07, 2009 at 23:00 UTC
    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>. :-)

      O yeah, I got it, I am thinking of working more with references than to worry about what a user-defined-data type is, I googled it up and guess what, I get this node in the search results returned back to me, the other results out there did not address the issue quite from this perspective so it was perplexing...

      so I am like let it lay low for a little while and I'd come back at it with sharper filed teeth :P..

      with regard to ternary operator, I am cool with it, and to that end I would use it ..thanks for the support everyone and thanks YourMother, you're there as usual :) _________________________________________________________________________
      Excellence is an Endeavor of Persistence. Chance Favors a Prepared Mind