Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Re: $foo->bar = 14;

by hypochrismutreefuzz (Scribe)
on Jan 04, 2003 at 10:20 UTC ( #224241=note: print w/ replies, xml ) Need Help??

in reply to $foo->bar = 14;

Another way to encapsulate a property is with a psuedo hash. The fields pragma gives the $self variable the instance variables and the new() returns a blessed reference. Then, another psuedo hash is made, $prop, that acts as a static variable that maps the property names to code references. Then the method 'property' is called thus: $obj->property('propertyname', 'value' ...);

The example I give uses a colorref type of variable that takes a red, blue, and green integer values and returns a hex value that you could put in an HTML attribute, say.
package ClassExample; use strict; use warnings; use fields qw/color texture/; my $prop = fields::phash( [qw/color texture/], [\&color_prop, \&texture_prop] ); sub create { my $class = shift; my $self = fields::new($class) unless ref($class); return $self; } sub property { my $self = shift; my ($propname, @params) = @_; $self->{$propname} = &{ $prop->{$propname} }(@params) if @params; return $self->{$propname}; } # these subroutines could be isolated in another module and imported # PRIVATE!!! keep out! sub color_prop { my %colors = @_; my $ok = 0; for (qw/red green blue/) { if (exists $colors{$_}) { next if $colors{$_} < 0; next if $colors{$_} > 0xff; $ok++; } } die "@_ is not a red/green/blue color ref" unless $ok == 3; return sprintf('%02X%02X%02X', $colors{red}, $colors{green}, $colo +rs{blue}); } sub texture_prop { my $texture = shift; my $ok = 0; for (qw/rough smooth crinkly/) { if ($texture eq $_) { $ok = 1; last; } } die "$texture is not rough/smooth/crinkly texture value" unless $o +k; return $texture; } 1; # #!/usr/local/bin/perl use strict; use warnings; use ClassExample; my $clex = ClassExample->create(); $clex->property('color', 'green', 0xde, 'red', 255, 'blue', 1); $clex->property('texture', 'rough'); my $printthing = <<"end_of_print"; Properties of %s: Texture: %s Color: %s end_of_print print "\n"; printf ($printthing, '$clex', $clex->property('texture'), $clex->prope +rty('color')); my $texture = $clex->property('texture', 'smooth'); my $colorref = $clex->property('color', red=>0x52, green=>0x20, blue=>0x11); printf ($printthing, '$clex', $texture, $colorref);

By running you get the output


Properties of $clex:
Texture: rough
Color:   FFDE01

Properties of $clex:
Texture: smooth
Color:   522011

I tested it using ActiveState Perl 5.8 under Windows 2000.

Comment on Re: $foo->bar = 14;
Download Code

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (5)
As of 2015-11-28 08:50 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (740 votes), past polls