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
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 |
nn | 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 |
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
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 |
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 |
Multiline Strings
If you want to introduce multiline strings into your programs, you can use standard quotes:
$string = 'This is |
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; |
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 |
You can also specify literals in hexadecimal, octal, and binary by specifying a preceding character to highlight the number types:
$num = 0xff; # Hexadecimal |
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