Wednesday, December 16, 2009

Perl for Noob

As before in our course of study, we just familiar with C, C++ and Java. For the sake of our project, I have been exploring Perl. Noted that memcached was firstly written by Brad Fitzpatrick together with Anatoly Corobey in Perl.

This is just fast study for very very very beginners of Perl (Correct me if i'm wrong, i am a noob too) :)

PERL - Practical Extraction and Report Language


Installing PERL
Perl is nicely included on Linux and Unix, but for Windows user, refer here. I currently use Ubuntu Karmic Koala, so I checked where is my Perl located by:

whereis perl

Most of the time, it located at /usr/bin/perl but anywhere doesnt matter. But, it's important to note whereis since it will be embedded in the first line of your script:

#!/usr/bin/perl --> This line must appear in the very first line of your script except for some condition I'll explain below.

OK, next to check is the version of your Perl.

perl -v

Mine is 5.10 (latest by now). I read somewhere that version 5.6.0 isn't stable and buggy than the later versions so do update ok :)

Done with checking.

Writing PERL
Simply touch your program or Perl file by putting extension; .pl or .plx --Both ways are OK. (e.g.: touch file.pl). Then change the file mode (chmod u+x file.pl) so it's executable.

Open editor (gedit, nano, vi, pico -- any will works, but I just started to love mcedit because it's nice); mcedit file.pl

OK, like I said, your first line must be #!/usr/bin/perl BUT, if you're using Perl 5.10 like me, you could consider of using use feature ':5.10' to replace #!/usr/bin/perl. This indicate that we could use some new feature in this latest version where some reserve words are added to simplify works. Here, I will discuss some basic commands of 5.10.

Basically, there are three in total - variable types in Perl:
  • Scalar - holds single data (int, long, char, string)
  • Array - holds list for scalar (10 int, 100 char, etc)
  • Hash - hold key-pair, refer further explanation below.
  • So, let us go one by one. I promise it's simple.

    Scalar
    Define: use dollar sign ($) together with variable name. E.g.: $var, $name.
    Example: $a = "value"; $number = 1;



    Remember this rule,
    • $ for Scalar

    • @ for Array

    • % for Hash


    $calar hold single data, so to print specific data or one element in @rray or %ash, use $calar. :-)

    No comments:

    Post a Comment