MyStuff/Foo.pm:
\npackage MyStuff::Foo;\n\nuse strict;\nuse MyStuff::OSDetectionAndOtherThings;\n\nsub new\n{\n $class = "FooUnix" if Unix();\n $class = "FooWindows" if Windows();\n\n # Funky Cold Factory Voodoo\n return bless $self, $class;\n}\n\nsub run\n{\n my $self = shift;\n # do some stuff\n $self->some_stuff();\n}\n
MyStuff/FooUnix.pm:
\npackage MyStuff::FooUnix;\n\nuse strict;\n\nuse MyStuff::Foo;\nuse base ("Foo");\n\nsub some_stuff\n{\n my $self = shift;\n # etc.\n}\n
Now, to use it:
\nuse strict;\nuse MyStuff::Foo;\n\nsub main\n{\n my $runner = MyStuff::Foo->new;\n $runner->run();\n}\n\nmain();\n
Error upon running this:
Can't locate object method "run" via package "FooUnix" at blah.pl line 7.
And my questions:
- Can I do what I'm trying to do; ie. bless a class into a particular subclass at runtime based on OS.
- If so, how do I do it properly? :-)