sub sumline{         # Sums the values in the list
    eval ( join "+", @_ );
    # eval calls the perl interpreter on a string
    # Default return value is last value calculated
}

sub printHeader{     # Print header for i, i^p, ... , i^q
    my ($p, $q) = (shift, shift);
    print "i, i^";
    print  join ", i^", ($p .. $q);
    print "\n";
}

sub power{     # Calculate $n^$p
    die "not enough args to power" if $#_ < 1;
    my ($n, $p) = (shift, shift);
    die "invalid power" if $p < 0;
    $ans = 1;
    for  (1 .. $p){
        $ans = $ans * $n;
    }
    return $ans;
}

sub calcLine{  # calculate $n^$i for $i in $sP .. $fP
    # is there a better way?
    my ($n, $sP, $fP) = (shift @_, shift @_, shift @_);

    my (@powList) = ();        # Start with empty list
    for $i ($sP .. $fP){
        $pow = power($n, $i);
        push @powList, $pow;
    }

    return $n, @powList;
}

sub printLine{ # Print values in the line, with commas
    my (@line) = @_;  # Get a local copy - as an example
    print join(", ", @line), "\n";
}

sub printTable{  # Print the table
    my ($startI, $finishI, $startP, $finishP) = @_;
    my @powLine;   # A local variable

    printHeader $startP, $finishP;

    for $i ($startI .. $finishI){
        @powLine = calcLine $i, $startP, $finishP;
        $sum += sumline @powLine;
        printLine @powLine;
    }
}

printTable 2,5, 3,6;
print "\nSum of all values: $sum\n";
# $sum is global only as an example.
# Use of global variables is recommended only where absolutely needed.