Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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

In reply to Re^2: Your favorite objects NOT of the hashref phylum (array w/ consts) by nothingmuch
in thread Your favorite objects NOT of the hashref phylum by blogical

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (4)
As of 2024-04-16 05:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found