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

definestrings.pl:

#!/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:
PERL - 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.

CharacterDescription
\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
\bBackspace
\aBell
\eEscapes the next character
\0nnCreates Octal formatted numbers
\xnnCreates Hexideciamal formatted numbers
\cXControl characters, x may be any character
\QDo not match the pattern
\EEnds \U, \L, or \Q functions
formattingcharacters.pl:

#!/usr/bin/perl

print "content-type: text/html \n\n"; #HTTP HEADER

# STRINGS TO BE FORMATTED
$mystring = "welcome to corejava.com!"; #String to be formatted
$newline = "welcome to \ncorejava.com!";
$capital = "\uwelcome to corejava.com!";
$ALLCAPS = "\Uwelcome to corejava.com!";

# PRINT THE NEWLY FORMATTED STRINGS
print $mystring."<br />";
print $newline."<br />";
print $capital."<br />";
print $ALLCAPS;

Any combination of these special characters can be used at any time to properly punctuate your strings. They also come in handy when printing out HTML with your PERL functions.

PERL - Substr() and String Indexing

The substr() function is a rather complicated function. It can be used to do many things and we'll start with the most basic, grabbing a substring and move onto more advanced ideas further on.

To use substr() to grab a substring, you need to give it both a string variable to pick something out of and an offset (which starts at 0). A string can be thought of as an array of characters, starting with element 0 at the beginning and +1 for each additional character. The string "hey" has 3 characters. The 0th element is "h", the 1st element is "e" and the 2nd and last element is "y".

The first argument of substr() is the string we want to take something from and the second argument is the offset, or where we want to start at.

stringreplace.pl:

#!/usr/bin/perl

print "content-type: text/html \n\n"; #HTTP HEADER

# DEFINE A STRING TO REPLACE
$mystring = "Hello, am I about to be manipulated?!";

# PRINT THE ORIGINAL STRING
print "Original String: $mystring<br />";

# STORE A SUB STRING OF $mystring, OFFSET OF 7
$substringoffset = substr($mystring, 7);
print "Offset of 7: $substringoffset<br />";
Display:

Original String: Hello, am I about to be manipulated?!
Offset of 7: am I about to be manipulated?!

substr() started at the 7th element (remember we count from 0) which was the "a" in "am" and returned the rest of the string and we stored it into $substringoffset. Play around with this function a little and get a feel for how offset works!

Below we have gone on to the more advanced options of substr(), taking advantage of the last two arguments of the function: length and replace value. Rather than grabbing the whole string from the offset, we can just grab a chunk of it by specifying the length we want this substring to be.

The final argument, replace value, replaces the substring specified by the first three arguments with whatever we want. Let's change the original string to say something different by grabbing a part of the string and replacing it with "I want".

stringreplace.pl:

#!/usr/bin/perl

print "content-type: text/html \n\n"; #HTTP HEADER
# DEFINE A STRING TO REPLACE
$mystring = "Hello, am I about to be manipulated?!";

# PRINT THE ORIGINAL STRING
print "Original String: $mystring<br />";

# STORE A SUB STRING OF $mystring, OFFSET OF 7 AND LENGTH 10
$suboffsetANDlength = substr($mystring, 7, 10);
print "Offset of 7 and length of 10: $suboffsetANDlength<br />";

# CHANGE $mystring, OFFSET OF 7 AND LENGTH 10 AND 
# REPLACE SUB STR WITH "I want"
$suboffsetANDlength = substr($mystring, 7, 10, "I want");
print "mystring is now: $mystring<br />";
Display:

Original String: Hello, am I about to be manipulated?!
Offset of 7 and length of 10: am I about
mystring is now: Hello, I want to be manipulated?!

The original string was changed, so be careful when using the replace value argument of substr(). However, it's a gre

Previous                                                                                                                                                       Next

Back to Top