master perl io_2011

38
Bioinformatics master course, ‘11/’12 Paolo Marcatili Read and Write Files with Perl

Upload: paolo-marcatili

Post on 09-May-2015

395 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Master perl io_2011

Bioinformatics master course, ‘11/’12

Paolo Marcatili

Read and Write Fileswith Perl

Page 2: Master perl io_2011

Bioinformatics master course, ‘11/’12

Paolo Marcatili

Resume

• Scalars $• Arrays @• Hashes %• Foreach |-@ $ • Length …• Split $ @

Page 3: Master perl io_2011

Bioinformatics master course, ‘11/’12

Paolo Marcatili

Agenda

• For• Conditions• Perl IO• Open a File• Write on Files• Read from Files• While loop

Page 4: Master perl io_2011

Bioinformatics master course, ‘11/’12

Paolo Marcatili

New cycle!

for (my $i=0;$i<100;$i++){print “$i \n”;

}

Can we rewrite foreach using for?

Hint: scalar @array is the array size

Page 5: Master perl io_2011

Bioinformatics master course, ‘11/’12

Paolo Marcatili

Conditions

if (condition){Do something}

Page 6: Master perl io_2011

Bioinformatics master course, ‘11/’12

Paolo Marcatili

Conditions

if ($time>5){print “Time to go!\n”;}

Page 7: Master perl io_2011

Bioinformatics master course, ‘11/’12

Paolo Marcatili

Conditions

if ($day == 27){print “Wage!\n”;}

Page 8: Master perl io_2011

Bioinformatics master course, ‘11/’12

Paolo Marcatili

Conditions

if ($day eq “Sunday”){print “Alarm off!\n”;}else{

print “snooze\n”;

}

Page 9: Master perl io_2011

Bioinformatics master course, ‘11/’12

Paolo Marcatili

Perl IO

(IO means Input/Output)

Page 10: Master perl io_2011

Bioinformatics master course, ‘11/’12

Paolo Marcatili

Why IO?

Since now, Perl is

#! /usr/bin/perl -w

use strict; #<- ALWAYS!!!my $string=”All work and no play makes Jack

a dull boy\n";for (my $i=1;$i<100;$i++){ print $string;}

Page 11: Master perl io_2011

Bioinformatics master course, ‘11/’12

Paolo Marcatili

Why IO?

But if we want to do the same with a user-submitted string?

Page 12: Master perl io_2011

Bioinformatics master course, ‘11/’12

Paolo Marcatili

Why IO?

But if we want to do the same with a user-submitted string?

IO can do this!

Page 13: Master perl io_2011

Bioinformatics master course, ‘11/’12

Paolo Marcatili

IO types

Main Inputs• Keyboard• File• ErrorsMain outputs• Display• File

Page 14: Master perl io_2011

Bioinformatics master course, ‘11/’12

Paolo Marcatili

More than tomatoes

Let’s try it:

#! /usr/bin/perl -w use strict; my $string=<STDIN>;

for (my $i=1;$i<100;$i++){print $string;

}

Page 15: Master perl io_2011

Bioinformatics master course, ‘11/’12

Paolo Marcatili

Rationale

Read from and write to different media

STDIN means standard input (keyboard)

^ this is a handle

<SMTH> means “read from the source corresponding to handle

SMTH”

Page 16: Master perl io_2011

Bioinformatics master course, ‘11/’12

Paolo Marcatili

Handles

Handles are just streams “nicknames”Some of them are fixed:STDIN <-default is keyboardSTDOUT <-default is displaySTDERR <-default is displaySome are user defined (files)

Page 17: Master perl io_2011

Bioinformatics master course, ‘11/’12

Paolo Marcatili

Open/Write a file

Page 18: Master perl io_2011

Bioinformatics master course, ‘11/’12

Paolo Marcatili

open

We have to create a handle for our fileopen(OUT, “>”,”out.txt”) or die(“Error opening

out.txt: $!”);

^N.B. : it’s user defined, you decide it

Tip“<“,”out.txt” <- read from out.txt “>”,”out.txt” <- write into out.txt “>>”,”out.txt” <- append to out.txt

Page 19: Master perl io_2011

Bioinformatics master course, ‘11/’12

Paolo Marcatili

close

When finished we have to close it: close OUT;

If you dont, Santa will bring no gift.

Page 20: Master perl io_2011

Bioinformatics master course, ‘11/’12

Paolo Marcatili

Print OUT

#! /usr/bin/perl -w

