Copyright © tutorialspoint.com

# PERL Scalar Variable

Scalar variables are simple variables containing only one element--a string or a number. Strings may contain any symbol, letter, or number. Numbers may contain exponents, integers, or decimal values. The bottom line here with scalar variables is that they contain only one single piece of data. What you see is what you get with scalar variables.

Following are examples of Scalar Variables

    #!/usr/bin/perl

    $number = "5";
    $exponent = "2 ** 8";
    $string = "Hello, PERL!";
    $float = 12.39;

    # We can also assign a scalar an empty (undefined) value:
    $nothing = undef;

    # Printing all the above values
    print "$number\n";
    print "$exponent\n";
    print "$string\n";
    print "$float\n";
    print "There is nothing: $nothing\n";

    This will give following result
    5
    2 ** 8
    Hello, PERL!
    12.39
    There is nothing:

# Scalar Strings

Strings are scalar as we mentioned previously. There is no limit to the size of the string, any amount of characters, symbols, or words can make up your strings.

When defining a string you may use single or double quotations, you may also define them with the q subfunction. Following are examples of how to define strings

$single = 'This string is single quoted'; $double = "This string is double quoted"; $userdefined = q^Carrot is now our quote^;

# Formatting Strings w/ Formatting Characters

Strings can be formatted to your liking using formatting characters. Some of these characters also work to format files created in PERL. Think of these characters as miniature functions.

Character | Description
\L | Transform all letters to lowercase
\l | Transform the next letter to lowercase
\U | Transform all letters to uppercase
\u | Transform the next letter to uppercase
\n | Begin on a new line
\r | Applys a carriage return
\t | Applys a tab to the string
\f | Applys a formfedd to the string
\b | Backspace
\a | Bell
\e | Escapes the next character
\0nn | Creates Octal formatted numbers
\xnn | Creates Hexideciamal formatted numbers
\cX | Control characters, x may be any character
\Q | Backslash (escape) all following non-alphanumeric characters.
\E | Ends \U, \L, or \Q functions

Here are few examples

    #!/usr/bin/perl

    $newline = "Welcome to \ntutorialspoint.com!";
    $small = "\uthis, Here t will become capital !"
    $ALLCAPS = "\UThis completely will become capital!";
    $PARTIALCAPS = "\UThis half will/E become capital!";
    $backslah = "\Q'Hello World'\E!";

    print "$newline\n";
    print "$small\n";
    print "$ALLCAPS\n";
    print "$PARTAILCAPS\n";
    print "$backslah\n";

    This will give following result
    Welcome to
    tutorialspoint.com!
    This, Here t will become capital !
    THIS COMPLETELY WILL BECOME CAPITAL!
    THIS HALF WILL become capital!
    \'Hello World\'!

# Substr() and String Indexing

The substr() function allows for the temporary replacement of characters in a string. We can change the string "Hello, PERL" to "Hello, World!" quite easily. Each character of the string is automatically assigned a numeric value by PERL, which means that we can index any of the characters in our strings with this number. PERL counts each character of the string beginning with 0 for the first character and continuing until it reaches the end of a string

Two arguments must be sent with our substr() function, the string you wish to index and the index number. If two arguments are sent, PERL assumes that you are replacing every character from that index number to the end.

    #!/usr/bin/perl

    # Define a string to replace
    $mystring = "Hello, PERL!";

    print "Before replacement : $mystring\n";

    substr($mystring, 7) = "World!";

    print "After replacement : $mystring\n";

    This will give following result
    Before replacement : Hello, PERL!
    After replacement : Hello, World!

Because we only specified one numeric parameter for the string, PERL assumed we wanted to replace every character after the 7th, with our new string. If we throw a third parameter in our function we can replace only a chunk of our string with a new string.

    #!/usr/bin/perl
    $mystring = "Hello, PERL!";

    print "Before replacement : $mystring\n";

    substr($mystring, 3, 6) = "World!";

    print "After replacement : $mystring\n";

    This will give following result
    Before replacement : Hello, PERL!
    After replacement : HelWorld!RL!

# Multiline Strings

If you want to introduce multiline strings into your programs, you can use standard quotes:

    $string = 'This is
    a multiline
    string';

But this is messy and is subject to the same basic laws regarding interpolation and quote usage. We could get around it using the q// or qq// operator with different delimiters.

Another better way of printing multilines

    print <<EOF;
    This is
    a multiline
    string
    EOF

# V-Strings

V-strings can be a useful way of introducing version numbers and IP addresses into Perl. They are any literal that begins with a v and is followed by one or more dot-separated elements. For example:

$name = v77.97.114.116.105.110;

# Numeric Scalars

Perl supports a number of a fairly basic methods for specifying a numeric literal in decimal:

    $num = 123;      # integer
    $num = 123.45;   # floating point
    $num = 1.23e45;  # scientific notation

You can also specify literals in hexadecimal, octal, and binary by specifying a preceding character to highlight the number types:

    $num = 0xff;        # Hexadecimal
    $num = 0377;        # Octal
    $num = 0b0010_0000; # Binary

| Note that the numbers are not stored internally using these bases.Perl converts the literal representation into a decimal internally.

Copyright © tutorialspoint.com