About Files

Input, Output and Other Things

I/O: Input and Output

The standard streams: STDIN, STDOUT and STDERR

my $data = <STDIN>;

my $data = <>;

print STDOUT $data;

print $data;

print STDERR "Something terrible has happened ... aborting.\n";

warn "Something terrible has happened ... aborting.\n";

STDIN, STDOUT and STDERR

This is the first disk-file, line 1.

This is the first disk-file, line 2.

This is the first disk-file, line 3.

This is the first disk-file, line 4.

This is the first disk-file, line 5.

This is the second disk-file, line 1.

This is the second disk-file, line 2.

This is the second disk-file, line 3.

Reading Files

This is the first disk-file, line 1.

This is the second disk-file, line 1.

This is the first disk-file, line 2.

This is the second disk-file, line 2.

This is the first disk-file, line 3.

This is the second disk-file, line 3.

This is the first disk-file, line 4.

This is the first disk-file, line 5.

Merging Files

#! /usr/bin/perl -w

# determine_args - print out the names of the disk-files named

# on the command-line.

if ( $#ARGV != 1 )

{

warn "Please supply the names of two disk-files on the

command-line.\n";

exit;

}

my ( $first_filename, $second_filename ) = @ARGV;

print "first disk-file name is: $first_filename\n";

print "second disk-file name is: $second_filename\n";

Determining the disk-file names

my $first_filename = $ARGV[0];

my $second_filename = $ARGV[1];

my $first_filename = shift;

my $second_filename = shift;

$ perl determine_args first_file.txt second_file.txt

first disk-file name is: first_file.txt

second disk-file name is: second_file.txt

The Default Argument Array

#! /usr/bin/perl -w

# check_args - check that the disk-files named

# on the command-line exist.

if ( $#ARGV != 1 )

{

die "Please supply the names of two disk-files on

the command-line.\n";

}

my ( $first_filename, $second_filename ) = @ARGV;

unless ( -e $first_filename && -f $first_filename && -r $first_filename )

{

die "$first_filename cannot be accessed. Does it exist?\n";

}

unless ( -e $second_filename && -f $second_filename && -r $second_filename )

{

die "$second_filename cannot be accessed. Does it exist?\n";

}

Opening named disk-files

open FIRSTFILE, "$first_filename";

open SECONDFILE, "$second_filename";

open FIRSTFILE, "<$first_filename";

open SECONDFILE, "<$second_filename";

open FIRSTFILE, "$first_filename"

or die "Could not open $first_filename. Aborting.\n";

open SECONDFILE, "$second_filename"

or die "Could not open $second_filename. Aborting.\n";

close FIRSTFILE;

close SECONDFILE;

The check_args program, cont.

Maxim 6.1

Open a disk-file for as long as needed, but no longer

Maxim 6.2

If you open a disk-file, be sure to close it later.

my ( $linefromfirst, $linefromsecond );

$linefromfirst = <FIRSTFILE>;

$linefromsecond = <SECONDFILE>;

Reading a line from each of the disk-files

#! /usr/bin/perl -w

# merge2files - merge the two disk-files named on the command-line.

if ( $#ARGV != 1 )

{

die "Please supply the names of two disk-files on

the command-line.\n";

}

my ( $first_filename, $second_filename ) = @ARGV;

unless ( -e $first_filename && -f $first_filename && -r $first_filename )

{

die "$first_filename cannot be accessed. Does it exist?\n";

}

Putting it all together

unless ( -e $second_filename && -f $second_filename && -r $second_filename )

{

die "$second_filename cannot be accessed. Does it exist?\n";

}

open FIRSTFILE, "$first_filename"

or die "Could not open $first_filename. Aborting.\n";

open SECONDFILE, "$second_filename"

or die "Could not open $second_filename. Aborting.\n";

my ( $linefromfirst, $linefromsecond );

while ( $linefromfirst = <FIRSTFILE> )

{

$linefromsecond = <SECONDFILE>;

print $linefromfirst;

print $linefromsecond;

}

close FIRSTFILE;

close SECONDFILE;

The merge2files program, cont.

$ chmod +x merge2files

$ ./merge2files first_file.txt second_file.txt

This is the first disk-file, line 1.

This is the second disk-file, line 1.

This is the first disk-file, line 2.

This is the second disk-file, line 2.

This is the first disk-file, line 3.

This is the second disk-file, line 3.

This is the first disk-file, line 4.

Use of uninitialized value in print at merge2files line 35, <SECONDFILE> line 3.

Use of uninitialized value in print at merge2files line 35, <SECONDFILE> line 3.

This is the first disk-file, line 5.

Use of uninitialized value in print at merge2files line 35, <SECONDFILE> line 3.

Use of uninitialized value in print at merge2files line 35, <SECONDFILE> line 3.

Running merge2files ...

$ ./merge2files second_file.txt first_file.txt

This is the second disk-file, line 1.

This is the first disk-file, line 1.

This is the second disk-file, line 2.

This is the first disk-file, line 2.

This is the second disk-file, line 3.

This is the first disk-file, line 3.

Running merge2files again ...

if ( !eof( SECONDFILE ) )

{

$linefromsecond = <SECONDFILE>;

print $linefromsecond;

}

Creating merge2files_v2

$ ./merge2files_v2 first_file.txt second_file.txt

This is the first disk-file, line 1.

This is the second disk-file, line 1.

This is the first disk-file, line 2.

This is the second disk-file, line 2.

This is the first disk-file, line 3.

This is the second disk-file, line 3.

This is the first disk-file, line 4.

This is the first disk-file, line 5.

Running merge2files_v2 ...

This is the second disk-file, line 1.

This is the first disk-file, line 1.

This is the second disk-file, line 2.

This is the first disk-file, line 2.

This is the second disk-file, line 3.

This is the first disk-file, line 3.

Running merge2files_v2 again ...

#! /usr/bin/perl -w

# merge2files_v3 - third version of merge2files: merge the disk-files

# named on the command-line (with some help from eof and defined).

# Make sure all lines are read from both disk-files.

if ( $#ARGV != 1 )

{

die "Please supply the names of two disk-files on

the command-line.\n";

}

my ( $first_filename, $second_filename ) = @ARGV;

unless ( -e $first_filename && -f $first_filename && -r $first_filename )

{

die "$first_filename cannot be accessed. Does it exist?\n";

}

Creating merge2files_v3

unless ( -e $second_filename && -f $second_filename && -r $second_filename )

{

die "$second_filename cannot be accessed. Does it exist?\n";

}

open FIRSTFILE, "$first_filename"

or die "Could not open $first_filename. Aborting.\n";

open SECONDFILE, "$second_filename"

or die "Could not open $second_filename. Aborting.\n";

my ( $linefromfirst, $linefromsecond );

The merge2files_v3 program, cont.

while ( $linefromfirst = <FIRSTFILE> )

{

print $linefromfirst;

if ( !eof( SECONDFILE ) )

{

$linefromsecond = <SECONDFILE>;

print $linefromsecond;

}

}

while ( !eof( SECONDFILE ) )

{

$linefromsecond = <SECONDFILE>;

print $linefromsecond;

}

close FIRSTFILE;

close SECONDFILE;

The merge2files_v3 program, cont.

@entire_file = <>;

Slurping

#! /usr/bin/perl -w

# slurper - a program which demonstrates disk-file "slurping".

use lib "$ENV{'HOME'}/bbp/";

use UsefulUtils qw( drawline );

open FIRSTSLURPFILE, "first_file.txt"

or die "Could not open first slurp disk-file. Aborting.\n";

open SECONDSLURPFILE, "second_file.txt"

or die "Could not open second slurp disk-file. Aborting.\n";

my @linesfromfirst = <FIRSTSLURPFILE>;

my @linesfromsecond = <SECONDSLURPFILE>;

print drawline( Count => 40 ), "\n";

print @linesfromfirst;

print drawline( Count => 40 ), "\n";

print @linesfromsecond;

print drawline( Count => 40 ), "\n";

close FIRSTSLURPFILE;

close SECONDSLURPFILE;

Slurping example

----------------------------------------

This is the first disk-file, line 1.

This is the first disk-file, line 2.

This is the first disk-file, line 3.

This is the first disk-file, line 4.

This is the first disk-file, line 5.

----------------------------------------

This is the second disk-file, line 1.

This is the second disk-file, line 2.

This is the second disk-file, line 3.

----------------------------------------

Results from slurper ...

my $file_to_open = "errors.log";

...

open( LOGFILE, ">$file_to_open" )

or die "Could not write to/create errors log disk-file.\n";

open( LOGFILE, ">>$file_to_open" )

or die "Could not append to/create errors log disk-file.\n";

print LOGFILE "Error: something terrible has happened.\n";

Writing Files

$ ./merge2files first_file.txt second_file.txt > merge.out

$ ./merge2files first_file.txt second_file.txt >> merge.out

$ ./merge2files first_file.txt second_file.txt > merge.out 2> merge.err

$ ./merge2files first_file.txt second_file.txt >> merge.out 2>> merge.err

Redirecting output

my $sequence = "TTATTATGTT GCTCAGTTCT GACCTCTTAA CTATGCGGTA";

print "The sequence is: $sequence\n";

print 'The sequence is: $sequence\n';

The sequence is: TTATTATGTT GCTCAGTTCT GACCTCTTAA CTATGCGGTA

The sequence is: $sequence\n

Variable interpolation

my $dna = "ATGTGCGGTATTGCTGACCTCTTA\n";

my $last = chop $dna;

# $dna is now "ATGTGCGGTATTGCTGACCTCTTA";

my $next = chop $dna;

# $dna is now "ATGTGCGGTATTGCTGACCTCTT";

my $dna = "ATGTGCGGTATTGCTGACCTCTTA\n";

my $last = chomp $dna;

# $dna is now "ATGTGCGGTATTGCTGACCTCTTA";

my $next = chomp $dna;

# $dna is still "ATGTGCGGTATTGCTGACCTCTTA";

Chopping and chomping

Where To From Here

2004-04-02T16:51:44