Created
October 22, 2012 15:21
-
-
Save wolfiestyle/3932036 to your computer and use it in GitHub Desktop.
Declarative objects in Perl using closures
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use strict; | |
sub create | |
{ | |
my ($name, $age) = @_; | |
return { | |
get_age => sub { $age; }, | |
set_age => sub { ($age) = @_; }, | |
get_name => sub { $name; }, | |
set_name => sub { ($name) = @_; }, | |
print => sub { print "Hi, I'm $name and I'm $age years old.\n"; }, | |
}; | |
} | |
my $a = create('Bob', 12); | |
$a->{print}->(); | |
$a->{set_name}->('Dora'); | |
$a->{set_age}->(25); | |
$a->{print}->(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment