CHAPTER 11. PERLBOT 11.3. INSTANCE VARIABLES 11.3 INSTANCE VARIABLES 11.4 SCALAR INSTANCE VARIABLES 149 package Foo; sub new { my $type = shift; my $self; $self = shift; bless \$self, $type; } package main; $a = Foo->new( 42 ); print "a=$$a
"; package Foo; sub new { my $type = shift; my %params = @_; my $self = {}; $self->{’High’} = $params{’High’}; $self->{’Low’} = $params{’Low’}; bless $self, $type; } package Bar; sub new { my $type = shift; my %params = @_; my $self = []; $self->[0] = $params{’Left’}; $self->[1] = $params{’Right’}; bless $self, $type; } package main; $a = Foo->new( ’High’ => 42, ’Low’ => 11 ); print "High=$a->{’High’}
"; print "Low=$a->{’Low’}
"; $b = Bar->new( ’Left’ => 78, ’Right’ => 40 ); print "Left=$b->[0]
"; print "Right=$b->[1]
"; An anonymous array or anonymous hash can be used to hold instance variables. Named parameters are also demonstrated. An anonymous scalar can be used when only one instance variable is needed.