in reply to Let's play poker

Your code is already nearly ready for objects. The "secret" to perl's basic OOP is that the function bless will tell perl that any function call using the syntax $object->method(arg1, arg2...) must be replaced by Package::method($object, arg1, arg2);, where Package can be seen as the type of the object (called class), and is the second argument to bless. Actually if perl can't find the function in the Package/class of the object, it can search in other "parent" classes, but you don't need that here.

When you are creating your first object, you don't have one to call $object->method(), so instead of using the syntax $object->new() you can write my $object = Package->new(), where the string "Package" is the first argument to the call, so that you can use it with bless.

You start with a package declaration:
package Deck; # at the top of the file
Then your sub deck already works well to create one, so to turn it into new, you have to rename it (obviously), fetch the package name in the arguments, and bless the reference (make it an object of type/class "Deck"):

my $class = shift; # at the top of the function return bless \@deck, $class; # at the bottom
Now you can create your deck with my $deck = Deck->new();

I told you that $object->method(args...) is turned into method($object, args). So your function deal can already be used in an OO way, simply turn deal($deck, $cards, $hands); into $deck->($cards, $hands);

Now List::Util::shuffle(list) works on a list, not a reference to an array, so a little modification is required since $deck->shuffle() will be turned into shuffle($deck);. Below is one way you could write that. I have written it so that list context (eg: @shuffled = $deck->shuffle()) will return a new list and leave $deck as is, but scalar or void context (eg: ; $deck->shuffle();) will shuffle $deck itself.

use List::util; # use the module but do not import shuffle # ... sub shuffle { my $deck = shift; my $out = wantarray ? [] : $deck; @$out = List::Util::shuffle(@$deck); return wantarray ? @$out : $out; }
Note that since the deck itself is returned in scalar context, you can call other methods on it. So $deck->shuffle()->deal($hands, $cards); would work. Or even Deck->new()->shuffle()->deal(); (Though right now this is a bit useless because shuffle is called again in deal).

Edit: for writing code like $object->(ARG1 => value, ARG2 => value); you can start your function with my ($obj, %params) = @_; and use $params{ARG1} and $params{ARG2};

Edit2: s/the secret to \KOOP/perl's basic OOP/

Replies are listed 'Best First'.
Re^2: Let's play poker
by reisinge (Hermit) on Jun 09, 2016 at 12:50 UTC

    Eily, that's a cool hint :-)! Here goes the OOP version (2 files - the module and the script):

    # Deck.pm package Deck; use 5.014; use warnings; use charnames ':full'; use List::Util; sub new { my $class = shift; my $n_cards = 52; my @suit = ( "\N{BLACK HEART SUIT}", "\N{BLACK SPADE SUIT}", "\N{BLACK DIAMOND SUIT}", "\N{BLACK CLUB SUIT}", ); my @rank = ((2 .. 10), qw(J Q K A)); my @deck; my $i = 0; while (@deck < $n_cards) { for my $s (@suit) { for my $r (@rank) { $deck[$i++] = "$r$s"; } } } return bless \@deck, $class; } sub shuffle { my $deck = shift; # list context (eg: @shuffled = $deck->shuffle()) will return a n +ew list # and leave $deck as is, but scalar or void context (eg: ; # $deck->shuffle();) will shuffle $deck itself my $out = wantarray ? [] : $deck; @$out = List::Util::shuffle(@$deck); return wantarray ? @$out : $out; } sub deal { my $deck = shift; my $args = shift; my $n_cards = $args->{cards} // 5; my $hands = $args->{hands} // 1; for (1 .. $hands) { my @hand; for (1 .. $n_cards) { die "no more cards in deck ...\n" unless @$deck; push @hand, shift @$deck; } binmode STDOUT, ':utf8'; say(join " ", @hand); } } 1; # ocards use 5.014; use warnings; use Deck; use Getopt::Long; use Pod::Usage; GetOptions( "h|?|help" => \( my $help ), "hands=i" => \( my $hands ), "cards=i" => \( my $cards ), ) or pod2usage(1); pod2usage( -exitval => 0, -verbose => 2, -noperldoc => 1 ) if $help; my $deck = Deck->new(); $deck->shuffle(); $deck->deal({cards => $cards, hands => $hands}); __END__ =head1 NAME ocards - deal cards from deck of 52 cards (OOP version of cards) =head1 SYNOPSIS ocards [options] options: -h, -?, --help brief help message --hands N number of hands to deal [1] --cards N cards per hand [5] =cut

    EDIT: Fixed my $hands = $args->{hands} instead of my $hands = $args->{cards}. Eily has sharp eyes, huh ... ;-)

    The best optimization strategy is to steal from what smarter people have already done. -- brian d foy