package Control; use strict; use vars qw/$AUTOLOAD/; sub new { my $class = shift; my $self = { @_ }; bless $self, $class; } sub _do_code { my $self = shift; my ($code, $context) = @_; if (ref $code) { local $_ = $context if $context; return &$code; } else { local $_ = $context if $context; my $rv = eval $code; die $@ if $@; return $rv; } } sub AUTOLOAD { my $self = shift; my $name = $AUTOLOAD; $name =~ s/.*:://; my $rv = $self->{$name}; if (@_) { $self->{$name} = shift; } return $rv; } package Foreach; use strict; use vars qw/@ISA/; use Control; @ISA = qw/Control/; sub run { my $self = shift; foreach (@{ $self->{array} }) { $self->_do_code($self->{body}, $_); } } package If; use strict; use vars qw/@ISA/; use Control; @ISA = qw/Control/; sub run { my $self = shift; if ( $self->_do_code( $self->{condition} ) ) { $self->_do_code( $self->{body}, $_ ); } else { my $done; ELSIF: foreach my $elsif ( @{ $self->{elsifs} } ) { if ( $self->_do_code( $elsif->{condition} ) ) { $self->_do_code( $elsif->{body}, $_ ); $done++; last ELSIF; } } if ($self->{else} and not $done) { $self->_do_code($self->{else}, $_ ); } } } sub elsif { my $self = shift; my $elsif = { @_ }; push @{ $self->{elsifs} }, $elsif; } 1; =head1 Name Control - OO control structures =head1 Synopsis #!/usr/bin/perl -w use Control; my @bar = (1,2,3,"dog"); my $loop = new Foreach ( body => 'print $_ . "\n"', array => \@bar ); $loop->run; $loop->{array} = [3,4,5,"cat"]; $loop->run; $loop->{body} = sub { print "The length of $_ is " . (length $_) . "\ +n";}; $loop->run; my $if = new If ( condition => '$_ % 2', body => sub { print "$_ is an odd number\n" } ); for ( 0 .. 10 ) { $if->run; } $if->elsif ( condition => 'not $_ % 2', body => 'print "$_ is an even number\n"' ); for (11 .. 20) { $if->run; } =cut

In reply to OO Control loops by dash2

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.