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

$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 "$numbern";
print "$exponentn";
print "$stringn";
print "$floatn";
print "There is nothing: $nothingn";

This will give following result
2 ** 8
Hello, PERL!
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.
LTransform all letters to lowercase
lTransform the next letter to lowercase
UTransform all letters to uppercase
uTransform the next letter to uppercase
nBegin on a new line
rApplys a carriage return
tApplys a tab to the string
fApplys a formfedd to the string
eEscapes the next character
nnCreates Octal formatted numbers
xnnCreates Hexideciamal formatted numbers
cXControl characters, x may be any character
QBackslash (escape) all following non-alphanumeric characters.
EEnds U, L, or Q functions
Here are few examples

$newline = "Welcome to!";
$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 "$newlinen";
print "$smalln";
print "$ALLCAPSn";
print "$PARTAILCAPSn";
print "$backslahn";

This will give following result
Welcome to!
This, Here t 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.

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

print "Before replacement : $mystringn";

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

print "After replacement : $mystringn";

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.
$mystring = "Hello, PERL!";

print "Before replacement : $mystringn";

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

print "After replacement : $mystringn";

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
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


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.;

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 u
sing these bases.Perl converts the literal representation into a decimal internally.

No comments:

Post a Comment