#!/usr/bin/perl -w use strict; package LuckyDraw; use overload '<>' => sub { my $self = shift; splice @$self, rand @$self, 1; }; sub new { my $class = shift; bless [@_], $class; } #### package main; my $cards = LuckyDraw->new(1..52); for (1..5) { my $card = <$cards>; print name_card($card),$/; } my $alltrue = LuckyDraw->new(qw/foo bar baz/); print 'All True:', $/; print while <$alltrue>; print $/; my $onezero = LuckyDraw->new(0..9); print 'One Zero:', $/; print while <$onezero>; print $/; my $onefalse = LuckyDraw->new(0..9); print 'One False:', $/; $_+=0, print while <$onefalse>; print $/; my $onenil = LuckyDraw->new(undef,1..9); print 'One Undef:', $/; print while <$onenil>; print $/; print 'continuing...',$/; print while <$onenil>; print $/; my $shuffle = new LuckyDraw(1..52); print name_card($_),' ' for <$shuffle>; print $/; sub name_card { my $card = shift; sprintf "%s of %s", (qw/Ace Deuce Trey Four Five Six Seven Eight Nine Ten Jack Queen King/)[$card % 13], (qw/Clubs Diamonds Hearts Spades/)[$card / 13]; } #### package LuckyDeck; use overload '<>' => sub { my $self = shift; return splice( @$self, rand @$self, 1) unless wantarray; my @deck; push @deck, splice( @$self, rand @$self, 1) while @$self; @deck; }; sub new { my $class = shift; bless [@_], $class; } package main; my $quux = LuckyDeck->new(0..9); print 'List Context',$/; print for <$quux>; print $/;