Hallo,
Im folgenden Beispiel, wird der Mehrdeutige Ausdruck
A::B->new( test => 1 )
evaluiert als
A::B()->new( test => 1 )
oder
'A::B'->new( test => 1 )
abhängig davon, ob A geladen wird mit
use A;
oder mit
require A;
(hierbei könnte A::B auch in einem separaten file stehen)
Würdet ihr das erwarten?
Ist der Workaround
return 'A::B' unless (@_);
in sub A::B ein ausreichender Fix (wenn man als Verhalten
'A::B'->new( test => 1 ) erwartet)?
Grüße, Christoph
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/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.
Bzw.:
A::B::new called with args: <A::B test 1>
$VAR1 = bless( {
'test' => 1
}, 'A::B' );