[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[FWP] fun with inheritance



I offer some meager (but fun) ways to play with objects:

  # search @ISA first, and THEN the original class
  $obj = new MyClass;
  {
    local @TMP::ISA = (@MyClass::ISA, "MyClass");
    $obj->TMP::method();
  }

  # same, using symbolic references
  $obj = new MyClass;
  {
    no strict 'refs';  # because you ARE using strict, AREN'T YOU?
    local @TMP::ISA = (@{ ref($obj) . "::ISA" }, ref($obj));
    $obj->TMP::method();
  }

  # doing SUPER:: anywhere you want
  $obj = new MyClass;
  {
    local *MyClass::method;
    $obj->method();  # forces @ISA search
  }

  # same thing, with symrefs
  $obj = new MyClass;
  {
    local *{ ref($obj) . "::$method" };  # or "::method"
    $obj->$method();  # or "->method()"
  }

Now, this upsets me... if you say

  local *MyClass::method;

then $MyClass::method is also temporarily nothing.  And I can't say:

  local &MyClass::method;

nor can I say

  local *MyClass::method = *MyClass::method;
  undef &MyClass::method;

because then Perl says:

  Undefined subroutine &MyClass::method called ...

even though it should go through the @ISA tree.  And I even tried

  my $cref = *MyClass::method{CODE};
  undef &MyClass::method;
  # ...
  *MyClass::method = $cref;

but that still gives the undefined subroutine warning.  Now, do I have to
do

  local *save = *MyClass::method;
  local *MyClass::method;
  *MyClass::method = \$save;
  *MyClass::method = \@save;
  *MyClass::method = \$save;
  *MyClass::method = *save{IO}
  *MyClass::method = *save{FORMAT};
  # any others I forgot?
  $obj->method();

That would be icky.  I think I'll petition P5P in light of this discovery
(this is under 5.005_02 -- if 5.005_03 or 5.6 has a better/different
behavior, let me know).  I'd like to be able to say

  local &func;

since, when it comes to inheritance, that OBVIOUSLY means something other
than

  my $cref = *func;
  undef &func;
  # ...
  *func = $cref;

That was Fun.

-- 
Jeff "japhy" Pinyan     japhy@pobox.com     http://www.pobox.com/~japhy/
PerlMonth - An Online Perl Magazine            http://www.perlmonth.com/
The Perl Archive - Articles, Forums, etc.    http://www.perlarchive.com/
CPAN - #1 Perl Resource  (my id:  PINYAN)        http://search.cpan.org/


==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe