Scalars hold one value and can feed arithmetic expressions.

arithmetic Perl uses familiar operators like `*` and `+` to calculate numeric values.

Scalars

unit_price
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";
  1. $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
  1. $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
  1. $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

  1. $unit_price starts at 12.
  2. $quantity starts at 3.
  3. $total = $unit_price * $quantity multiplies 12 * 3.
  4. $total becomes 36.
  5. The program prints unit=12 and total=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.