Perl beginner – install and run hello world, MySQL test

In Programming on February 22nd, 2010

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

  1. $ 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.

  1. #!/usr/bin/perl
  2. #simple perl program to print the user input
  3. print ("Hello world! test goes this\n");
  4. $inputline=<stdin>;
  5. print ($inputline);

 

Save file named as perl_test.pl and run following command.

  1. $ sudo chmod +x perl_test.pl
  2. $ ./perl_test.pl

 

Database Test:
Create perl_db.pl file and save following content.

  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use DBI;
  5.  
  6. my $username='test';
  7. my $pass='test';
  8. my $db='test';
  9. my $dbh = DBI->connect( "dbi:mysql:$db", $username, $pass, { 'PrintError' => 1, 'RaiseError' => 1 } );
  10. my $sql='select * from test';
  11. my $sql_handle=$dbh->prepare($sql);
  12. $sql_handle->execute();
  13. my @data;
  14. while (@data=$sql_handle->fetchrow_array()) {
  15. print join("\n",@data)
  16. }

 

  1. $ sudo chmod +x perl_db.pl
  2. $ sudo ./perl_db.pl

 

Make sure user test with password test and database test exits. And it has test table.

 
Haurry!!!