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

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

Thread Previous | Thread Next
From:
Aristotle Pagaltzis
Date:
September 27, 2016 13:47
Subject:
Re: I'd like to get rid of "use UNIVERSAL;"
Message ID:
20160927134742.GA88934@plasmasturm.org
* Ævar Arnfjörð Bjarmason <avarab@gmail.com> [2016-09-27 14:36]:
> I just couldn't find the part in the core that would allow me to
> distinguish between:
>
>     # should not die
>     $ perl -wE 'package Foo {}; Foo->import'
>     # should die
>     $ perl -wE 'package Foo {}; Foo->import(qw(bar))'

Seems easy?

    $ perl -MO=Concise -e'Foo->import'
    7  <@> leave[1 ref] vKP/REFC ->(end)
    1     <0> enter ->2
    2     <;> nextstate(main 1 -e:1) v:{ ->3
    6     <1> entersub[t1] vKS/TARG ->7
    3        <0> pushmark s ->4
    4        <$> const(PV "Foo") sM/BARE ->5
    5        <$> method_named(PV "import") ->6
    -e syntax OK

    $ perl -MO=Concise -e'Foo->import(1)'
    8  <@> leave[1 ref] vKP/REFC ->(end)
    1     <0> enter ->2
    2     <;> nextstate(main 1 -e:1) v:{ ->3
    7     <1> entersub[t1] vKS/TARG ->8
    3        <0> pushmark s ->4
    4        <$> const(PV "Foo") sM/BARE ->5
    5        <$> const(IV 1) sM ->6
    6        <$> method_named(PV "import") ->7
    -e syntax OK

If I read this right, the arguments are already on the stack by the time
that Perl_gv_fetchmethod_pvn_flags gets called.

> I.e. perl will just ignore missing import/unimport routines, that's
> the bit in the core I patche (to not ignore them for UNIVERSAL), but
> maybe there's a better way.

If what I said is right, then you’d need to add a check for whether the
method is being called with arguments. Then just delete the import sub
from UNIVERSAL (which leaves the file without any code). That should add
up to the right thing happening, no?

> I guess I'd need to patch pp_entersub for that to work, unless anyone
> has better ideas. I.e. check if $pkg eq "UNIVERSAL" and $sub ~~
> [qw(import unimport)] on every call, which would be quite nasty.

Yeah, too nasty to do it that.

Regards,
-- 
Aristotle Pagaltzis // <http://plasmasturm.org/>

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About