IWETHEY v. 0.3.0 | TODO
1,095 registered users | 0 active users | 0 LpH | Statistics
Login | Create New User
IWETHEY Banner

Welcome to IWETHEY!

New Perl class question
I'm trying to do some fancy-schmancy factory method stuff with bless in Perl:

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:
  1. Can I do what I'm trying to do; ie. bless a class into a particular subclass at runtime based on OS.
  2. If so, how do I do it properly? :-)

Regards,

-scott anderson

"Welcome to Rivendell, Mr. Anderson..."
New Answering my own question:
Changing the bless to "MyStuff::FooUnix" worked.
Regards,

-scott anderson

"Welcome to Rivendell, Mr. Anderson..."
Expand Edited by admin Jan. 27, 2003, 06:28:22 PM EST
Expand Edited by admin Jan. 27, 2003, 06:32:26 PM EST
New In only 5 minutes! :)
Alex

"No man's life, liberty, or property are safe while the legislature is in session."\t-- Mark Twain
New You can lead a Foo to Unix
but you can't Foolinux
New Sure you can
[link|http://nysv.org/foolinux/about.shtml|http://nysv.org/foolinux/about.shtml]
--
Chris Altmann
New !! Who'd a thunk_____Every pun's a title now!
     Perl class question - (admin) - (5)
         Answering my own question: - (admin) - (4)
             In only 5 minutes! :) -NT - (a6l6e6x)
             You can lead a Foo to Unix - (Ashton) - (2)
                 Sure you can - (altmann) - (1)
                     !! Who'd a thunk_____Every pun's a title now! -NT - (Ashton)

But he doesn't look quite skeletal enough for both to be true, does he?
62 ms