Wednesday 5 February 2014

Perl and object oriented programming

When you program and you want to write some code you can reuse, it is always a good practice to use an object oriented approach for your code. Today I realized that I never follow this rule when I program in Perl, because I am not so familiar with the oo synthax for this programming language. So I decided to fill this gap and I wrote a small program to convert a some files containing sequence alignements arranged in a pretty strange format to classic (and readable) fasta files. Here is the result of my experiment (I simplified a lot my script!). I hope someone will find it useful!

 The main script:
 
#!/usr/bin/perl 
 
#Perl will search for packages in the current directory
push(@INC,'.'); 

#I load the package Covertcsv
use Convertcsv; 
 
#I create a new object Convertcsv 
$obj=Convertcsv->new(); 
 
#I call the method csv2fasta 
$obj->csv2fasta("a.txt","b.fasta");



And the package Convertcsv.pm:

#!/usr/bin/perl 
 
#this is the package Convertcsv 
package Convertcsv;
use strict; 
 
#constructor 
sub new{
 my $this={};
 bless $this;
 return $this;
} 
 
#method 
sub csv2fasta{
 my $this = shift;

 my($file_input,$file_output)=@_;

 #code to convert the files

}

 

After all it is easy to work with objects in Perl too! :)

No comments:

Post a Comment