Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use strict;
- use warnings qw( all );
- use feature qw( say );
- BEGIN {
- package Wire;
- sub new {
- my ($class, %args) = @_;
- my $voltage = $args{voltage} // 0;
- my $self = bless({}, $class);
- $self->{voltage} = $voltage;
- $self->{parent} = $self; # Memory leak.
- return $self;
- }
- sub _tail {
- my ($self) = @_;
- $self = $self->{parent} while $self != $self->{parent};
- return $self;
- }
- sub get_voltage {
- my ($self) = @_;
- return $self->_tail()->{voltage};
- }
- sub set_voltage {
- my ($self, $voltage) = @_;
- $self->_tail()->{voltage} = $voltage;
- }
- sub fuse {
- my ($self, $src) = @_;
- $self->_tail()->{parent} = $src;
- }
- $INC{"Wire.pm"} = 1;
- }
- my $o1 = Wire->new( voltage => 1 );
- my $o2 = Wire->new( voltage => 2 );
- my $o3 = Wire->new( voltage => 3 );
- my $o4 = Wire->new( voltage => 4 );
- say join " ", map $_->get_voltage(), $o1, $o2, $o3, $o4; # 1 2 3 4
- $o2->fuse($o1);
- $o3->fuse($o4);
- $o1->fuse($o3);
- say join " ", map $_->get_voltage(), $o1, $o2, $o3, $o4; # 4 4 4 4
- $o1->set_voltage(5);
- say join " ", map $_->get_voltage(), $o1, $o2, $o3, $o4; # 5 5 5 5
- $o3->set_voltage(6);
- say join " ", map $_->get_voltage(), $o1, $o2, $o3, $o4; # 6 6 6 6
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement