5.2. Scalar variables (use my)

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

  • A scalar variable can hold either a string or a number

  • Value assignment to varaible 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

Example 5-1. examples/scalars/scalar_variables.pl

#!/usr/bin/perl
use strict;
use warnings;

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

A scalar can hold either string or numerical value. They can be changed any
time. If a value was not given it holds the special value 'undef'.

my $x;         # the value is a special value called 'undef'

If you are interested in on-site trainings by the author, please contact me directly.

Online courses:

Would you like to get
updated when I publish
the next article?

Follow me:

Google Plus Twitter RSS feed