develooper Front page | perl.perl5.porters | Postings from April 2007

Re: use parent 'Some::Class' @import_list;

Thread Previous | Thread Next
Brandon Black
April 29, 2007 17:17
Re: use parent 'Some::Class' @import_list;
Message ID:
On 4/29/07, Ævar Arnfjörð Bjarmason <> wrote:
> On 4/29/07, Ovid <> wrote:
> So:
> use parent qw( a b );
> would be exactly equivalent to:
> use base qw( a );
> BEGIN { a->import('b') }
> ?
> FWIW `parent' as opposed to `base' doesn't make me think "ah, this'll
> call import too" as much as it makes me wonder what (if any) is the
> difference between a base and a parent package.

I'm also looking at right now for a different reason, but it
impacts this as well.  I suspect (a few more patches in the future)
that bleadperl will be much more efficient when doing:

@Foo::ISA = qw/ some long list of things /;

than when doing:

push(@Foo::ISA, 'some');
push(@Foo::ISA, 'long');
push(@Foo::ISA, 'list');

because of @ISA linearization caching issues and delayed setisa magic
in aassign.  To that end, I'm going to see if I can modify to
play nicer without breaking things.  If your "use parent" interface
only allows specifying one base-class per statement, such an
optimization won't be possible.

use base has other subtle (sometimes undocumented) interesting
side-effects, like the fact that it refuses to add a baseclass to @ISA
which the class already ->isa (through deep inheritance possible).  I
suspect this was a performance hack, but the mro linearization
functions in blead eliminate this redundancy internally anyways.  I'd
like to change the import loop of to be more like:

foreach my $newbase (@newbases) {
    # "require if necessary" magic, which croaks if require fails
@Class::ISA = (@Class::ISA, @newbases);

instead of the current loop which is more like:

foreach my $newbase (@newbases) {
   next if Class->isa($newbase);
   # "require if necessary" magic, which croaks if require fails
   push(@Class::ISA, $newbase);

What about modifying syntax in a backwards-compat way, to work
like this:

use base qw/A B C/; # just like before
use base ('A' => \@import_list, 'B', 'C' => \@import_list, 'D', 'E',
'F' => []); # calls import on A, C, and F (empty import list in the
case of F)

By having it look at ref($foo) on the args as it goes to tell what's
what.  Of course, when combining that idea with the changes I'm
proposing earlier in this email, there's some definite questions about
when imports should be called (just after the "require if necessary",
which is before the package is actually added to @ISA, or call them
all in order after the big @ISA change - I tend to vote for the

-- Brandon

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About