Zesty
Zesty

Reputation: 281

was given too many arguments; it expects 1 (Perl)

I've been working on a card game that gets your deck and picks 1 out of three powers cards but whenever I try to run it, it gives me an error

Error:

Error: In call to CPUser::addCards(), was given too many arguments; it expects 1 at Server/CPUser.pm line 427.

Line 427:

 if($intItem == 821){ #Adds Card Jitsu Cards for Classic 
             $self->addCards($self->buildStarterDeck); 
       }

and this the buildStarterDeck Method

method buildStarterDeck {

    sub get_cards;
    my (@stackone, @stacktwo) = get_cards;

    sub get_cards 
    {
        my @start_cards = (1, 6, 9, 14, 17, 20, 22, 23, 26);
        my @power_cards = (73, 81, 89);
        @power_cards = $power_cards[rand @power_cards];

    return (@start_cards, @power_cards);
    }
}

The addCard method is empty since I've been trying to figure out this error and I couldn't get any luck.

Upvotes: 0

Views: 328

Answers (1)

Borodin
Borodin

Reputation: 126722

The problem is in your definition of addCards which you persist in hiding from us despite multiple requests to see it. It must look something like this

method addCards($param) {
    ...;
}

But you are passing it a ten-element list in $self->addCards($self->buildStarterDeck) so the given too many arguments error is raised. You don't explain what you want it to do, but something like this is probably more appropriate

method addCards(@cards) {
    ...;
}

You really shouldn't declare subroutines inside other subroutines. It doesn't limit the scope of the inner subroutine, but you can create a closure over variables declared in the outer subroutine that doesn't work properly

Also bearing in mind zdim's warning from the comments, your code should look more like this

use strict;
use warnings 'all';

use Method::Signatures;

method buildStarterDeck {

    my @stack = get_cards;
}

sub get_cards {

    my @start_cards = (1, 6, 9, 14, 17, 20, 22, 23, 26);
    my @power_cards = (73, 81, 89);

    $power_card = $power_cards[rand @power_cards];

    return (@start_cards, $power_card);
}

Upvotes: 4

Related Questions