Package-Based Object Patterns
Package Calls
Pass a current value into a package routine and receive an updated value.
package-calls
A package routine can receive state, perform work, and return the result.
Package Calls
package_calls.pl
Replay: real traced execution (multi-file project)
use strict;
use warnings;
package Counter;
sub add {
my ($value, $step) = @_;
return $value + $step;
}
package main;
my $step = 3;
my $start = 10;
my $next = Counter::add($start, $step);
print "start=$start\n";
print "step=$step\n";
print "next=$next\n";
use strict;
use warnings;
package Counter;
sub add {
my ($value, $step) = @_;
return $value + $step;
}
package main;
my $step = 1;
my $start = 10;
my $next = Counter::add($start, $step);
print "start=$start\n";
print "step=$step\n";
print "next=$next\n";
use strict;
use warnings;
package Counter;
sub add {
my ($value, $step) = @_;
return $value + $step;
}
package main;
my $step = 5;
my $start = 10;
my $next = Counter::add($start, $step);
print "start=$start\n";
print "step=$step\n";
print "next=$next\n";
$step ← 3, $start ← 10
13my $step→ 3 = 3; #@step=1, 514my $start→ 10 = 10;15my $next = Counter::add($start10, $step3);$value ← 10, $step ← 3
6sub add {7 my ($value→ 10, $step→ 3) = @_;8 return $value10 + $step3;9}$next ← 13
14my $start = 10;15my $next→ 13 = Counter::add($start10, $step3);1617print "start=$start10\n";18print "step=$step3\n";19print "next=$next13\n";outputstart=10 step=3 next=13
$step ← 1, $start ← 10
13my $step→ 1 = 1;14my $start→ 10 = 10;15my $next = Counter::add($start10, $step1);$value ← 10, $step ← 1
6sub add {7 my ($value→ 10, $step→ 1) = @_;8 return $value10 + $step1;9}$next ← 11
14my $start = 10;15my $next→ 11 = Counter::add($start10, $step1);1617print "start=$start10\n";18print "step=$step1\n";19print "next=$next11\n";outputstart=10 step=1 next=11
$step ← 5, $start ← 10
13my $step→ 5 = 5;14my $start→ 10 = 10;15my $next = Counter::add($start10, $step5);$value ← 10, $step ← 5
6sub add {7 my ($value→ 10, $step→ 5) = @_;8 return $value10 + $step5;9}$next ← 15
14my $start = 10;15my $next→ 15 = Counter::add($start10, $step5);1617print "start=$start10\n";18print "step=$step5\n";19print "next=$next15\n";outputstart=10 step=5 next=15