Tuesday, 16 July 2013

scalar variables an defining scalar variables in perl programming

Scalar Variables

Scalar variables can be either a number or a string -- What might seem confusing at first sight actually makes a lot of sense and can make programming a lot easier.
  • You can use variable types almost interchangeably. Numbers first then strings later
  • Numbers are numbers -- there is no integer type per se. Perl regards all numbers a floating point numbers for calculations etc.

Defining Scalar Variables

You define scalar variables by assigning a value (number or string) to it.
  • You do not declare variable types at a special place in the program as in PASCAL.
  • It is a good idea to declare all variables together near the top of the program.
The following are simple examples (var1.pl) of variable declarations:
$first_name = "David";
$last_name = "Marshall";

$number = 3;

$another_number = 1.25;

$sci_number = 7.25e25;

$octal_number = 0377; # same as 255 decimal

$hex_number = 0xff; # same as 255 decimal
NOTE:
  • All references to scalar variables need a $.
  • Perl commands end with a semicolon (;). This can be omitted from last lines of ``blocks'' of statements like PASCAL.
  • All standard number formats are supported integer, float and scientific literal values are supported.
  • Hexadecimal and Octal number formats are supported by 0x and 0 prefix.

0 comments:

Post a Comment