in reply to $self->{foo} in sub new {}
What you really want is to create a new variable called $self and bless that (not an anonymous hashref, as you're currently doing) e.gsub new { my $self = shift; print "I was invoked by: $self\n"; } main->new; __output__ I was invoked by: main
Now the new method blesses the $self variable into the class that invoked the method. For more information on OO in perl see perlboot and perltoot.{ package demo; sub new { my($class, $foo) = @_; my $self = { foo => $foo }; return bless $self, $class; } } use Data::Dumper; my $obj = demo->new('a string'); print Dumper($obj); __output__ $VAR1 = bless( { 'foo' => 'a string' }, 'demo' );
_________
broquaint
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: $self->{foo} in sub new {}
by Roy Johnson (Monsignor) on Dec 22, 2003 at 20:40 UTC | |
by merlyn (Sage) on Dec 22, 2003 at 21:10 UTC | |
|
Re:^2 $self->{foo} in sub new {}
by flounder99 (Friar) on Dec 22, 2003 at 14:50 UTC | |
by merlyn (Sage) on Dec 22, 2003 at 15:07 UTC | |
by flounder99 (Friar) on Dec 22, 2003 at 15:23 UTC | |
by Taulmarill (Deacon) on Dec 22, 2003 at 15:46 UTC | |
by hardburn (Abbot) on Dec 22, 2003 at 18:00 UTC | |
by grinder (Bishop) on Dec 22, 2003 at 15:16 UTC |