Program to illustrate the use of scalar variables. : Scalar « Language Basics « Perl






Program to illustrate the use of scalar variables.

   


#!perl

$a = 5;
print "The value of variable a is: $a\n";

$a = $a + 5;
print "Variable a after adding 5 is: $a\n";

$a *= 2;
print "Variable a after multiplying by 2 is: $a\n";

# using an uninitialized variable in the context of a string
print "Using a variable before initializing: $var\n";

# using an uninitialized variable in a numeric context
$test = $num + 5;
print "Adding uninitialized variable \$num to 5 yields: $test.\n";

# using strings in numeric contexts
$str = "A string value";
$a = $a + $str;
print "Adding a string to an integer yields: $a\n";

$strnum = "15charactersand1";
$c = $a + $strnum;
print "Adding $a to string \"$strnum\" yields: $c\n";

   
    
    
  








Related examples in the same category

1.Constant scalar
2.Convert to scalar
3.Creating a Scalar
4.Declare scalar variable to store the integer value
5.Naming Scalar Variables
6.scalar value interpolation
7.Scalars (Denoted by $)
8.Simple calculation with scalar variable
9.A scalar is a variable that holds a single value, a single string, or a number.
10.A scalar variable can reference a string value or a numeric value.
11.Assign new value to the integer scalar variable
12.Compound assignment operator with scalar variable
13.Concatenate two scalar variables with double quotes
14.$data is a scalar variable, while @data is an array
15.Initializing scalars and printing their values
16.Perl supports integers (decimal, octal, hexadecimal), floating point numbers, scientific notation, Booleans, and null.
17.Merge scalar variable into the string for output
18.If there are no quotes, then Perl has to decide whether the value is a string or a numeric value.
19.Scalar variables hold a single number or string and are preceded by a dollar sign ($).
20.Curly Braces