Modules and Packages
Package Variables
our declares a package variable that package subroutines can share.
package variable
A package variable belongs to a package namespace and can be referenced with a qualified name.
Package Variables
package_variables.pl
Replay: real traced execution (multi-file project)
use strict;
use warnings;
package Counter;
our $total = 0;
sub add {
my ($amount) = @_;
$total = $total + $amount;
return $total;
}
package main;
my $amount = 4;
my $first = Counter::add($amount);
my $second = Counter::add(2);
print "amount=$amount\n";
print "first=$first\n";
print "second=$second\n";
use strict;
use warnings;
package Counter;
our $total = 0;
sub add {
my ($amount) = @_;
$total = $total + $amount;
return $total;
}
package main;
my $amount = 1;
my $first = Counter::add($amount);
my $second = Counter::add(2);
print "amount=$amount\n";
print "first=$first\n";
print "second=$second\n";
use strict;
use warnings;
package Counter;
our $total = 0;
sub add {
my ($amount) = @_;
$total = $total + $amount;
return $total;
}
package main;
my $amount = 7;
my $first = Counter::add($amount);
my $second = Counter::add(2);
print "amount=$amount\n";
print "first=$first\n";
print "second=$second\n";
$total ← 0, $amount ← 4
6our $total→ 0 = 0;78sub add {9 my ($amount) = @_;10 $total = $total + $amount;11 return $total;12}1314package main;1516my $amount→ 4 = 4; #@amount=1, 717my $first = Counter::add($amount4);18my $second = Counter::add(2);$amount ← 4, $total ← 4
pass 1 of 28sub add {9 my ($amount→ 4) = @_;10 $total→ 4 = $total + $amount4;11 return $total4;12}$first ← 4
16my $amount = 4; #@amount=1, 717my $first→ 4 = Counter::add($amount4);18my $second = Counter::add(2);$amount ← 2, $total ← 6
pass 2 of 28sub add {9 my ($amount→ 2) = @_;10 $total→ 6 = $total + $amount2;11 return $total6;12}$second ← 6
17my $first = Counter::add($amount);18my $second→ 6 = Counter::add(2);1920print "amount=$amount4\n";21print "first=$first4\n";22print "second=$second6\n";outputamount=4 first=4 second=6
$total ← 0, $amount ← 1
6our $total→ 0 = 0;78sub add {9 my ($amount) = @_;10 $total = $total + $amount;11 return $total;12}1314package main;1516my $amount→ 1 = 1;17my $first = Counter::add($amount1);18my $second = Counter::add(2);$amount ← 1, $total ← 1
pass 1 of 28sub add {9 my ($amount→ 1) = @_;10 $total→ 1 = $total + $amount1;11 return $total1;12}$first ← 1
16my $amount = 1;17my $first→ 1 = Counter::add($amount1);18my $second = Counter::add(2);$amount ← 2, $total ← 3
pass 2 of 28sub add {9 my ($amount→ 2) = @_;10 $total→ 3 = $total + $amount2;11 return $total3;12}$second ← 3
17my $first = Counter::add($amount);18my $second→ 3 = Counter::add(2);1920print "amount=$amount1\n";21print "first=$first1\n";22print "second=$second3\n";outputamount=1 first=1 second=3
$total ← 0, $amount ← 7
6our $total→ 0 = 0;78sub add {9 my ($amount) = @_;10 $total = $total + $amount;11 return $total;12}1314package main;1516my $amount→ 7 = 7;17my $first = Counter::add($amount7);18my $second = Counter::add(2);$amount ← 7, $total ← 7
pass 1 of 28sub add {9 my ($amount→ 7) = @_;10 $total→ 7 = $total + $amount7;11 return $total7;12}$first ← 7
16my $amount = 7;17my $first→ 7 = Counter::add($amount7);18my $second = Counter::add(2);$amount ← 2, $total ← 9
pass 2 of 28sub add {9 my ($amount→ 2) = @_;10 $total→ 9 = $total + $amount2;11 return $total9;12}$second ← 9
17my $first = Counter::add($amount);18my $second→ 9 = Counter::add(2);1920print "amount=$amount7\n";21print "first=$first7\n";22print "second=$second9\n";outputamount=7 first=7 second=9