0% found this document useful (0 votes)
54 views4 pages

Use Use My My Die Print: Strict Warnings $filename Open $FH $filename or $FH Close $FH

This document contains Perl code examples for performing various file input/output operations: 1. The first example writes a simple string to a text file. 2. The second example takes user input from standard input and writes it to a file. 3. Additional examples demonstrate reading and printing a file's contents, using a hash to associate names with families, reversing the keys and values of a hash, counting word frequencies, and formatting environment variables in columns.

Uploaded by

Roopesh Jhurani
Copyright
© © All Rights Reserved
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)
54 views4 pages

Use Use My My Die Print: Strict Warnings $filename Open $FH $filename or $FH Close $FH

This document contains Perl code examples for performing various file input/output operations: 1. The first example writes a simple string to a text file. 2. The second example takes user input from standard input and writes it to a file. 3. Additional examples demonstrate reading and printing a file's contents, using a hash to associate names with families, reversing the keys and values of a hash, counting word frequencies, and formatting environment variables in columns.

Uploaded by

Roopesh Jhurani
Copyright
© © All Rights Reserved
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/ 4

Perl Programs RJ

1. Writing to files with perl this program writes using file handler to
report.txt file but text is given inside program itself. Next program
uses <stdin>
Greater than operator wipes out old file if present with same name and
creates one.
If warnings were not use then if file location would not have been
presented correct say in some directory dir/file.txt then too script would
not have told us.it would have just keep on running..so use warnings
always. So even if file is not available done will get printed.

use strict;
use warnings;
my $filename = 'report.txt';
open(my $fh, '>', $filename) or die "Could not open file '$filename' $!";
print $fh "My first report generated by perl\n";
close $fh;
print "done\n";
2. use strict;
use warnings;
my $filename ='report.txt';
open(my $fh, '>', $filename) or die "'$filename' $!";
#print $fh "my second report by perl\n";
print $fh <stdin>;
close $fh;
print "done\n";
Now this program uses stdin so it will take through stdin user from
input and will print to $fh i.e., using this handler it will print it to file
associated to with, so now any input can be taken from user.
This die thing is to stop script in between if file required is not found.
http://perlmaven.com/writing-to-files-with-perl - all taken from this link.
Short and simple file operations: http://www.thegeekstuff.com/2010/09/perlfile-handle/

3. Program to print file content on screen


use warnings;
open $fh, '<', 'LNW.txt' or die "not able to open $!";
while($line=<$fh>)
{
print $line;
}
close $fh;
4. Program to prepare a hash key value and print while
continuously getting input from user.
$family_name{'fred'}='ran';
$family_name{'janny'}='roy';
$family_name{'raven'}='ravi';
print "enter family name\n";
while(chomp(my $name=<stdin>))
{
print "I have met $name $family_name{$name} \n";
}
If @arr=%family_name;
Print @arr; then this will print the keys and values of the key value
hash pair in an array form..no guarantee that they will in the same
order.
5. Code to change key,value,key,value in an array from hash pair
to
Value,key,value,key and so on. And also presented is a
goodway to include hash pairs.
my %family_name = (
'fred' => 'flintstone',
'dino' => undef,
'barney' => 'rubble',
'betty' => 'johny',
);
@arr=%family_name;
print "@arr\n";
my %inverse_hash=reverse %family_name;
@reverse=%inverse_hash;

print "@reverse\n";
can omit keys quotes but say an operator is used as key so
confusion so better keep it.
Also remember for comparsion something..first chomp the
value.
6. Code to enter word count using hash trick.
print "enter words\n";
while(chomp(my $word=<stdin>)){
if(exists $count{$word}){
$count{$word}+=1;
}
else
{
$count{$word}=1;
}
}
foreach my $key(sort keys %count)#see keys is a system keyword in
#perl
{
$value=$count{$key};
print "$key=>$value\n";
}
7. Program to print all the environment variables in column
format with length used of maximum key as the column width.
my $longest = 0;
foreach my $key ( keys %ENV ) {
my $key_length = length( $key );
$longest = $key_length if $key_length > $longest;
}
foreach my $key ( sort keys %ENV ) {
printf "%-${longest}s %s\n", $key, $ENV{$key};
}
8. A program to match a particular string in a program but with
case sensitivity but this program gives it even with an
attachment with a big word.. so here we find fred. Now this
program it even coming if given input say Alfred. But not
coming from Fred.
Idea is to open a filehandle to get the text from file and similar
to how we display it on the screen we take it in file handle and

play with it. So here dont think of printing $line or $fh


everything comes inside $_.
text
>cat test_file10.txt
1. hello fred how are you
2. hello Fred how are you
3. hello fredrick how are you
4. helllo alfred
5. hello everyone out there
6. hello all
program displays only lines with small fred so lines 1,3,4 get
printed.
use warnings;
open $fh, '<', 'test_file10.txt';
while(<$fh>)
{
if(/fred/)
{
print $_;
}
}
close $fh;
if u want to include Fred aswell then change it to/[fF]red/ or /(f|F)red/ or /fred|Fred/

You might also like