Skip to content

Instantly share code, notes, and snippets.

@wolfiestyle
Created October 22, 2012 15:21
Show Gist options
  • Save wolfiestyle/3932036 to your computer and use it in GitHub Desktop.
Save wolfiestyle/3932036 to your computer and use it in GitHub Desktop.
Declarative objects in Perl using closures
#!/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