Subject: | Attribute initialization vs Module loading |
Date: | Thu, 30 Aug 2018 15:12:53 +0000 |
To: | "bug-Moo [...] rt.cpan.org" <bug-Moo [...] rt.cpan.org> |
From: | "Valderrama, Mario" <valderrama [...] strato.de> |
Hello,
I know this is probably not the best way to do this, but creating objects of subclasses from a function in the superclass
doesn't correctly initalize the attributes.
Say I have 3 files:
Foo.pm
package Foo;
use Moo;
use FooBar;
has foo => (is => 'ro');
sub make_foobar { FooBar->new(@_) }
1;
FooBar.pm
package FooBar;
use Moo;
extends 'Foo';
has bar => (is => 'ro');
1;
main.pl
#!/usr/bin/env perl
use strict;
use warnings;
use 5.026;
use lib '.';
use Foo;
my $foobar = Foo::make_foobar(foo => 'foo', bar => 'bar');
say $foobar->foo();
say $foobar->bar();
Invoking main.pl will yield the following:
$ perl main.pl
Use of uninitialized value in say at main.pl line 9.
bar
This however can be fixed by either adding a 'use FooBar;' before the 'use Foo;' in main.pl or by using Moose instead of Moo in any of the two modules.
Making one of these changes will then result in:
$ perl main.pl
foo
bar
I'm using:
- Perl 5.26.0 (installed with plenv)
- Moo 2.003004
- Moose 2.2011 (for reference)
- Fedora 28 with Kernel 4.17.18-200.x86_64
Message body is not shown because sender requested not to inline it.
Message body is not shown because sender requested not to inline it.
Message body is not shown because sender requested not to inline it.