Friday, September 02, 2011

Perl, here I come

I have been longing to learn a new language pretty fast and use it to do something interesting and exciting. I was trying to check out Ruby… But for inexplicable reason I found myself in the camp of Perl team.
This is what I have learnt while checking out Perl.

When it comes to data type, you have scalar, array (list) and hash table. Your beautiful character type is gone. String is even scalar … And to declare a scalar you have to append dollar sign before your variable name. For array you will have to append “@” sign and “%” for hash table.

String
I always believe that for one to learn new language he/she needs to have a good handle on how to manipulate string. So, let’s see what Perl have for us. Split and join functions are pretty what you have in other languages. Trim, rtrim and ltrim are just as their names sound. Pretty like Java’s trim. Substr function is used for finding substring. For case manipulations you have the followings: uc. lc, ufirst, lfirst. And for finding the length of string you need is length function.
There is one area of Perl that will keep you busy all the time that is context. We have scalar and array context. Take for example reverse function. It returns array by default, however if you call it inside a scalar function, it would be forced to work in scalar context. It would append its elements together.

$doo = scalar reverse $too;
$fin = @goo;
$con = (2,3,4);
$con will be assigned 2 because we are in scalar context. However,

@can = (2,3,4)… returns an array.

File manipulation
Note that Perl is at home when it comes for file. To open a file for writing,
Open FILEOBJECT, “>” ,filename || die “If not able to open file”;
Open FILEOBJECT, “<” ,filename || die “If not able to open file”; For reading.
Open FILEOBJECT, “>>” ,filename || die “If not able to open file”; To append to already existing file.
You have readline function , but Perl made life pretty easier with this

while(my $con ()){}
Remember that if you do “<.pl>” , we will loop through all the files in your current folder ending with that extension. And if you just do “<>” , then you will be accessing command line arguments.
Some really funny stuff -f checks if it is a file, -e checks if the file or folder exists and –d checks if it is a folder. You have many others. You may need to remember input-record separator $/… if you undef it, you will be reading the whole file once. There is also Output-record separator $\, it’s used by print function to decide how to terminate the record. Another what knowing is Field-separator, $”.
To write to a file just do,
Print FILEOBJECT, $boon;


Function creation.
In order to create a function you will type sub followed by function name and a block

Sub myfunc {}
There is nothing like function’s parameter … Perl has done that job for you. Inside the block you will use @_ array to access the arguments. For example,
My( $name, $address) = @_;
You can pass function as data, all you need is to make it a reference, \&functionname. You can also alias a function,
Sub mfor { \&foreach}

Array manipulation
You have unshift , shift , pop and push functions. You can also you use traditional indexing method of accessing elements of array.
@woo = (3 ,4 ,5);
$dd = $woo[2] ;

To merge two or more arrays together, it’s pretty easy. You don’t need to do much, just put them into a list.
@goo = (@woo, @noo);

When you want to call a function with array as an input argument, you need to maintain the integrity of the array by making it a reference. Just append backslash. \@goo. From now you have to assign it to scalar.
$foo = \@goo;
To dereference, just do @$foo. Perl has a lot of functional language concept … So, let’s see list comprehension.
Map --- takes array and function and returns array of the elements mapped to function.
Grep--- takes array and a predicate function and returns an array of elements that satisfies the predicate.
Sort function works for numeric and string.
for numeric … you use $a  $b as the comparison function, and for string you will do $a cmp $b
To compare strings you have the followings: eq(equal), le(less than), gt(greater than), ne(not equal)
For numeric, you have: >, < , >= ,<=
For Character

You need to split the string scalar which will result to an array of characters. However, you have char function which takes ascii and returns their character, ord does the reverse.


Mathematically inclined …you have Math module to help you out.
Basic Math functions: +, - , / , %
And to try out weby stuff you have LWP module. And when it comes to GUI stuff, you will be overwhelmed by the options open to you. From TK to GTK… the choice is yours.

For looping and iterating, you have while loop, for, and foreach.
And for conditional statement you have if, elif, else and unless.

For interactive mode do this in your shell mode
Perl –de -0

For magic from Perl… Type has gone and now you are free to do whatever you want. Check out this

$f = “o” x 3; Try it.

To accept input from user do, chomp (my $name =<>);

Regular Expression
$din =~ m/[o-s]/;
To find and replace you have,
$din =~ s/[o-p]+/”loop”;
To replace one string with another, you have
$din =~ tr/”kool”/”duuwla”/;

This is just the surface; Perl is the best-in-breed when it comes to Regrex.

I would not want to deceive myself about Perl when it comes to package and module. Here to me Perl failed, however because it is a language of smart people Package and module use the same names. That makes life pretty easy for mere mortals like me.

3 comments:

Murtaza said...

A great way to help the newbies!

Good effort:)

selftaught said...

What kind of programming background did you have before you started to learn perl? Very nice blog!

Emeka said...

@Selftaught,
I play around with Clojure.

Tags

Arduino (1) C (3) Clojure (3) Perl (1) the other side (8) VBA (1)

micro's shared items