#!/usr/bin/perl use strict; use warnings; package Range; sub new { my $class = shift; my $lower = shift; my $upper = shift; my $delta = shift || 0.00001; my $self = { lower => $lower, upper => $upper, delta => $delta, current => $lower, }; return bless $self, $class; } sub next { my $self = shift; if ($self->{current} + $self->{delta} < $self->{upper}) { $self->{current} += $self->{delta}; return 1; } else { return undef; } } package main; sub integrate { my $function = shift; my $iter = shift; my $sum; do { $sum += $function->($iter->{current}) * $iter->{delta}; } while ($iter->next()); return $sum; } print integrate(sub {(shift)**2}, new Range (2, 5)), "\n";