Foundations
Scalars
Scalars hold one value and can feed arithmetic expressions.
arithmetic
Perl uses familiar operators like `*` and `+` to calculate numeric values.
Scalars
scalars.pl
Replay: real traced execution (multi-file project)
use strict;
use warnings;
my $unit_price = 12;
my $quantity = 3;
my $total = $unit_price * $quantity;
print "unit=$unit_price\n";
print "total=$total\n";
use strict;
use warnings;
my $unit_price = 8;
my $quantity = 3;
my $total = $unit_price * $quantity;
print "unit=$unit_price\n";
print "total=$total\n";
use strict;
use warnings;
my $unit_price = 20;
my $quantity = 3;
my $total = $unit_price * $quantity;
print "unit=$unit_price\n";
print "total=$total\n";
$unit_price ← 12, $quantity ← 3, $total ← 36
4my $unit_price→ 12 = 12; #@unit_price=8, 205my $quantity→ 3 = 3;6my $total→ 36 = $unit_price12 * $quantity3;78print "unit=$unit_price12\n";9print "total=$total36\n";outputunit=12 total=36
$unit_price ← 8, $quantity ← 3, $total ← 24
4my $unit_price→ 8 = 8;5my $quantity→ 3 = 3;6my $total→ 24 = $unit_price8 * $quantity3;78print "unit=$unit_price8\n";9print "total=$total24\n";outputunit=8 total=24
$unit_price ← 20, $quantity ← 3, $total ← 60
4my $unit_price→ 20 = 20;5my $quantity→ 3 = 3;6my $total→ 60 = $unit_price20 * $quantity3;78print "unit=$unit_price20\n";9print "total=$total60\n";outputunit=20 total=60
What Happens
$unit_pricestarts at12.$quantitystarts at3.$total = $unit_price * $quantitymultiplies12 * 3.$totalbecomes36.- The program prints
unit=12andtotal=36.
Value Map
| unit_price | quantity | total | | --- | --- | --- | | 8 | 3 | 24 | | 12 | 3 | 36 | | 20 | 3 | 60 |
Try It
Exercise: scalars.pl
Reproduce unit=12 and total=36, then use the pinned unit_price values 8 and 20 to predict each total.