Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Scalar variables (use my)

  • my

  • scalar variables

  • Scalar variables always start with a $ sign, name is alphanumeric (a-zA-Z0-9) and underscore (_)

  • A scalar variable can hold a string, a number, a reference to another data-structure, undef

  • Value assignment to variable is done by the = sign

  • Use the my keyword to declare variables (optional but recommended)

$this_is_a_long_scalar_variable
$ThisIsAlsoGoodButWeUseItLessInPerl
$h
$H             # $h and $H are two different variables
#!/usr/bin/perl
use strict;
use warnings;

my $greeting   = "Hello world\n";
my $the_answer = 42;
my $name;                   # undef
print $greeting;
print $the_answer, "\n";

$name = 'Foo Bar';
print $name, "\n";

$the_answer = "Hi, you two";

The value can be changed any time.

Scalar variables