Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: accessing module variables howto

by jaa (Friar)
on Aug 29, 2006 at 07:32 UTC ( [id://570100]=note: print w/replies, xml ) Need Help??


in reply to accessing module variables howto

Lots of things wrong with your sample code - but probably the most crucial, is that it contains a number of basic syntax problems, and so would never have actually run.

Perhaps you want something like:

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}; }
There are many ways to use object orientation, this is just one of them...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://570100]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-19 20:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found