Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^4: use fields; # damnit

by nothingmuch (Priest)
on Aug 21, 2004 at 09:08 UTC ( #384771=note: print w/ replies, xml ) Need Help??


in reply to Re^3: use fields; # damnit
in thread use fields; # damnit

Field checking doesn't have much to do with accessors, or ISA checks. Reading the rest of your thread, I'd like to summarize.

package Dog; use fields qw/intelligence/ sub new { my $pkg = shift; my Dog $self = $pkg->fields::new(); $self->{intelligence} = shift; $self; } sub intelligence { my Dog $self = shift; $self->{intelligence}; }
That's the code you're responsible for. Lets say the accessor for 'intelligence' had a typo, and accessed the field 'inteligence'. Without using fields and typed variable declarations, you would not get any erros.

This won't compile, and is broken:

sub intelligence { my Dog $self = shift; $self->{inteligence}; # typo }

And normally people would write

sub intelligence { my $self = shift; $self->{inteligence}; # typo }
and the code would compile correctly, but is just as broken.

useing fields is meant to help you write your classes a bit cleaner, and is a bit like trying to catch typos in variable names. Just using $foo wouldn't work normally if there's no such variable, yet perl will only complain with use strict. Using a variable to store the intelligence value, $intelligence, and typoing it the next time around to $inteligence is the best analogy I can find for getting your field names wrong, and that's where use fields; helps.

-nuffin
zz zZ Z Z #!perl


Comment on Re^4: use fields; # damnit
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (10)
As of 2014-09-18 08:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

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











    Results (109 votes), past polls