in reply to ISA with packages ... A barebones minimal example

Thanks to the responses and a little digging, I know now enough to re-ask the original question. The real sticking point was this ... "what are the available alternatives to the standard scope resolution operator in perl (aka double-colon)".

Although the suggestions about using OO programming are appreciated, the problem is I have two pre-existing packages "Alpha" and "Bravo" (lets avoid the names A and B) and they are already done and not coded OO-style, they're just filled with run-of the mill subroutines.

With that in mind, I redid the barebones example, I took out all the Exporter stuff, and just used @ISA in tandem with Bravo->SayBye() ... it worked as expected.

The new problem is, that Bravo->SayBye() does not exactly work the same as Bravo::SayBye() because the first one (invisibly?) passes an extra argument, as someone already pointed out.

That's not good because it messes up the expected results of the subroutines.

What it all boils down to is I need to do inheritance but without being able to rewrite "OO style" package code, and without the benefit of having the double-colon scope resolution operator as an option. That's essentially what I was trying to do. Any more suggestions? Do I have to go in and rewrite Alpha and Bravo just to be able to do inheritance?

  • Comment on The Scope Resolution Operator (was Re: ISA with packages ... A barebones minimal example)

Replies are listed 'Best First'.
Re: The Scope Resolution Operator (was Re: ISA with packages ... A barebones minimal example)
by Arunbear (Prior) on Oct 08, 2004 at 22:44 UTC
    Here is one way to 'inherit' subroutines without rewriting your packages in the OO style:
    use strict; use warnings; package Alpha; our $language = "US-English"; $Alpha::first = "Alpha"; sub SayHello {"hello from package Alpha\n"}; sub SayBye {"Goodbye.\n"} sub GreetByName {"Well Hello $_[0].\n";} package Bravo; our $AUTOLOAD; sub AUTOLOAD { $AUTOLOAD =~ s/^.+:://; # strip package name *sym = $Alpha::{$AUTOLOAD}; *sym{CODE}->(@_); } $Bravo::first = "Bravo"; sub SayHello {"hello from package Bravo\n"}; package main; ### begin_: get stuff from A print("$Alpha::first\n"); print Alpha::SayHello(); print("$language\n"); print Alpha::SayBye(); # -> Goodbye. print Alpha::GreetByName('Dolly'); # -> Well Hello Dolly. print "\n---------------------\n"; ### begin_: get stuff from Bravo print("$Bravo::first\n"); print Bravo::SayHello(); print("$language\n"); print Bravo::SayBye(); # -> Goodbye. print Bravo::GreetByName('Billy'); # -> Well Hello Billy. print "\n---------------------\n";
    Here, when you call Bravo::foo() but foo() is not in Bravo, Bravo's AUTOLOAD subroutine will call Alpha's foo() subroutine. See 'Autoloading' in perlsub for more on AUTOLOAD.