I am seasoned PHP programmer, been around ten years I am coding on PHP. Although I learn and tried many different programming languages, I always scare to give a try to Perl. I was in impression that Perl is difficult language so never get confident to try it, or never can arrange time for it. But, today as I was working on Asterisk for VoIP system, there’s was no choice left, but Perl.
I gave first try today in Ubuntu. It wasn’t that bad as I heard. Actually I was wrong about Perl, it was proved.
Below is how, I tried first hello world and first database application
Install Perl
-
$ sudo apt-get install perl
Don’t worry, if Perl is already installed, it do nothing. If you have Ubuntu server edition, Perl is already there.
Create directory called perl_test. Create perl_test.pl file and save following content.
-
#!/usr/bin/perl
-
#simple perl program to print the user input
-
print ("Hello world! test goes this\n");
-
$inputline=<stdin>;
-
print ($inputline);
Save file named as perl_test.pl and run following command.
-
$ sudo chmod +x perl_test.pl
-
$ ./perl_test.pl
Database Test:
Create perl_db.pl file and save following content.
-
#!/usr/bin/perl
-
use strict;
-
use warnings;
-
use DBI;
-
-
my $username='test';
-
my $pass='test';
-
my $db='test';
-
my $dbh = DBI->connect( "dbi:mysql:$db", $username, $pass, { 'PrintError' => 1, 'RaiseError' => 1 } );
-
my $sql='select * from test';
-
my $sql_handle=$dbh->prepare($sql);
-
$sql_handle->execute();
-
my @data;
-
while (@data=$sql_handle->fetchrow_array()) {
-
print join("\n",@data)
-
}
-
$ sudo chmod +x perl_db.pl
-
$ sudo ./perl_db.pl
Make sure user test with password test and database test exits. And it has test table.
Haurry!!!