0% found this document useful (0 votes)
25 views5 pages

Perl

This document discusses 5 Perl programs: 1. A program to count the number of words in a file 2. A currency conversion program 3. A program to convert hexadecimal and octal numbers to decimal 4. A program with questions and joke answers in a hash 5. A prime number calculation program up to a given number

Uploaded by

bharathimanian
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views5 pages

Perl

This document discusses 5 Perl programs: 1. A program to count the number of words in a file 2. A currency conversion program 3. A program to convert hexadecimal and octal numbers to decimal 4. A program with questions and joke answers in a hash 5. A prime number calculation program up to a given number

Uploaded by

bharathimanian
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

PERL (PRACTICAL EXTRACTION AND REPORT LANGUAGE)

EX NO : 1 DATE : 26.07.2012

ALGORITHM: 1. Start 2. Type the PERL program using vi editor and save with the extension .plx 3. # program-name.plx to execute the program 4. Repeat the same steps for executing remaining examples 5. End of Algorithm

I.COUNTING THE NUMBER OF WORDS IN A FILE PROGRAM: [fosslab@fosslab ~]$ vi sa.plx #!/usr/bin/perl print"Enter the file name:"; $filename=<STDIN>; chomp $filename; open($fh,"<",$filename); while($line=<$fh>) { @words=split'',$line; $nw=$nw+scalar(@words); } print"Number of words in file:$nw\n"; close($fh);

OUTPUT: [fosslab@fosslab ~]$ perl sa.plx Enter the file name:aaa.plx Number of words in file:41

II.CURRENCY CONVERTOR PROGRAM: [fosslab@fosslab ~]$ vi sa1.plx #!/usr/bin/perl use warnings; use strict; print"Currencyconvertor:\n\n\nPlease enter the yen to pound exchage rate:"; my$yen=<STDIN>; print"Now please enter ur first amount:"; my$first=<STDIN>; my$a=($first/$yen); print"Now please enter ur second amount:"; my$second=<STDIN>; my$b=($second/$yen); print"Now please enter ur third amount:"; my$third=<STDIN>; my$c=($third/$yen); my $mod1=(int(100*$a)/100); my $mod2=(int(100*$b)/100); my $mod3=(int(100*$c)/100); chomp($first,$second,$third); printf("First yen is %2f pound\n",$mod1); printf("Second yen is %2f pound\n",$mod2); printf("Third yen is %2f pound\n",$mod3); OUTPUT: [fosslab@fosslab ~]$ perl sa1.plx Currencyconvertor: Please enter the yen to pound exchage rate:10 Now please enter ur first amount:10 Now please enter ur second amount:20 Now please enter ur third amount:30 First yen is 1.000000 pound Second yen is 2.000000 pound Third yen is 3.000000 pound [fosslab@fosslab ~]$

III.CONVERSION OF HEXADECIMAL AND OCTAL TO DECIMAL NUMBER PROGRAM: [fosslab@fosslab ~]$ vi sa2.plx #!/usr/bin/perl use warnings; print"Enter hexadecimal number to convert it to decimal number:\n"; my $hex=<STDIN>; chomp($hex); print("The decimal value is:",(hex("$hex")),"\n\n"); print"Enter octal number to convert it to decimal number:\n"; my $oct=<STDIN>; chomp($oct); print("The decimal value is:",(oct("$oct")),"\n\n");

OUTPUT: [fosslab@fosslab ~]$ perl sa2.plx Enter hexadecimal number to convert it to decimal number:ff The decimal value is:255 Enter octal number to convert it to decimal number:56 The decimal value is:46

IV.QUESTIONS AND ANSWERS PROGRAM: [fosslab@fosslab ~]$ vi sa3.plx #!/usr/bin/perl use warnings; use strict; my $que_1="How many java programmers does it take to change a lightbulb?\n"; my $que_2="How many phython pgmers does it take to change a lightbulb?\n"; my $que_3="How many perl programmers does it take to change a lightbulb?\n"; my $que_4="How many c programmers does it take to change a lightbulb?\n"; my $answer1="None.Change it once,and its same everywhere\n"; my $answer2="one.he just stands below socket and the world revolves aroun him\n"; my $answer3="A million,one to change it,the rest to try andto do it in fewer lines\n"; my $answer4="Change!!!"; my %jokes=($que_1=>"$answer1", $que_2=>"$answer2", $que_3=>"$answer3", $que_4=>"$answer4"); print "$que_1"; sleep 5; print"$jokes{$que_1}"; print "$que_2"; sleep 5; print"$jokes{$que_2}"; print "$que_3"; sleep 5; print"$jokes{$que_3}"; print "$que_4"; sleep 5; print"$jokes{$que_4}"; OUTPUT: [fosslab@fosslab ~]$ perl sa3.plx How many java programmers does it take to change a lightbulb? None.Change it once,and its same everywhere How many phython pgmers does it take to change a lightbulb? one.he just stands below socket and the world revolves aroun him How many perl programmers does it take to change a lightbulb? A million,one to change it,the rest to try andto do it in fewer lines How many c programmers does it take to change a lightbulb? Change!!!

V.CALCULATION OF PRIMES PROGRAM: [fosslab@fosslab ~]$ vi sa4.plx #!/usr/bin/perl use warnings; use strict; print"What number would u like to calculate primes:"; my $n=<STDIN>; my $i=3; print"1 2"; OUTER:while($i<=$n) { my $num=2; for(1...$i) { if(($i%$num==0)and($i!=$num)) { $i++; next OUTER; } if($num>sqrt($i)) { print"$i"; $i++; next OUTER; } $num++; } } OUTPUT: [fosslab@fosslab ~]$ perl sa4.plx What number would u like to calculate primes:10 12357 RESULT: Thus the PERL program is executed and the output is verified.

You might also like