swuecho
5/24/2013 - 12:56 AM

role.pl

use v5.14;

package Switch {
    use Moose::Role;
    
    has 'power' => (is => 'rw', default => sub { 0 });
   
    sub on { shift->power(1); }

    sub off { shift->power(0); }

}

package Computer {
    use Moose;
    with 'Switch';
}


package Light {
    use Moose;
    with 'Switch';
}

my $laptop = Computer->new();
say "computer is off" unless $laptop->power;
$laptop->on;
say "computer is on" if $laptop->power;

my $light = Light->new();
say "light is off" unless $light->power;
$light->on;
say "light is on" if $light->power;