GMgKe586q6suSQnyqZLlGCooeWM

Pages

Search

Showing posts with label Perl. Show all posts
Showing posts with label Perl. Show all posts

Saturday, January 22, 2011

Perl As Server Side Scripting

Create file index.pl in htdocs directory then fill it with:

print "Content-Type: text/html"
print "Holla From Perl"

Then direct your browser to http://localhost/index.pl
Share/Bookmark

Creating CGI File

It's simple to create a CGI file. Just create hello.cgi file in htdocs directory then fill it with:
#!"C:\xampp\perl\bin\perl.exe"
print "Content-Type: text/html"

print "Holla World From CGI"

Then direct your browser to http://localhost/hello.cgi
           
Share/Bookmark

Wednesday, December 22, 2010

Perl Subroutines

To create a subroutine in Perl, type:
sub hello
{
    print "Hello, World!\n";
}

To call it, type:
&hello;
or
$hello($_);
or
$hello(1+2,$_);
            
Share/Bookmark

Tuesday, December 21, 2010

File I/O In Perl

Reading File:
To read a file, you need a handle:
>> open(my $in, "<", "D://data.txt") or die "Cannot open file: $!";

Then you use:
>> my $line = <$in>;    # just per line
>> print $line;
>> my @lines = <$in>;   # whole documents
>> print @lines;

Or you can read to whole of the text use:
>> while(<$in>){
>>     print "Text: $_";
>> }

Writing To File:
Firstly, create file handle:
>> open(my $out, ">", "D://test.txt") or die "Can't open: $!";

Then write to your file:
>> $msg = "Hello. Hope this file\nGot in the text so we can",
          "happy use it";
>> print $out $msg;
        
Share/Bookmark

Variable In Perl

Perl has three type of variables: Scalars, Arrays, and Hashes.

Scalar:
This type represent a single value:
You need to declare it use my keyword:
>> my $name = "lady gaga";
>> print "The world is full of: ", $name;
>> print "$name: ".$name;    # output: lady gaga: lady gaga

Arrays:
This represent a list of values:
>> my @name = ("lady gaga", "luna maya", "larry page");
>> my @id = (45, 98, 75, 66, 22);
>> my @mixed = ("lady gaga", 24);

To access an element, use its index:
>> print $name[0];
>> print $name[$#name];    # print the last element

More manouver:
>> @name[0,2];       # gives the 1 and the 3 element.
>> @name[0..2];      # gives the 1 to 3 elements
>> @name[0..$#name];  # gives the first till the last element
>> my @sorted = sort @name   # sorts elements
>> my @reverse = reverse @name    # reverse element

To iterate to all elements, use:
>> foreach(@name){
>>     print $_,"\n";
>> }

Hashes:
Hashes is key/value pairs. It's like assoc arrays in PHP:
>> my %name = ("lady"=>"gaga",
>>             "larry"=>"page",
>>             "aura"=>"kasih");

To access an element, use
>> print $name{"lady"};    # curly bracket, not square

To get the keys and values as an array, use:
>> my @fname = keys %name;
>> my @lname = values %name;

To iterate to all elements of the map, use:
>> foreach my $key (keys %name){
>>     print "The value of $key is $name{$key}\n";
>> } 
          
Share/Bookmark

Perl Fisrt Program

To write output in Perl, write:
>> print "Hello World";

# is a comment in perl

>> print "Hello,$name\n";
>> print 'Hello, $name\n';    # print literally  
>> print 45;
>> print("Holla World");
Share/Bookmark