| 1 | package Acme::Magic8Ball; | ||||||||||||
| 2 | |||||||||||||
| 3 | 3 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | |||||||||||||
| 5 | 3 | require Exporter; | |||||||||||
| 6 | 3 | ▶ | use vars qw($VERSION $CONSISTENT @EXPORT_OK @ISA); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | |||||||||||||
| 8 | |||||||||||||
| 9 | |||||||||||||
| 10 | # are we ever going to need enhancements? Apparently yes :( | ||||||||||||
| 11 | 3 | $VERSION = "1.3"; | |||||||||||
| 12 | 3 | $CONSISTENT = 0; | |||||||||||
| 13 | 3 | @ISA = qw(Exporter); | |||||||||||
| 14 | 3 | @EXPORT_OK = qw(ask); | |||||||||||
| 15 | |||||||||||||
| 16 | 3 | ▶ | use Data::Dumper; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 17 | |||||||||||||
| 18 | 3 | ▶ | sub import { | ||||||||||
|
Subroutine
import: called CC=1 SCAR=0.0
| |||||||||||||
| 19 | 3 | $CONSISTENT = grep { /^:consistent$/ } @_; | |||||||||||
| 20 | 3 | @_ = grep { !/^:consistent$/ } @_; | |||||||||||
| 21 | 3 | goto &Exporter::import; | |||||||||||
| 22 | } | ||||||||||||
| 23 | |||||||||||||
| 24 | =head1 NAME | ||||||||||||
| 25 | |||||||||||||
| 26 | Acme::Magic8Ball - ask the Magic 8 Ball a question | ||||||||||||
| 27 | |||||||||||||
| 28 | =head1 SYNOPSIS | ||||||||||||
| 29 | |||||||||||||
| 30 | use Acme::Magic8Ball qw(ask); | ||||||||||||
| 31 | my $reply = ask("Is this module any use whatsoever?"); | ||||||||||||
| 32 | |||||||||||||
| 33 | ... you can also pass in your own list of answers ... | ||||||||||||
| 34 | |||||||||||||
| 35 | my $reply = ask("What should the next bit be?", 0, 1); # reply will always be 0 or 1 | ||||||||||||
| 36 | |||||||||||||
| 37 | ... or make answers consistent ... | ||||||||||||
| 38 | |||||||||||||
| 39 | use Acme::Magic8Ball qw(ask :consistent); | ||||||||||||
| 40 | for (0..1000) { | ||||||||||||
| 41 | my $reply = ask("Is this module any use whatsoever?"); # reply will always be the same | ||||||||||||
| 42 | } | ||||||||||||
| 43 | |||||||||||||
| 44 | =head1 DESCRIPTION | ||||||||||||
| 45 | |||||||||||||
| 46 | This is an almost utterly pointless module. But I needed it. So there. | ||||||||||||
| 47 | |||||||||||||
| 48 | =head1 METHODS | ||||||||||||
| 49 | |||||||||||||
| 50 | =head2 ask <question> [answers] | ||||||||||||
| 51 | |||||||||||||
| 52 | Ask and ye shall receive! | ||||||||||||
| 53 | |||||||||||||
| 54 | If you don't pass in an array of answers it will use the traditional ones. | ||||||||||||
| 55 | |||||||||||||
| 56 | =cut | ||||||||||||
| 57 | |||||||||||||
| 58 | 1006 | ▶ | sub ask { | ||||||||||
|
Subroutine
ask: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||
| 59 | 1006 | ▶ | my $question = shift || return "You must ask a question!"; | ||||||||||
|
Condition100%2/2
shift() || (return 'You must ask a question!')
MC/DC100%1/1
shift() || (return 'You must ask a question!')
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||
| 60 | 1005 | my @answers = @_; | |||||||||||
| 61 | |||||||||||||
| 62 | 1005 | ▶ | unless (@answers) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 63 | 1004 | my $pos = tell DATA; | |||||||||||
| 64 | 1004 | @answers = map { chomp; $_ } <DATA>; | |||||||||||
| 65 | 1004 | seek DATA, $pos,0; | |||||||||||
| 66 | } | ||||||||||||
| 67 | 1005 | ▶ | return $answers[rand @answers] unless $CONSISTENT; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 68 | |||||||||||||
| 69 | 1002 | my $hashcode = 0; | |||||||||||
| 70 | 1002 | $hashcode += ord($_) foreach split(//, $question); | |||||||||||
| 71 | 1002 | return $answers[$hashcode % scalar(@answers) - 1]; | |||||||||||
| 72 | } | ||||||||||||
| 73 | |||||||||||||
| 74 | =head1 AUTHOR | ||||||||||||
| 75 | |||||||||||||
| 76 | Simon Wistow <simon@thegestalt.org> | ||||||||||||
| 77 | |||||||||||||
| 78 | =head1 COPYING | ||||||||||||
| 79 | |||||||||||||
| 80 | Copyright 2005, Simon Wistow | ||||||||||||
| 81 | |||||||||||||
| 82 | Distributed under the same terms as Perl itself. | ||||||||||||
| 83 | |||||||||||||
| 84 | =head1 DEVELOPMENT | ||||||||||||
| 85 | |||||||||||||
| 86 | You can get the latest version from | ||||||||||||
| 87 | |||||||||||||
| 88 | https://github.com/simonwistow/Acme-Magic8Ball | ||||||||||||
| 89 | |||||||||||||
| 90 | =head1 SEE ALSO | ||||||||||||
| 91 | |||||||||||||
| 92 | The 8 Ball FAQ - http://8ball.ofb.net/faq.html | ||||||||||||
| 93 | |||||||||||||
| 94 | Mattel (who own the 8 Ball) - http://www.mattel.com | ||||||||||||
| 95 | |||||||||||||
| 96 | =cut | ||||||||||||
| 97 | |||||||||||||
| 98 | |||||||||||||
| 99 | |||||||||||||
| 100 | |||||||||||||
| 101 | __DATA__ | ||||||||||||