Ambiguous method call

Ambiguous method call

am 27.11.2007 13:29:47 von Ch Lamprecht

Examining the example below, I find the following behavior:
The (ambiguous) expression
A::B->new( test => 1 )
is evaluated as
A::B()->new( test => 1 )
or as
'A::B'->new( test => 1 )

depending on whether A is loaded with
use A;
or with
require A;

(package A::B could as well be required/used separately from a different file
without changing the result)

Is that behavior expected?

Regards, Christoph

#!/usr/bin/perl
use strict;
use warnings;
use A;
#require A;
use Data::Dumper;

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

__END__

file A.pm:

package A;
use strict;
use warnings;

sub B{
print "A::B called with args <@_>\n";
#return 'A::B' unless (@_);
shift;
return A::B->new(@_);
}

package A::B;

sub new{
print "A::B::new called with args: <@_>\n";
my $class = shift;
my $self = {@_};
bless $self, $class;
return $self;
}
1;


Output:
A::B called with args <>
A::B::new called with args:
A::B::new called with args:
Attempt to bless into a reference at A.pm line 18.

Vs.:
A::B::new called with args:
$VAR1 = bless( {
'test' => 1
}, 'A::B' );