http://www.perlmonks.org?node_id=653255

reneeb has asked for the wisdom of the Perl Monks concerning the following question:

Hi folks,

I've got one question:

test1.pl:

#!/usr/bin/perl use A; my $test = A::B->new(test => 1);

A.pm:

package A; sub B{ print "A::B called with args <@_>\n"; shift; return A::B->new(@_); } package A::B; sub new{ print "A::B::new called with args: <@_>\n"; return bless {}, shift; } 1

result:

A::B called with args <> A::B::new called with args: <A::B> A::B::new called with args: <A::B=HASH(0x23602c) test 1> Attempt to bless into a reference at A.pm line 14.

test2.pl:

require A; my $test = A::B->new(test => 1);

result:

A::B::new called with args: <A::B test 1>

I've search several docs, but haven't found anything that would explain why "use" and a "require" in a BEGIN block have a different behaviour than "require" or a "use"(in a string eval). Why is in one case the subroutine B called (when A is loaded in compile time) and in the other case the subroutine "new" of package A::B is called (when A is loaded in run time)?

Cheers,
Renee