Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Re: Re: Re: Perl Programming guidlines/rules

by pdcawley (Hermit)
on Nov 22, 2002 at 15:02 UTC ( [id://215108]=note: print w/replies, xml ) Need Help??


in reply to Re: Re: Re: Perl Programming guidlines/rules
in thread Perl Programming guidelines/rules

Oh, I'd definitely make that an object. If only so I could either make normalized_designation a computed attributed, or at least write a set_normalized_designation that looked something like:
sub set_normalized_designation { my $self = shift; my $designation = shift; $self->ensure_normalization($designation); $self->{normalized_designation} = shift; } sub ensure_normalization { my $self = shift; my $designation = shift; $designation =~ /.../ or die "$designation is not correctly normalized"; }
You could certainly argue that this might be considered long winded, but writing things this way makes assumptions explicit and ensure that they're enforced.

Of course, in Perl 6 you can have your cake and eat it:

class Thing { my $.title is rw; my $.standard_directory is rw; # Where graphics etc are found my $.designation is rw; my Hash $.def; # Use another class? method normalized_designation { my $normalized_des = $.designation; $normalized_des =~ s/.../.../; return $normalized_des; } }
Now, I might be a little behind on Perl 6 OO, but ISTR that setting is rw on object attributes will autogenerate accessors. If not I'm sure someone will subclass Object to do the right thing.

Note that, in not much more space than it took to comment your hash you have an object, and, for free you get a restricted set of keys, and your dependent field removed and replaced with a computation, and you've got somewhere to hang common behaviour relating to this data structure.

Damn, but I want Perl 6 soon.

Replies are listed 'Best First'.
Re: Re: Re: Re: Re: Perl Programming guidlines/rules
by mirod (Canon) on Nov 22, 2002 at 15:33 UTC

    The only reason the normalized designation is a field is purely a syntactic one: it makes it (admittedly slightly!) easier to print within a string: print qq{$standart->{des} ($standart->{norm_des})\n}; looks nicer than print qq{$standart->{des} (} . normalized( $standart->{des}) . qq{)\n};. Otherwise it would be really easy to use a Memoize-ed function to compute it on the fly.

    And (before you jump on this one ;--) yes, if I used objects I could use the printf version which looks ok: printf( "%s (%s)\n", $standard->des, $standard->norm_des);

    I guess it's purely a matter of personal preference here, but I usually leave OO out of smallish scripts, and comment my data structures.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2024-04-24 23:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found