Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

merlyn (47)

merlyn
  merlyn@stonehenge.com
http://www.stonehenge.com/merlyn/
AOL IM: realmerlyn (Add Buddy, Send Message)
Yahoo! ID: realmerlyn (Add User, Send Message)

PAUSE-ID: MERLYN [cpan.org].
See my home page [stonehenge.com].

Journal of merlyn (47)

Saturday January 04, 2003
12:49 PM

changing the tied object

[ #9759 ]
Just had a discussion on IRC... thought I would write this down in case anyone else wants the conclusion.

Yes, you can indeed write to $_[0] inside one of the tie accessor callbacks (like STORE or FETCH), and change the underlying hidden tied action object.

For example, suppose you want to wrap Apache::Session, so that on first store or fetch, it does some init before becoming a real session object:

package My::Apache::Session;
sub TIEHASH {
  my $class = shift;
  bless [@_], $class; # remember real args
}
sub FETCH {
  ## DO NOT USE shift HERE
  $_[0]->my_initialization;
  $_[0] = Apache::Session->TIEHASH(@{$_[0]});
  $_[0]->FETCH($_[1]);
}
sub STORE {
  ## DO NOT USE shift HERE
  $_[0]->my_initialization;
  $_[0] = Apache::Session->TIEHASH(@{$_[0]});
  $_[0]->STORE($_[1], $_[2]);
}
sub my_initialization { ... }

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.