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

In reply to Re^2: Let's play poker by reisinge
in thread Let's play poker by reisinge

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.