Ambiguous method call

C

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>
A::B::new called with args: <A::B=HASH(0x225314) test 1>
Attempt to bless into a reference at A.pm line 18.

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top