How to take code reference to constructor?

前端 未结 3 503
日久生厌
日久生厌 2021-01-20 18:55

I have following code:

my $coderef = ${MyModule::MyTool->new};

but when I try

$coderef->();

i got error:

Not a CODE re         


        
3条回答
  •  别那么骄傲
    2021-01-20 19:21

    This should work regardless of which package holds the new:

    my $coderef = MyModule::MyTool->UNIVERSAL::can( 'new' ); 
    

    So that if MyModule::MyTool does not implement their constructor, you can still get it's handle.

提交回复
热议问题