in reply to Re: Using a 'package' as a 'module', and choosing at runtime?
in thread Using a 'package' as a 'module', and choosing at runtime?

References to package subroutines are always done symbolically (even with strict 'refs' on), so you only need to change how the package is called.

#!/usr/bin/perl -w my $pkg; BEGIN { if ( $^O =~ /dar/ ) { $pkg = 'mac' } elsif ( $^O =~ /sol/ ) { $pkg = 'sun' } elsif ( $^O =~ /aix/ ) { $pkg = 'aix' } else { die "unknown os." } }; print "answer is " . $pkg->get_answer() . "\n"; exit 0; package mac; sub get_answer { return "Apple" } package sun; sub get_answer { return "Sun" } package aix; sub get_answer { return "IBM" } __END__

Which makes it a lot easier to add new class methods in the future.

----
I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
-- Schemer

:(){ :|:&};:

Note: All code is untested, unless otherwise stated

Replies are listed 'Best First'.
Re: Re: Re: Using a 'package' as a 'module', and choosing at runtime?
by blueflashlight (Pilgrim) on Oct 28, 2003 at 18:10 UTC
    Excellent! Thanks so much; this looks exactly like what I want to accomplish.