Вопрос

Which way should I use?

package My_Module;

# way one
my $package = $^O eq 'MSWin32' ? 'My_Module::Win32' : 'My_Module::Linux';
sub new {
    my $class = shift;
    return $package->new( @_ );
}

# way two
use parent $^O eq 'MSWin32' ? 'My_Module::Win32' : 'My_Module::Linux';
Это было полезно?

Решение

Personally I'd go with the first technique. Creating a class that sometimes inherits from one parent, and sometimes inherits from another, seems a little dirty to me. It'll work certainly, but it will confuse class analysis tools (like Pod::Coverage::CountParents) no end.

That said, I'd probably rename sub new to something like new_by_platform. There's a strong expectation that My_Module->new will return an object of type My_Module. Having it return an object of another class could confuse people. (Though if My_Module::Win32 and My_Module::Linux inherit from My_Module, then naming it new seems less bad.)

Другие советы

2 - use parent is the better way to go about it.

http://lo-f.at/glahn/2009/08/useBaseAnduseParent.html

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top