Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
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 ( [id://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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://539277]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2024-04-25 08:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found