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

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

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 (IV)

Post by m0skit0 » Fri Nov 18, 2011 3:17 pm

<< Previous | Next >>

Script arguments

A Perl script can receive arguments from the user as well. In Perl I always prefer passing arguments than asking user for manual input. This is due to the fact that I use Perl for scripting, thus I want to automate the task: launch it and it does the work, not have it wait for my input.

A Perl script receives the arguments from the OS in the @ARGV array (and yet another C-like thing ;) ). The first element $ARGV[0] is actually the first argument, and not the script name (C users will understand why I'm taking about this).

So for example, a script that computes the addition of 2 numbers:

Code: Select all

#!/usr/bin/perl

use strict;

my $sum = $ARGV[0] + $ARGV[1];
print("$ARGV[0] + $ARGV[1] = $sum\n");

Code: Select all

> perl sum.pl 6 4
6 + 4 = 10
Water can flow... or it can crash!

Perl flow control structres are the same than on C. Boolean operators are the same than on C, too (although there are more word-ish alternatives, like "and" instead of "&&"...).

Code: Select all

if(condition)
{
}
elsif (condition)
{
}
else
{
}

Code: Select all

while(condition)
{
}

do
{
} while (condition)

Code: Select all

for(initialization; stop_condition; iteration_operation)
{
}

for $variable (@array)
{
}
The for $variable (@array) loop will go through all array elements, while $variable will take one element value in each iteration. To show an example:

Code: Select all

#!/usr/bin/perl

use strict;

my @array_example = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9);

for my $number (@array_example)
{
	print("$number\n");
}
The $variable can be removed, in which case the default scalar $_ will hold the value:

Code: Select all

#!/usr/bin/perl

use strict;

my @array_example = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9);

for (@array_example)
{
	print("$_\n");
}
Comparison operators

(numbers)

Code: Select all

==		Equal
!=		Not equal
>=		Greater or equal
>		Greater
<		Lesser
<=		Lesser or equal
(strings)

Code: Select all

eq		Equal
ne		Not equal
gt		Greater than
lt		Lesser than
Logic operators

Code: Select all

!	Not
&&	And
||	Or
^	Exclusive or (Xor)
Let's expand our last addition script into adding any list of numbers passed as arguments:

Code: Select all

#!/usr/bin/perl

use strict;

my $total = 0;
my $print;
for (@ARGV)
{	
	$print .= "$_+";
	$total += $_;
}
chop($print);
print("$print = $total\n");

Code: Select all

> ./sum.pl 6 5 6 12 12.5
6+5+6+12+12.5 = 41.5
<< Previous | Next >>
Advertising
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"

Post Reply

Return to “Programming and Security”