http://www.perlmonks.org?node_id=539343


in reply to How to best pass on the context / wantarray?

DBM::Deep does the following which is inspired by CGI's self_or_default()
sub floober { my $self = shift->_get_self; # Do stuff here with the actual object from tied() as opposed to t +he potentially # blessed item that is tied. }
It's a method because this allows me to overload how to get to the tied method in either DBM::Deep::Array or DBM::Deep::Hash.

My criteria for good software:
  1. Does it work?
  2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?

Replies are listed 'Best First'.
Re^2: How to best pass on the context / wantarray?
by Corion (Patriarch) on Mar 27, 2006 at 06:01 UTC

    Something like this was my first idea too, but that doesn't preserve the calling context - wantarray will always be defined in ->_get_self. jdporter's method would solve that problem of course, by inspecting the context one calling level higher.

      I don't think you want the calling context thing. I've been trying to figure out how I would use your proposed API and all it's doing is confusing me. Grandfather said it right.

      My criteria for good software:
      1. Does it work?
      2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?

      _get_self() doesn't need to have the same context as its caller, it just needs to know the context of its caller. So it is as simple as:

      my $self= shift(@_)->_get_self( wantarray );

      - tye