Dive Into Perl

Alright, I really only use perl if I run into a wall when writing a bash script. Here is a quick run down on perl taken from thier documentation:


First up, Hello World!. This is how you can send a string to standard out.

print "Hello World!";

Now that’s out of the way, lets take a look at scalars. Scalar variables are single values objects. You don’t have to declare a datatype to use them and they can freely switch between types. One requirement though, the use of the keyword “my”.

This example will create a scalar called “str” with the value of “Hello World!”

my $str = "Hello World!";

Arrays are always useful. You create arrays much like how you create a scalar value, just in this case you surround the elements you wish to store with parenthesis and use a “@” instead of a “$”.

my @arr = ("Hello", "World","!");

You can also do some interesting things working with the indexes of an array. One thing worth noting, it seems that “$” represents a scalar value for the perl compiler. If you want to some more dynamic programming make sure to use the “@”. The “..” operator will iterate through the list for a given range. You can use “..” together with a reference to the array ($#arr) to target the last element.

print $arr[0];  # "Hello"
print @arr; # "HelloWorld!"
print @arr[0..2] "HelloWorld!"
print @arr[1..$#arr] "World!"

Alright, now to the data types I like Hashes. Hashes and dictionaries are essential two lists organized by key and value pairs. To declare a hash, swap out the “@” of a list with “%”. This following example should create a hash with the keys, “Hello”, “World” with null values.

my %hash = ("Hello", "World");

You can also declare a hash with predefined values. In this case I created a hash with key “Hello” with the paired value “Bye”.

my %hash = (
    Hello => "Bye", 
    World => "World"

Naturall,y you can retrieve a list of the keys or values.

my @keys = keys %hash;
print @keys;
# WorldHello

As you can see the list of the keys are in reversed order. The same is true for the values.

my @vals = values %hash;
print @vals;
# WorldBye

This is about the end of this mini spot light on the perl language. If you’re interested in learning more, make sure you go read this doc, http://perldoc.perl.org/perlintro.html.

The doc was written by Kirrily “Skud” Robert and covers file I/O and subroutines. Before end this post I might as well show you an example of a foreach loop. This example will print out each element contained in my list “@keys”.

foreach (@keys ) {
    print "$_\n";

This next foreach example will print the contents of the hash.

foreach my $key (keys %hash ) {
    print "$key -> $hash{$key}\n";
# World -> World
# Hello -> Bye

I’ll try to pull together a collection of publicly available resources for learning perl later tonight.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s