develooper Front page | perl.perl5.porters | Postings from September 2016

Re: I'd like to get rid of "use UNIVERSAL;"

Thread Previous | Thread Next
September 27, 2016 11:40
Re: I'd like to get rid of "use UNIVERSAL;"
Message ID:
David Cantrell <> writes:

> On Mon, Sep 26, 2016 at 10:32:11PM +0200, ?var Arnfj?r? Bjarmason wrote:
>> I've just pushed a smoke-me branch to remote UNIVERSAL::import from
>> that package, and make its (no)?import a fatal error via a hack to
>> gv.c:
>> The motivation here is that someone doing "use UNIVERSAL;" in some
>> module shouldn't make $pkg->can("import") true for any value of $pkg
>> in your program.
>> But technically this does break backwards compatibility without a
>> deprecation cycle. Because "use UNIVERSAL;" isn't an error today, and
>> some code on the CPAN redundantly uses that.
> You'll also break this:
>   use UNIVERSAL;
>   ...
>   if(UNIVERSAL::isa($foo, 'Some::Class')) { ...
> which is a lot more convenient than checking to see if $foo is blessed,
> and if it is calling $foo->isa().

This continues to "work", and never required 'use UNIVERSAL', since the
function is provided by universal.c, not

> That said, it's not a particularly common idiom and I'm happy to change
> it in my code.

The idiom also breaks for any class that overrides isa, like
MooseX::Types::TypeDecorator and Moo::HandleMoose::FakeMetaClass, and is
warned against in the UNIVERSAL documentation:

    # but never do this!
    $is_io    = UNIVERSAL::isa($fd, "IO::Handle");
    $sub      = UNIVERSAL::can($obj, "print");

    Previous versions of this documentation suggested using isa as a
    function to determine the type of a reference[.]

    The problem is that this code would never call an overridden isa
    method in any class

"I use RMS as a guide in the same way that a boat captain would use
 a lighthouse.  It's good to know where it is, but you generally
 don't want to find yourself in the same spot." - Tollef Fog Heen

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About