Skip to main content

Perl programming #2

Hi,

Yesterday we learnt about installing perl in windows (Didn't read yesterday's post? check here Perl programming #1 )and how to “print” using perl!
Today, let’s start with the basics, how to get input using perl?


To get a text file from user using perl:
Just it is very simple, type as follows,

$variable=<STDIN>;

This will get the file name. This also works in perl,

$variable=<>;

No need to give STDIN! Just the open and closed angular brackets are itself considered as standard input.

Then, always give chomp after the standard input, this chomp is nothing but “chop” which chops the “enter” à new line character i.e., whenever you give an input, you press “enter” which will be considered as a new line character. In order to remove that new line character, chomp must be given.

How to use chomp? Use as follows,

$variable=<>;
chomp($variable);

Simple, this chops the new line character from your input.
Let us look at sample program for getting a text file and a word or pattern.
Program for getting a DNAFILE and printing the file content: (sample.pl)

print "enter a dna sequence file\n";

$dnafile=<>;
chomp($dnafile);
unless(open(DNAFILENAME,$dnafile))
{
print "file not found\n";
}
$dnafile=<DNAFILENAME>;
$dna=join("",$dnafile);
$dna=~s/\s//g;
$dna=uc($dna);

print "DNA seq. is\n",$dna;
close(DNAFILENAME);
exit;


O/P of the program sample.pl

Here, DNAFILENAME is the file handler. Here, we used unless for checking, whether the given file name exists or not.

For opening the file, we can give just  as,

open(DNAFILENAME,$dnafile);

don’t forget the semicolon.

Don’t forget to give the line,

 $dnafile=<DNAFILENAME>;

This assigns the content of the given dnafile to “$dnafile”
With out assigning this if you are just proceeding, you will get the output printed as the “file name” and not the file content.

“join” command is given for joining the spaces in the file. This helps when you are working with DNA and protein sequences.

$dna=~s/\s//g; This is also given for removing the spaces in the file

$dna=uc($dna); This changes the content of the file into upper case(UC)

“close” is used for closing the file,

print – It prints everything given within double quotes, even \n must be given within quotes, but, the variables like $dnafile must not be given within the quotes. Check the syntax given in the program.

Program for getting a pattern or word from the user and printing it (sample1.pl)


print "enter a pattern\n";
$pattern=<>;
chomp($pattern);
$pattern=join("",$pattern);
$pattern=~s/\s//g;
$pattern=uc($pattern);
print "Pattern is\n",$pattern;
exit;


O/P of the program sample1.pl

Here, no file handlers are used. Once you try executing the programs, you could easily understand!

Try printing the output, after using “join” and the line “$dna=~s/\s//g” for knowing their difference. Here “g” stands for global i.e., removes the space globally.

Any doubts? Kindly comment. I’ll try to clear your doubts.

:) Happy perl-ing :)



Comments

Popular posts from this blog

Sea Butterfly!

Neih hou!  Don't roll you eyes wondering what it is! "Neih hou" is how you say "hello" in Cantonese. Guess what, I am in Hong Kong and therefore the language Cantonese. I came to Hong Kong this summer as an intern before officially joining as a PhD Student. This is my second experience abroad, far away from home, new language, new culture, I expected me to have a "really" bad cultural shock, but, actually I experienced only 50% of what I expected. For a girl who have used the marina beach only for eating "sundal", bikini in beach was a shock (FEEL FREE TO JUDGE ME!). The first time, I went to the big wave beach here, I was the only one who was totally covered, when everyone was enjoying their Beer, I was slowly sipping my lemon juice (THE ODD ONE OUT!). I realized how beautifully different the world outside is! There is no single standard for right or wrong, it varies in different countries, in different regions around the world. And it ...

The Butterfly flew for the first time!

Hi dear sweet lovely reader, Didn't meet you for a long time... Just after complaining about the stipend problem in the last post, I actually wanted to write a lot here, but, couldn't find time (Such a lazy girl, I am!). As you might be knowing, the author (me) is the butterfly here :P And, I flew for the first time!! YEAH! I FLEW! I flew like a butterfly! It was an awesome experience flying for the very first time! (ow, no! I dint put a trans-gene in my body and fly after getting  a pair of wings, but with a passport and VISA). I'm this girl, full of dreams wishing to do "this", "that" and "all kind of stuffs that one can do". And, one among those dreams was to fly one day! TO FLY FREE!(I mean, not free of cost :P, but liberation) I always wanted to fly in those big, big airplanes, but, I always have avoided that mode of transport. Reason? Obviously, it would burn a big hole in my dad's pocket (actually he can afford if I wan...

Plant Tissue culture - Basics

Hi, I know a bit about Plant tissue culture and I would like to share it here. Plant Tissue culture Rack Plant Tissue culture, to be simple, it is growing plants in laboratory (invitro). To be more simple, you grow plants in test tubes and bottles by providing them light, nutrient by mimicking the natural conditions. It is effectively used as tool for producing GM(Genetically Modified) plants. Don't confuse "plant tissue culture" with "HORTICULTURE" The basic of Plant Tissue Culture : Totipotency:  The ability of a single plant cell to grow into an entire plant is generally referred as totipotency. Plasticity:  The ability of plants to survive in extreme conditions by modifying their metabolism is generally referred as plasticity. Things you need to do plant tissue culture 1) Laminar air hood 2) Plant growth chamber 3)Chemicals for preparing medium 4)Autoclave 5)pH meter 6) Test tubes or Bottles with caps 7) Cotton plugs 8) Scalpels, Fo...