Ambiguous method call

Discussion in 'Perl Misc' started by Ch Lamprecht, Nov 27, 2007.

  1. Ch Lamprecht

    Ch Lamprecht Guest

    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' );
     
    Ch Lamprecht, Nov 27, 2007
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ishekara

    ambiguous call to overloaded

    ishekara, Feb 25, 2005, in forum: C++
    Replies:
    7
    Views:
    6,531
    red floyd
    Feb 25, 2005
  2. Replies:
    5
    Views:
    355
    Pete Becker
    Mar 10, 2006
  3. rolandz
    Replies:
    5
    Views:
    531
    Jim Langston
    Jul 4, 2006
  4. Replies:
    8
    Views:
    388
    terminator
    Sep 3, 2007
  5. , India
    Replies:
    1
    Views:
    413
    Kai-Uwe Bux
    Sep 10, 2007
Loading...

Share This Page