Something like this?
#!/usr/bin/perl use warnings; use strict; imperative(); procedural(); functional(); oo(); =head1 Various programming styles Pick a number between 1-10, and it tells you whether you guessed right + or not). =cut # IMPERATIVE ############################################ sub imperative { my $x = 1 + int rand 10; print 'Your guess? '; my $guess = <>; if ($guess == $x) { print "right\n"; } else { print "wrong\n"; } } # PROCEDURAL ############################################ sub procedural { my $x = generate(1, 10); my $guess = get_answer('Your guess?'); validate($x, $guess); } sub generate { my ($from, $to) = @_; return $from + int rand(1 + $to - $from); } sub get_answer { print @_, ' '; return scalar <>; } sub validate { my ($x, $guess) = @_; if ($guess == $x) { print "right\n"; } else { print "wrong\n"; } } # FUNCTIONAL ############################################ sub functional { validate(generate(1, 10), get_answer('Your guess?')); } # OO #################################################### { package Local::Guesser; sub new { bless {}, shift; } sub generate { my ($self, $from, $to) = @_; $self->{random} = $from + int rand(1 + $to - $from); } sub get_answer { my ($self, $prompt) = @_; print $prompt, ' '; $self->{answer} = <>; } sub validate { my $self = shift; if ($self->{random} == $self->{answer}) { print "right\n"; } else { print "wrong\n"; } } } sub oo { my $g = Local::Guesser->new; $g->generate(1, 10); $g->get_answer('Your guess?'); $g->validate; }

In reply to Re: Programming Examples: Declarative, Imperative, Functional, Etc. by choroba
in thread Programming Examples: Declarative, Imperative, Functional, Etc. by aaron_baugher

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.