Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^2: Your favorite objects NOT of the hashref phylum (array w/ consts)

by nothingmuch (Priest)
on Mar 26, 2006 at 11:28 UTC ( #539277=note: print w/ replies, xml ) Need Help??


in reply to Re: Your favorite objects NOT of the hashref phylum (array w/ consts)
in thread Your favorite objects NOT of the hashref phylum

For compile time catching of typos in attrs when using hashes as the internal representation use the fields module.

In perl 5.8 it's based on pseudohashes, which are a bit like mapping strings into arrays, except they use hash syntax. But everyone knows that pseudohashes suck ;-)

In perl 5.9 fields uses Hash::Util and locks the hash for runtime checks, and also performs the same compile time checks, so you get the benefits of fields without the insanity of pseudohashes.

The way you use it is:

use fields qw/attr1 attr2/; sub new { # ... my __PACKAGE__ $self = fields::new($pkg); # construct using fields +::new $self->{attr1} = "foo"; # ok $self->{attttr2} = "bar"; # compile time error } sub some_method { my __PACKAGE__ $self = shift; # use __PACKAGE__ or any class name +to tell perl what class $self is $self->{foo}; # because we told it to make compile time checks bas +ed on __PACKAGE__'s fields this will be checked at compile time }
But this still doesn't let you encapsulate the properties in subroutines that you can override.
-nuffin
zz zZ Z Z #!perl


Comment on Re^2: Your favorite objects NOT of the hashref phylum (array w/ consts)
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://539277]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (8)
As of 2014-09-16 10:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (4 votes), past polls