Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PurePerl object initializers should be compiled in a controlled namespace #106

Open
tobyink opened this issue Jan 27, 2020 · 0 comments
Open

Comments

@tobyink
Copy link

tobyink commented Jan 27, 2020

In Mouse::Meta::Method::Constructor, the _generate_initialize_object method generates a coderef by evaluating a string like:

    package %s;
    sub {
        my($meta, $instance, $args, $is_cloning) = @_;
        %s;
        return $instance;
    }

Where the first %s is the class name that constructor is being compiled in, and the second %s contains a whole bunch of code dealing with type constraints, defaults, triggers, etc.

That code potentially contains various Perl built-ins like push. But the class that the constructor is being compiled for might have overridden push, in which case the compiled object initializer will potentially see the overridden push instead of CORE::push.

A quick fix would be something like:

    package %s;
    sub {
        my($meta, $instance, $args, $is_cloning) = @_;
        do { package Mouse::Meta::Method::Constructor; %s };
        return $instance;
    }

This is probably not the only place where this issue occurs, but it's the place that's bitten me.

TLDR:

This warns:

package Foo;
use Mouse;
has bar => ( is => 'ro', trigger => sub {} );
sub push { 1 }
Foo->new( bar => 1 );

This dies:

package Foo;
use Mouse;
use subs 'push';
has bar => ( is => 'ro', trigger => sub {} );
sub push { die; }
Foo->new( bar => 1 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant