Alien has asked for the wisdom of the Perl Monks concerning the following question:
I have the following class :
and the following code :package Link; sub FETCH { my $this = shift; return $this->{"site"}; } sub STORE { my ($self,$site) = @_; $self->{"site"} = $site; } sub print_method { my $self = shift; print $self->{"site"}; } sub TIESCALAR { my $class = shift; my $link = shift; my $this = {}; bless($this,$class); $this->{"site"} = $link; return $this; } 1;
Running the code above results in a runtime error : Can't call method "print_method" without a package or object reference at tietest.pl line 4. . I think I understand why : when fetched , the method returns a string , which doesn't respond to "print_method" . I would like to be able to do the following things : if fetched , print the site's address , if stored , change the address . I would also like to be able to call methods on the object . I am aware that if I return $self from fetch , I will accomplish the "call methods objective" , but that will invalidate the first objective ( fetch ) . Is there a way I could accomplish all of them ?use Link; tie my $var,"Link","http://somesite.com"; $var = "http://anothersite.com"; $var->print_method; print $var;
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Tying objects
by Arunbear (Prior) on Feb 07, 2009 at 20:59 UTC | |
by JadeNB (Chaplain) on Feb 08, 2009 at 00:04 UTC | |
Re: Tying objects
by shmem (Chancellor) on Feb 08, 2009 at 11:29 UTC | |
Re: Tying objects
by Herkum (Parson) on Feb 08, 2009 at 02:59 UTC | |
Re: Tying objects
by pajout (Curate) on Feb 08, 2009 at 09:24 UTC | |
Re: Tying objects
by LanX (Saint) on Feb 08, 2009 at 13:55 UTC | |
Re: Tying objects
by dragonchild (Archbishop) on Feb 09, 2009 at 21:24 UTC |
Back to
Seekers of Perl Wisdom