in reply to accessing module variables howto
Perhaps you want something like:
There are many ways to use object orientation, this is just one of them...use strict; # Note how a constructor is called # Classname->new() NOT Classname::new() my $Mref = Manager->new(); # Prints the value of var for $Mref using an accessor method print $M1->var, "\n"; print $M1->var("peaches"), "\n"; # Each instance has its own var my $M2 = Manager->new(); $M2->var("apples"); ############## Manager.pm ######### package Manager; use strict; # reworked constructor sub new { my $class = shift; # first arg is name of class # create anon hash containing member var # and bless into class... my $self = bless { var => "var initialised", }, $class; return $self; } # return true for successful package initialisation 1; # set/get accessor for var - sub var { my $self = shift; $self->{var} = $_[0] if scalar @_; return $self->{var}; }
|
|---|