The simplest form of variable in Perl is the scalar. A scalar is a single item of data such as:
Arthur
Just Another Perl Hacker
42
0.000001
3.27e17
Here's how we assign values to scalar variables:
my $name = "Arthur"; my $whoami = 'Just Another Perl Hacker'; my $meaning_of_life = 42; my $number_less_than_1 = 0.000001; my $very_large_number = 3.27e17; # 3.27 by 10 to the power of 17 |
There are other ways to assign things apart from the = operator, too. They're covered on pages 92-93 of the Camel.
As you can see, a scalar can be text of any length, and numbers of any precision (machine dependent, of course). Perl magically converts between them when it needs to. For instance, it's quite legal to say:
# adding an integer to a floating point number my $sum = $meaning_of_life + $number_less_than_1; # here we're putting the int in the middle of a string we # want to print print "$name says, 'The meaning of life is $meaning_of_life.'\n"; |
This may seem extraordinarily alien to those used to strictly typed languages, but believe it or not, the ability to transparently convert between variable types is one of the great strengths of Perl. Some people say that it's also one of the great weaknesses.
You can explicitly cast scalars to various specific data types. Look up int() on page 180 of the camel, for instance.