Our Odd Numbers code in Perl.
Odd Numbers Class File:
package ODDNUMBERS;
BEGIN {
require Exporter;
# for the sake of standard
our $VERSION = 2016.12;
# Inherit from exporter to export functions and variables
our @ISA = qw(Exporter);
# Functions and variables to be exported by default
our @EXPORT_OK = qw(prepResult);
}
use warnings;
use strict;
my ($start, $stop);
my @result;
# simulate an object construct
# takes two arguments -- besides its name;
# start and stop values for the range
sub new {
my $this = shift;
my $parameters = {@_};
bless $parameters, $this;
$this->_init(@_);
return $this;
}
# simulate a constructor
sub _init {
my $self = shift;
($start, $stop) = @_;
}
# Returns an array reference of the desired set odd numbers
sub prepResult {
# Loop from start to stop and rip out odd numbers;
until ($start > $stop) {
if (($start % 2) != 0) { # modulo(%) is explained later
push @result, $start;
}
$start += 1; # increase start by 1
}
return \@result;
}
1;
Main Class:
#!/usr/bin/perl;
use strict;
use warnings;
use ODDNUMBERS;
# Useful variables
my ($lower_boundary, $upper_boundary, $answer);
# Use the odd number module/class
$lower_boundary = 1;
$upper_boundary = 100;
my $odd_list = ODDNUMBERS->new($lower_boundary, $upper_boundary);
$answer = $odd_list->prepResult();
print "Odd numbers between $lower_boundary and $upper_boundary are:\n@{$answer}\n";
print "\n\n";
Try it out!
Elegance (0.0)