On May 29, 2004, at 5:10 AM, Graeme McLaren wrote:
[..]
I create an object and pass a value to my constructor like this:
Conf->new(site=>'x'), this tells the constructor which site I want the
config for, then at the end of the constructor I call $self->_init();
This passes a reference to $self ( I think ) using $self = shift;
When I die out using: die %{$self};
I get the value that "x" equals ( x => '/usr/local/apache/htdocs/x' )
I need to perform additional logic in $self->_init(); and what I
really need to do is get the name, which in this case is "x" instead
of the value which in this case is "/usr/local/apache/htdocs/x". How
do I do this I'm a bit lost with perl OO programming :(
Thank you for any light anyone can shed on this.
Graeme :)
################ Perl Code #################
sub new{
my ($caller, %arg) = @_;
my $conv = {
x => '/usr/local/apache/htdocs/x',
y => '/usr/local/apache/htdocs/y',
z => '/usr/local/apache/htdocs/z'
};
my $caller_is_obj = ref($caller);
my $class = $caller_is_obj || $caller;
my $self = bless {}, $class;
$self->{_doc_root} = $conv->{$arg{site}||'x'};
if(!exists $conv->{$arg{site}}){
die "check for typos in yer params";
}
$self->_init();
}
sub _init{
my $self = shift;
die %{$self};
}
##################### End of Perl Code ####################
Well, you could start a whole lot simpler with say
#---------------------------------
# Our Stock Constructor
# note: <http://www.perlmonks.org/index.pl?node_id=52089>
sub new
{
my $class = shift;
my $self = {};
bless $self, $class;
} # end of our simple new
since that part about the
my $caller_is_obj = ref($caller);
is not as useful - note the url to perlmonks
Then if you really want to go with the '_init()'
Idea then you could grow that to
sub new
{
my $class = shift;
my $self = {};
bless $self, $class;
$self->_init(@_);
}
sub _init{
my ($caller, %arg) = @_;
my $conv = {
x => '/usr/local/apache/htdocs/x',
y => '/usr/local/apache/htdocs/y',
z => '/usr/local/apache/htdocs/z'
};
....
self; # since the 'new' returns the blessed self...
}
ciao
drieux
---
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>