use strict;open(OUT, ">”,”out.txt") || die("Error opening

out.txt: $!");print "type your claim:\n";my $string=<STDIN>;for (my $i=1;$i<100;$i++){ print OUT $string;}close OUT;

Now let’s play with <,>,>> and file permissions

Page 21: Master perl io_2011

Bioinformatics master course, ‘11/’12

Paolo Marcatili

Read from Files

Page 22: Master perl io_2011

Bioinformatics master course, ‘11/’12

Paolo Marcatili

Readopen(IN, “<song.txt”) or die(“Error opening song.txt: $!”);print <IN>;print <IN>;print <IN>;print <IN>;print <IN>;print <IN>;print <IN>; print <IN>;print <IN>; print <IN>;print <IN>; print <IN>;print <IN>; print <IN>;

close IN;

Page 23: Master perl io_2011

Bioinformatics master course, ‘11/’12

Paolo Marcatili

Read with loops

Problems:• It’s long• File size unknown

solution:Use loops

Page 24: Master perl io_2011

Bioinformatics master course, ‘11/’12

Paolo Marcatili

While loop

Page 25: Master perl io_2011

Bioinformatics master course, ‘11/’12

Paolo Marcatili

While

while (condition){ do something…

}

Page 26: Master perl io_2011

Bioinformatics master course, ‘11/’12

Paolo Marcatili

While - for differences

While

• Undetermined

• No counter

For

> Determined> Counter

Page 27: Master perl io_2011

Bioinformatics master course, ‘11/’12

Paolo Marcatili

While exampleApprox. solution of x^2-2=0(Newton’s method)

my $sol=0.5;my $err=$sol**2-2;while ($err**2>.001){$sol-=($sol**2-2)/(2*$sol);$err=$sol**2-2;print “X is $sol\nError=$err\n”;}

Page 28: Master perl io_2011

Bioinformatics master course, ‘11/’12

Paolo Marcatili

Read with while

#! /usr/bin/perl -w

use strict;open(MOD, "<IG.pdb") || die("Error

opening IG.pdb: $!");

while (my $line=<MOD>){ print substr($line,0,6)."\n";}close MOD;

Page 29: Master perl io_2011

Bioinformatics master course, ‘11/’12

Paolo Marcatili

Redirect outputs

Page 30: Master perl io_2011

Bioinformatics master course, ‘11/’12

Paolo Marcatili

Redirections

#!/usr/bin/perlopen(STDOUT, ">foo.out") || die "Can't redirect stdout";open(STDERR, ">&STDOUT") || die "Can't dup stdout";

select(STDERR); $| = 1; # make unbufferedselect(STDOUT); $| = 1; # make unbuffered

close(STDOUT);close(STDERR);

Page 31: Master perl io_2011

Bioinformatics master course, ‘11/’12

Paolo Marcatili

@ARGV

Page 32: Master perl io_2011

Bioinformatics master course, ‘11/’12

Paolo Marcatili

Command Line Arguments

• Command line arguments in Perl are extremely easy.• @ARGV is the array that holds all arguments passed in from

the command line.– Example:

• % ./prog.pl arg1 arg2 arg3– @ARGV would contain ('arg1', arg2', 'arg3)

• $#ARGV returns the number of command line arguments that have been passed. – Remember $#array is the size of the array -1 !

Page 33: Master perl io_2011

Bioinformatics master course, ‘11/’12

Paolo Marcatili

Quick Program with @ARGV

• Simple program called log.pl that takes in a number and prints the log base 2 of that number;

#!/usr/local/bin/perl -w$log = log($ARGV[0]) / log(2);print “The log base 2 of $ARGV[0] is $log.\n”;

• Run the program as follows:– % log.pl 8

• This will return the following:– The log base 2 of 8 is 3.

Page 34: Master perl io_2011

Bioinformatics master course, ‘11/’12

Paolo Marcatili

$_

• Perl default scalar value that is used when a variable is not explicitly specified.

• Can be used in– For Loops– File Handling– Regular Expressions

Page 35: Master perl io_2011

Bioinformatics master course, ‘11/’12

Paolo Marcatili

$_ and For Loops• Example using $_ in a for loop

@array = ( “Perl”, “C”, “Java” );for(@array) { print $_ . “is a language I know\n”;}

– Output : Perl is a language I know. C is a language I know. Java is a language I know.

Page 36: Master perl io_2011

Bioinformatics master course, ‘11/’12

Paolo Marcatili

$_ and File Handlers• Example in using $_ when reading in a file;

while( <> ) { chomp $_; # remove the newline char @array = split/ /, $_; # split the line on white space

# and stores

data in an array}

• Note:– The line read in from the file is automatically store in

the default scalar variable $_

Page 37: Master perl io_2011

Bioinformatics master course, ‘11/’12

Paolo Marcatili

Opendir, readdir

Page 38: Master perl io_2011

Bioinformatics master course, ‘11/’12

Paolo Marcatili

Opendir & readdir• Just like open, but for dirs

# load all files of the "data/" folder into the @files arrayopendir(DIR, ”$ARGV[0]");@files = readdir(DIR);closedir(DIR); # build a unsorted list from the @files array:print "<ul>"; foreach $file (@files) { next if ($file eq "." or $file eq ".."); print "<li><a href=\"$file\">$file</a></li>";} print "</ul>";