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

I have the following problem with object oriented Perl:

I have a package with a method connect

sub connect() { my $self = shift; my ($ip, $port) = @_; # more code }

When I call this method from another script via: $test->connect($ipaddr, $portno);

I get the following error:

Wrong number of parameters: expected 0, got 3. Foo::connect('Foo=HASH(0xca3df0)', $ipaddr, $portno) called at test_co +nnect.pl line 10
Note: The error gives the value of $ipaddr and $portno, but I don't want you to hak me :)

I have looked at every doc I can think of and even using sub connect(\$$$) {} doesn't help. Where am I going wrong? My new constructor works perfectly.

I have a wiry brain/each eye a camera. Robot Tourist, by Ten Benson

Replies are listed 'Best First'.
Re: Wrong number of arguments in methods call
by davis (Vicar) on May 09, 2002 at 10:57 UTC
    It's because you've supplied connect with a prototype by appending the parentheses - () to the name
    Remove these, and it should work.
    see perldoc perlsub for more information.
    hope that helps
    davis
    Is this going out live?
    No, Homer, very few cartoons are broadcast live - it's a terrible strain on the animator's wrist

      Removing the () from connect still gives me the same error. Also, calling connect with no arguments (well just the implied object) gives me the same error: 3 arguments received when 0 expected (although $ipaddr and $portno aren't reported, since they're not there anymore, just in case you thought I wasn't saving my script before I ran it).

      I have a wiry brain/each eye a camera. Robot Tourist, by Ten Benson

        You must be doing something odd (as well as the incorrect () you added) so post your code in its entirity or copy this (works fine)

        my $foo = new Foo; $foo->connect('ip', 8080 ); package Foo; sub new { return bless {}, shift } sub connect { my $self = shift; my ($ip, $port) = @_; print "$self $ip $port"; }

        cheers

        tachyon

        s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print