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
In reply to Re: Re: Using a 'package' as a 'module', and choosing at runtime?
by hardburn
in thread Using a 'package' as a 'module', and choosing at runtime?
by blueflashlight
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |