Perl Topics





Español Français 中文 Deutsch Portuguese Japanese nederlands
   
 
Perl Tutorial
Introduction Introduction
Installing Perl Installing Perl
Basics of Perl Basics of Perl
Scalar Variables Scalar Variables
Operators Operators
Control Structures Control Structures
Lists Lists
Array Array
Arrays Manipulation Arrays Manipulation
Arrays Functons Array Functions
Hash Hash
Hash Functions Hash Functions
String Functions String Functions
Regular Expression Regular Expression
Regular Expression Functions Regular Expression Functions
Numerical Functions Numerical Functions
List Functions List Functions
User Defined Function User Defined Function
File Handling File Handling
Forums Ask Your Doubts
Feedback Feedback
 






outsourced web hosting support

Scalar Variable in Perl


Tutorials Perl

Topic

What are the Scalar Variables?
How to do string Concatenation and Variable Substitution?


Explanation

Scalar Variable is a simple data that can be manipulated very easily by Perl. Numbers and Strings are basic scalar variables.

Number:

In Perl, integers can be expressed in decimal (base 10), hexadecimal (base 16) or octal (base 8) notation. Octal numbers are preceded by a 0 (zero).Perl internally considers integer as a double precision floating point value. Integer cannot be delimited by comma instead Perl allows underscore to seperate inetegers.

Example:
    #! C:\programfiles\perl\bin\perl
    print "content-type: text/html\n\n";
    #floating-point values
    $x = 4.15;
    $y = -7.78;
    #integer values
    $a = 4000;
    $b = -20;
    print "$x\n";
    print $a;
Result:
    4.15 4000

The number can also accept a string ie., $a=20 is similar to $a="20".

String:

The string defines a sequence of characters which can be defined in a single or double quotes.But when using Double quotes one has to carefully use the escape character "\" to get the correct display.

Example:
    #! C:\programfiles\perl\bin\perl
    print "content-type: text/html\n\n";
    print "Sam says, "Give me $200".";
Result:
    Server Error!

In the above example "$" is used for variable substitution and also as a symbol.Perl will assume that the string ends at the second double quotes. To get the correct display the string has to be escaped as below.

Example:
    #! C:\programfiles\perl\bin\perl
    print "content-type: text/html\n\n";
    print "Sam says, \"Give me \$200\".";
Result:
    Sam says, "Give me $200"

If single quotes is used no need to escape a string as below.

Example:
    #! C:\programfiles\perl\bin\perl
    print "content-type: text/html\n\n";
    print 'Sam says, "Give me $200".';
Result:
    Sam says, "Give me $200".
String Concatenation:

In perl strings can be concatenated using the concatenation operator ".", but if the string contains quotes, carriage returns, backslashes, all these special characters need to be escaped with a backslash.

Example:
    #! C:\programfiles\perl\bin\perl
    print "content-type: text/html\n\n";
    $a = "Hiox, India\'s";
    $c = "Hosting Company";
    $concat = $a ." leading Web". $c;
    print $concat;
Result:
    Hiox, India's leading WebHosting Company

In the above example the strings "$a", "$b","$c" is concatenated using the "." operator.

Variable Substitution:

Variable substitution means that variables within a double-quoted string will be substituted by their values at the time the statement is evaluated.This is not allowed when using a single quote, but when double quotes is used variable substitution is accepted.

Example:
    #! C:\programfiles\perl\bin\perl
    print "content-type: text/html\n\n";
    $cel=40;
    $fah = ($cel * 1.8) + 32;
    print "The $cel degrees Celsius is $fah fahrenheit \n";
    print 'The $cel degrees Celsius is $fah fahrenheit ';
Result:
    The 40 degrees Celsius is 104 fahrenheit
    The $cel degrees Celsius is $fah fahrenheit

In the above example first the value of the variables "$cel","$fah" are substituted as its given between double quotes. In the second print statement the string is given within single quotes so "$cel","$fah" is printed as it is.








A Note
Simple introduction, basic CGI perl programming codes with examples. Do send your feedback or suggestions on this tutorial. This is a copyright content.

Other Links

web hosting