Skip to content

Instantly share code, notes, and snippets.

@2shortplanks
Created February 1, 2016 23:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 2shortplanks/f2760d1d8b2d8a8d27a6 to your computer and use it in GitHub Desktop.
Save 2shortplanks/f2760d1d8b2d8a8d27a6 to your computer and use it in GitHub Desktop.
The perils of use constant
Mark@travis:~/tmp/nac$ ls
MyClass.pm MyConstants.pm RoleA.pm RoleB.pm
Mark@travis:~/tmp/nac$ cat MyConstants.pm
package MyConstants;
use base qw(Exporter);
# this is fine
sub my_good_constant { 1 };
push @EXPORT_OK, qw(my_good_constant);
# this ain't
use constant my_bad_constant => 1;
push @EXPORT_OK, qw(my_bad_constant);
1;
Mark@travis:~/tmp/nac$ cat RoleA.pm
package RoleA;
use Moose::Role;
use MyConstants qw( my_good_constant my_bad_constant );
sub from_role_a { return 1 };
1;
Mark@travis:~/tmp/nac$ cat RoleB.pm
package RoleB;
use Moose::Role;
use MyConstants qw( my_good_constant my_bad_constant );
sub from_role_b { return 1 };
1;
Mark@travis:~/tmp/nac$ cat MyClass.pm
package MyClass;
use Moose;
with 'RoleA', 'RoleB';
__PACKAGE__->meta->make_immutable;
1;
Mark@travis:~/tmp/nac$ perl MyClass.pm
Due to a method name conflict in roles 'RoleA' and 'RoleB', the method 'my_bad_constant' must be implemented or excluded by 'MyClass' at /opt/markperl/lib/site_perl/5.22.0/darwin-2level/Moose/Exporter.pm line 419
Moose::with('RoleA', 'RoleB') called at MyClass.pm line 4
Mark@travis:~/tmp/nac$
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment