[Perl] Perl OO Methodenaufruf in anderem Objekt

enr00

enr00

Routinier
Hallo,
Ich bin dabei in Perl ein Pokerspiel zu programmieren, hänge aber momentan an einem Problem, wo ich nicht weiterkomme.
Ich habe die Klasse poker_game, und ein Test-programm dafür:

poker_game.pm
Code:
#!/usr/bin/env perl 

use strict;
use warnings;
use utf8;

my @suits = ("S", "H", "D", "C");
my @numbers = (1..13);

use lib '.';
use card_deck;
use poker_player;

package poker_game;

sub new {
	my $class = shift;
	my $self = {@_};

	$self->{round} = 0;
	#one array for the players
	$self->{holdem_cards} = new card_deck;

	my $bl = bless $self, $class;

	return $bl;
}

#give players their cards
sub player_cards {
	my $self = shift;
	#each player gets 2 cards
	foreach my $player (0..3) {
		foreach my $c (1..2) {
			my $card = ($self->{holdem_cards})->draw_card();
			#push $player->{cards}, $card;
			$self->{players}[$player]->{cards}[$c] = $card;
		}
	}
}

#deal 3 cards for common use
sub flop {
	my $self = shift;
}

#deal 1 card for common use
sub turn {
	my $self = shift;
}

#deal 1 card for common use
sub river {
	my $self = shift;
}

sub list_players {
	my $self = shift;

	my $ref = $self->{players};
	@$ref[1]->print_info;
	foreach my $player (@$ref) {
		$player->print_info;                     # <-------------------------------------------------- Fehler
	}
}

test_poker_game.pl
Code:
#!/usr/bin/env perl 

use strict;
use warnings;
use utf8;
#use diagnostics;

use lib '.';
use poker_game;
use poker_player;


my @players = ();

#determine number of players
print "How many players?";
my $players = <STDIN>;
chomp $players;

#fill player objects with names
foreach (1..$players) {
	print "Please enter a name: ";
	my $name = <STDIN>;
	chomp $name;

	my $pl = new poker_player(name => $name);
	push @players, $pl;

}

print "Players for this round: \n";
foreach my $i (@players) {
	#$i->print_info();
}

my $test = \@players;
foreach my $i (@$test) {
	$i->print_info;                  # <-------------------------------------------- kein Fehler
}
<STDIN>;

my $poker_game = new poker_game(players => \@players);
$poker_game->player_cards();

$poker_game->list_players();

Der Fehler
Code:
Can't call method "print_info" on unblessed reference at poker_game.pm line 61, <STDIN> line 2.
tritt aber nur auf, wenn ich die Methode in der .pm-Datei aufrufe, nicht aber aus der .pl-Datei.

Irgendwelche Ideen?
 

Ähnliche Themen

Server und Client für TCP und UDP

Arbeitsspeicher von Perl Threads wieder freigeben

perl: curl sendet basic anstatt ntlm

Perl serielle Schnittstelle

Akonadi startet nicht mehr

Zurück
Oben