Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Perl IO
1. Read and Write Files
with Perl
Paolo Marcatili - Programmazione 09-10
2. Agenda
> Perl IO
> Open a File
> Write on Files
> Read from Files
> While loop
2
Paolo Marcatili - Programmazione 09-10
3. Perl IO
(IO means Input/Output)
Paolo Marcatili - Programmazione 09-10
4. Why IO?
Since now, Perl is
#! /usr/bin/perl -w
use strict; <- ALWAYYYYYSSSS!!!
my $string=”All work and no play makes Jack a
dull boyn";
for (my $i=1;$i<100;$i++){
print $string;
}
4
Paolo Marcatili - Programmazione 09-10
5. Why IO?
But if we want to do the same
with a user-submitted string?
5
Paolo Marcatili - Programmazione 09-10
6. Why IO?
But if we want to do the same
with a user-submitted string?
IO can do this!
6
Paolo Marcatili - Programmazione 09-10
7. IO types
Main Inputs
> Keyboard
> File
> Errors
Main outputs
> Display
> File
7
Paolo Marcatili - Programmazione 09-10
8. 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;
}
8
Paolo Marcatili - Programmazione 09-10
9. 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”
9
Paolo Marcatili - Programmazione 09-10
10. Handles
Handles are just streams “nicknames”
Some of them are fixed:
STDIN <-default is keyboard
STDOUT <-default is display
STDERR <-default is display
Some are user defined (files)
10
Paolo Marcatili - Programmazione 09-10
12. open
We have to create a handle for our file
open(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
12
Paolo Marcatili - Programmazione 09-10
13. close
When finished we have to close it:
close OUT;
If you dont, Santa will bring no gift.
13
Paolo Marcatili - Programmazione 09-10
14. 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
14
Paolo Marcatili - Programmazione 09-10
20. While - for differences
While For
> Undetermined > Determined
> No counter > Counter
20
Paolo Marcatili - Programmazione 09-10
21. While example
Approx. solution of x^2-2=0
(Newton’s method)
my $sol=0.5;
my $err=$sol**2-2;
while ($err>.1){
$sol-=($sol**2-2)/(2*$sol);
$err=$sol**2-2;
print “Error=$errn”;
}
21
Paolo Marcatili - Programmazione 09-10
22. 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;
22
Paolo Marcatili - Programmazione 09-10
26. 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!
26
Paolo Marcatili - Programmazione 09-10
27. 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.
27
Paolo Marcatili - Programmazione 09-10
28. $_
> Perl default scalar value that is used when a
variable is not explicitly specified.
> Can be used in
> For Loops
> File Handling
> Regular Expressions
28
Paolo Marcatili - Programmazione 09-10
29. $_ and For Loops
> Example using $_ in a for loop
@array = ( “Perl”, “C”, “Java” );
for(@array) {
print $_ . “is a language I known”;
}
> Output :
Perl is a language I know.
C is a language I know.
Java is a language I know.
29
Paolo Marcatili - Programmazione 09-10
30. $_ 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 $_
30
Paolo Marcatili - Programmazione 09-10
32. Opendir & readdir
> Just like open, but for dirs
# load all files of the "data/" folder into the @files array
opendir(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>";
32
Paolo Marcatili - Programmazione 09-10