Advertising (This ad goes away for registered users. You can Login or Register)

[Tutorial] Introduction to Perl's awesomeness (II)

Discuss about your favorite (gaming...or not) devices here. The most popular ones will end up getting their own categories
Programming discussions for your favorite Device
Forum rules
Forum rule Nº 15 is strictly enforced in this subforum.
Post Reply
User avatar
m0skit0
Guru
Posts: 3817
Joined: Mon Sep 27, 2010 6:01 pm

[Tutorial] Introduction to Perl's awesomeness (II)

Post by m0skit0 » Fri Nov 18, 2011 8:06 am

<< Previous | Next >>

Variables. Be strict with them!

Variables in Perl are defined by the "$" symbol before their name.

Code: Select all

#!/usr/bin/perl

$a = 5;
$b = 6;
$c = $a + $b;
print($c);
Yes, like PHP. PHP is heavily influenced by Perl's syntax.

Perl variables have no type. They can be used as string, as a number, or even as a boolean value (we'll look into this later).

Code: Select all

#!/usr/bin/perl

$a = "5";
$a++;
print($a);
Perl's flexibility allows you to not declare variables. That is, you can use varibales without declaring them, as you've seen on last examples. But even if Perl allows you to do so, I won't recommend you abusing this feature, specially when you have to use more than 2-3 variables in your script. Declaring the variables can save you a lot of headaches of useless searching for variables typos you've made. To enforce variable declaration, you include

Code: Select all

use strict;
at the beginning of your Perl script. This will intruct the Perl interpreter to raise an error whenever it founds a variable that has not been declared. To declare any kind of variable, you use the "my" keyword.

Code: Select all

#!/usr/bin/perl

use strict;

my $a = "5";
$a++;
print($a);
The variables I've shown so far (denoted by the $ character) are called "scalars". They're called scalars because they're comparable and can only hold one value.

Strings can be declared using single (') or double (") quotes. This is not only a matter of taste: it affects the variable resolving inside a string. Double-quoted string do resolve variables and sigle-quoted string do not. But what's that variable resolving thing? I think it's better shown in an example:

Code: Select all

#!/usr/bin/perl

use strict;

my $a = "5";
my $b = "$a";
my $c = '$a';
print($b);
print("\n");
print($c);
As you can see, $b actually got $a value and not the "$a" string. This is because before assigning the value, all variables and special characters (like \n for example) inside the double quotes are substituted by their value (aka they're parsed). On the other hand, single quotes indicate that no substitution should be done, and the string should be taken literally. Here's another example:

Code: Select all

#!/usr/bin/perl

use strict;

my $a = 5;
my $b = "A dang string";
my $c = '$a\n$b';
my $d = "$a\n$b";
print("$a $b $c $d\n");
Native operators for numbers are basically the same than on C:

Code: Select all

+	Addition
-	Substraction
*	Multiplication
/	Division
%	Modulo
**	Power
For strings you get the concatenation and repeat operators:

Code: Select all

.	Concatenate
x	Repeat
that work like

Code: Select all

#!/usr/bin/perl

use strict;

my $a = "Hello ";
my $b = "world\n";
print($a . $b);
print($a x 3)
For string manipulation there's also a set of handy functions:

length($string) -> Returns string length
reverse($string) -> Returns the string reversed
split(/regex/, $string) -> Splits a string on a regular expression (more about this later on) and returns an array
uc($string) -> Returns string all uppercase
lc($string) -> Returns string all lowercase
substr($string, [$initial_index,][$number_characters,][$substitution]) -> Returns a substring (replaced if 4th argument indicated) of the original string
printf($format, $string) -> Prints string on standard output with C-like printf() formatting
sprintf($format, $string) -> Same as printf() but instead of printing it it returns the formatted string
chop($string) -> Removes last string character (affects passed string, returns character removed)

<< Previous | Next >>
Advertising
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"

Sirius
Posts: 103
Joined: Sat Dec 18, 2010 3:31 pm

Re: [Tutorial] Introduction to Perl's awesomeness (II)

Post by Sirius » Fri Dec 09, 2011 11:59 pm

Hi, everything is very clear, just one question

Code: Select all

**   Power
Do you mean ++?
Advertising

User avatar
m0skit0
Guru
Posts: 3817
Joined: Mon Sep 27, 2010 6:01 pm

Re: [Tutorial] Introduction to Perl's awesomeness (II)

Post by m0skit0 » Mon Dec 12, 2011 8:31 am

No, I mean **.

x++ is the same as x = x + 1 (and btw Perl also accepts it)
x**2 means x*x
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"

Post Reply

Return to “Programming and Security”