SlideShare a Scribd company logo
Read and Write Files
     with Perl




Paolo Marcatili - Programmazione 09-10
Agenda

> Perl IO
> Open a File
> Write on Files
> Read from Files
> While loop




                                                 2
        Paolo Marcatili - Programmazione 09-10
Perl IO

(IO means Input/Output)




Paolo Marcatili - Programmazione 09-10
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
Why IO?

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




                                                 5
        Paolo Marcatili - Programmazione 09-10
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
IO types

Main Inputs
> Keyboard
> File
> Errors
Main outputs
> Display
> File


                                                 7
        Paolo Marcatili - Programmazione 09-10
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
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
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
Open/Write a file




Paolo Marcatili - Programmazione 09-10
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
close

When finished we have to close it:
close OUT;

If you dont, Santa will bring no gift.




                                                      13
             Paolo Marcatili - Programmazione 09-10
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
Read from Files




Paolo Marcatili - Programmazione 09-10
Read

open(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;



                                                              16
               Paolo Marcatili - Programmazione 09-10
Read with loops

Problems:
> It’s long
> File size unknown

solution:
Use loops



                                                 17
        Paolo Marcatili - Programmazione 09-10
While loop




Paolo Marcatili - Programmazione 09-10
While


while (condition){
               do something…

}




                                                 19
        Paolo Marcatili - Programmazione 09-10
While - for differences

While                       For

> Undetermined              > Determined
> No counter                > Counter




                                                 20
        Paolo Marcatili - Programmazione 09-10
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
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
Redirect outputs




Paolo Marcatili - Programmazione 09-10
Redirections

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

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

 close(STDOUT);
 close(STDERR);


                                                      24
         Paolo Marcatili - Programmazione 09-10
@ARGV




Paolo Marcatili - Programmazione 09-10
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
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
$_

> 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
$_ 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
$_ 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
Opendir, readdir




Paolo Marcatili - Programmazione 09-10
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

More Related Content

What's hot

Advanced modulinos trial
Advanced modulinos trialAdvanced modulinos trial
Advanced modulinos trial
brian d foy
 
SPL, not a bridge too far
SPL, not a bridge too farSPL, not a bridge too far
SPL, not a bridge too far
Michelangelo van Dam
 
Php mysql
Php mysqlPhp mysql
Php mysql
Alebachew Zewdu
 
Advanced modulinos
Advanced modulinosAdvanced modulinos
Advanced modulinosbrian d foy
 
Introduction to Perl Programming
Introduction to Perl ProgrammingIntroduction to Perl Programming
Introduction to Perl Programming
Collaboration Technologies
 
08 Advanced PHP #burningkeyboards
08 Advanced PHP #burningkeyboards08 Advanced PHP #burningkeyboards
08 Advanced PHP #burningkeyboards
Denis Ristic
 
Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)Kang-min Liu
 
Codes
CodesCodes
Javascript basics
Javascript basicsJavascript basics
Javascript basics
Fin Chen
 
Old Oracle Versions
Old Oracle VersionsOld Oracle Versions
Old Oracle VersionsJeffrey Kemp
 
Code Generation in PHP - PHPConf 2015
Code Generation in PHP - PHPConf 2015Code Generation in PHP - PHPConf 2015
Code Generation in PHP - PHPConf 2015
Lin Yo-An
 
Quick tour of PHP from inside
Quick tour of PHP from insideQuick tour of PHP from inside
Quick tour of PHP from inside
julien pauli
 
Phpをいじり倒す10の方法
Phpをいじり倒す10の方法Phpをいじり倒す10の方法
Phpをいじり倒す10の方法Moriyoshi Koizumi
 
Melhorando sua API com DSLs
Melhorando sua API com DSLsMelhorando sua API com DSLs
Melhorando sua API com DSLs
Augusto Pascutti
 
CyberLink LabelPrint 2.5 Exploitation Process
CyberLink LabelPrint 2.5 Exploitation ProcessCyberLink LabelPrint 2.5 Exploitation Process
CyberLink LabelPrint 2.5 Exploitation Process
Thomas Gregory
 
Exploit Development: EzServer Buffer Overflow oleh Tom Gregory
Exploit Development: EzServer Buffer Overflow oleh Tom GregoryExploit Development: EzServer Buffer Overflow oleh Tom Gregory
Exploit Development: EzServer Buffer Overflow oleh Tom Gregory
zakiakhmad
 
SfCon: Test Driven Development
SfCon: Test Driven DevelopmentSfCon: Test Driven Development
SfCon: Test Driven Development
Augusto Pascutti
 

What's hot (20)

Advanced modulinos trial
Advanced modulinos trialAdvanced modulinos trial
Advanced modulinos trial
 
Php arduino
Php arduinoPhp arduino
Php arduino
 
SPL, not a bridge too far
SPL, not a bridge too farSPL, not a bridge too far
SPL, not a bridge too far
 
Hachiojipm11
Hachiojipm11Hachiojipm11
Hachiojipm11
 
Php mysql
Php mysqlPhp mysql
Php mysql
 
Advanced modulinos
Advanced modulinosAdvanced modulinos
Advanced modulinos
 
Introduction to Perl Programming
Introduction to Perl ProgrammingIntroduction to Perl Programming
Introduction to Perl Programming
 
08 Advanced PHP #burningkeyboards
08 Advanced PHP #burningkeyboards08 Advanced PHP #burningkeyboards
08 Advanced PHP #burningkeyboards
 
Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)
 
Codes
CodesCodes
Codes
 
Javascript basics
Javascript basicsJavascript basics
Javascript basics
 
Old Oracle Versions
Old Oracle VersionsOld Oracle Versions
Old Oracle Versions
 
Code Generation in PHP - PHPConf 2015
Code Generation in PHP - PHPConf 2015Code Generation in PHP - PHPConf 2015
Code Generation in PHP - PHPConf 2015
 
Perl6 grammars
Perl6 grammarsPerl6 grammars
Perl6 grammars
 
Quick tour of PHP from inside
Quick tour of PHP from insideQuick tour of PHP from inside
Quick tour of PHP from inside
 
Phpをいじり倒す10の方法
Phpをいじり倒す10の方法Phpをいじり倒す10の方法
Phpをいじり倒す10の方法
 
Melhorando sua API com DSLs
Melhorando sua API com DSLsMelhorando sua API com DSLs
Melhorando sua API com DSLs
 
CyberLink LabelPrint 2.5 Exploitation Process
CyberLink LabelPrint 2.5 Exploitation ProcessCyberLink LabelPrint 2.5 Exploitation Process
CyberLink LabelPrint 2.5 Exploitation Process
 
Exploit Development: EzServer Buffer Overflow oleh Tom Gregory
Exploit Development: EzServer Buffer Overflow oleh Tom GregoryExploit Development: EzServer Buffer Overflow oleh Tom Gregory
Exploit Development: EzServer Buffer Overflow oleh Tom Gregory
 
SfCon: Test Driven Development
SfCon: Test Driven DevelopmentSfCon: Test Driven Development
SfCon: Test Driven Development
 

Viewers also liked

Iostreams
IostreamsIostreams
Iostreams
aptechsravan
 
java copy file program
java copy file programjava copy file program
java copy file program
Glen Pais
 
I/O in java Part 1
I/O in java Part 1I/O in java Part 1
I/O in java Part 1
ashishspace
 
2CPP17 - File IO
2CPP17 - File IO2CPP17 - File IO
2CPP17 - File IO
Michael Heron
 
Servlet Event framework
Servlet Event frameworkServlet Event framework
Servlet Event framework
AshishSingh Bhatia
 
Java I/O Part 2
Java I/O Part 2Java I/O Part 2
Java I/O Part 2
AshishSingh Bhatia
 
Files & IO in Java
Files & IO in JavaFiles & IO in Java
Files & IO in JavaCIB Egypt
 
Chapter 6 Java IO File
Chapter 6 Java IO FileChapter 6 Java IO File
Chapter 6 Java IO File
Khirulnizam Abd Rahman
 
Chapter 5 Class File
Chapter 5 Class FileChapter 5 Class File
Chapter 5 Class File
Khirulnizam Abd Rahman
 
JRuby: Pushing the Java Platform Further
JRuby: Pushing the Java Platform FurtherJRuby: Pushing the Java Platform Further
JRuby: Pushing the Java Platform FurtherCharles Nutter
 
Featuring JDK 7 Nio 2
Featuring JDK 7 Nio 2Featuring JDK 7 Nio 2
Featuring JDK 7 Nio 2
Orange and Bronze Software Labs
 
Java Tutorial Lab 6
Java Tutorial Lab 6Java Tutorial Lab 6
Java Tutorial Lab 6
Berk Soysal
 
Java IO
Java IOJava IO
Java IO
UTSAB NEUPANE
 
Reading and writting
Reading and writtingReading and writting
Reading and writtingandreeamolnar
 
File io
File ioFile io
File io
Pri Dhaka
 
Various io stream classes .47
Various io stream classes .47Various io stream classes .47
Various io stream classes .47myrajendra
 
I/O In Java Part 2
I/O In Java Part 2I/O In Java Part 2
I/O In Java Part 2
ashishspace
 
Xlab #1: Advantages of functional programming in Java 8
Xlab #1: Advantages of functional programming in Java 8Xlab #1: Advantages of functional programming in Java 8
Xlab #1: Advantages of functional programming in Java 8
XSolve
 
Java Input Output (java.io.*)
Java Input Output (java.io.*)Java Input Output (java.io.*)
Java Input Output (java.io.*)
Om Ganesh
 
Java Course 8: I/O, Files and Streams
Java Course 8: I/O, Files and StreamsJava Course 8: I/O, Files and Streams
Java Course 8: I/O, Files and Streams
Anton Keks
 

Viewers also liked (20)

Iostreams
IostreamsIostreams
Iostreams
 
java copy file program
java copy file programjava copy file program
java copy file program
 
I/O in java Part 1
I/O in java Part 1I/O in java Part 1
I/O in java Part 1
 
2CPP17 - File IO
2CPP17 - File IO2CPP17 - File IO
2CPP17 - File IO
 
Servlet Event framework
Servlet Event frameworkServlet Event framework
Servlet Event framework
 
Java I/O Part 2
Java I/O Part 2Java I/O Part 2
Java I/O Part 2
 
Files & IO in Java
Files & IO in JavaFiles & IO in Java
Files & IO in Java
 
Chapter 6 Java IO File
Chapter 6 Java IO FileChapter 6 Java IO File
Chapter 6 Java IO File
 
Chapter 5 Class File
Chapter 5 Class FileChapter 5 Class File
Chapter 5 Class File
 
JRuby: Pushing the Java Platform Further
JRuby: Pushing the Java Platform FurtherJRuby: Pushing the Java Platform Further
JRuby: Pushing the Java Platform Further
 
Featuring JDK 7 Nio 2
Featuring JDK 7 Nio 2Featuring JDK 7 Nio 2
Featuring JDK 7 Nio 2
 
Java Tutorial Lab 6
Java Tutorial Lab 6Java Tutorial Lab 6
Java Tutorial Lab 6
 
Java IO
Java IOJava IO
Java IO
 
Reading and writting
Reading and writtingReading and writting
Reading and writting
 
File io
File ioFile io
File io
 
Various io stream classes .47
Various io stream classes .47Various io stream classes .47
Various io stream classes .47
 
I/O In Java Part 2
I/O In Java Part 2I/O In Java Part 2
I/O In Java Part 2
 
Xlab #1: Advantages of functional programming in Java 8
Xlab #1: Advantages of functional programming in Java 8Xlab #1: Advantages of functional programming in Java 8
Xlab #1: Advantages of functional programming in Java 8
 
Java Input Output (java.io.*)
Java Input Output (java.io.*)Java Input Output (java.io.*)
Java Input Output (java.io.*)
 
Java Course 8: I/O, Files and Streams
Java Course 8: I/O, Files and StreamsJava Course 8: I/O, Files and Streams
Java Course 8: I/O, Files and Streams
 

Similar to Perl IO

Perl Moderno
Perl ModernoPerl Moderno
Perl Moderno
Tiago Peczenyj
 
Getting modern with logging via log4perl
Getting modern with logging via log4perlGetting modern with logging via log4perl
Getting modern with logging via log4perl
Dean Hamstead
 
Perl one-liners
Perl one-linersPerl one-liners
Perl one-liners
daoswald
 
groovy & grails - lecture 2
groovy & grails - lecture 2groovy & grails - lecture 2
groovy & grails - lecture 2
Alexandre Masselot
 
2010 Smith Scripting101
2010 Smith Scripting1012010 Smith Scripting101
2010 Smith Scripting101
bokonen
 
Good Evils In Perl
Good Evils In PerlGood Evils In Perl
Good Evils In PerlKang-min Liu
 
Perl Sucks - and what to do about it
Perl Sucks - and what to do about itPerl Sucks - and what to do about it
Perl Sucks - and what to do about it
2shortplanks
 
Una historia de ds ls en ruby
Una historia de ds ls en rubyUna historia de ds ls en ruby
Una historia de ds ls en ruby
Leonardo Soto
 
Yapc::NA::2009 - Command Line Perl
Yapc::NA::2009 - Command Line PerlYapc::NA::2009 - Command Line Perl
Yapc::NA::2009 - Command Line Perl
Bruce Gray
 
Overloading Perl OPs using XS
Overloading Perl OPs using XSOverloading Perl OPs using XS
Overloading Perl OPs using XS
ℕicolas ℝ.
 
Bag of tricks
Bag of tricksBag of tricks
Bag of tricks
brian d foy
 
Verilog Lecture5 hust 2014
Verilog Lecture5 hust 2014Verilog Lecture5 hust 2014
Verilog Lecture5 hust 2014
Béo Tú
 
Bioinformatica 29-09-2011-p1-introduction
Bioinformatica 29-09-2011-p1-introductionBioinformatica 29-09-2011-p1-introduction
Bioinformatica 29-09-2011-p1-introduction
Prof. Wim Van Criekinge
 
Perl courseparti
Perl coursepartiPerl courseparti
Perl coursepartiernlow
 

Similar to Perl IO (20)

Master perl io_2011
Master perl io_2011Master perl io_2011
Master perl io_2011
 
Regexp Master
Regexp MasterRegexp Master
Regexp Master
 
Perl Moderno
Perl ModernoPerl Moderno
Perl Moderno
 
Hashes Master
Hashes MasterHashes Master
Hashes Master
 
Getting modern with logging via log4perl
Getting modern with logging via log4perlGetting modern with logging via log4perl
Getting modern with logging via log4perl
 
Perl one-liners
Perl one-linersPerl one-liners
Perl one-liners
 
groovy & grails - lecture 2
groovy & grails - lecture 2groovy & grails - lecture 2
groovy & grails - lecture 2
 
2010 Smith Scripting101
2010 Smith Scripting1012010 Smith Scripting101
2010 Smith Scripting101
 
Good Evils In Perl
Good Evils In PerlGood Evils In Perl
Good Evils In Perl
 
Perl Sucks - and what to do about it
Perl Sucks - and what to do about itPerl Sucks - and what to do about it
Perl Sucks - and what to do about it
 
Una historia de ds ls en ruby
Una historia de ds ls en rubyUna historia de ds ls en ruby
Una historia de ds ls en ruby
 
Yapc::NA::2009 - Command Line Perl
Yapc::NA::2009 - Command Line PerlYapc::NA::2009 - Command Line Perl
Yapc::NA::2009 - Command Line Perl
 
Auto start
Auto startAuto start
Auto start
 
Auto start
Auto startAuto start
Auto start
 
Overloading Perl OPs using XS
Overloading Perl OPs using XSOverloading Perl OPs using XS
Overloading Perl OPs using XS
 
Bag of tricks
Bag of tricksBag of tricks
Bag of tricks
 
Perl Intro 4 Debugger
Perl Intro 4 DebuggerPerl Intro 4 Debugger
Perl Intro 4 Debugger
 
Verilog Lecture5 hust 2014
Verilog Lecture5 hust 2014Verilog Lecture5 hust 2014
Verilog Lecture5 hust 2014
 
Bioinformatica 29-09-2011-p1-introduction
Bioinformatica 29-09-2011-p1-introductionBioinformatica 29-09-2011-p1-introduction
Bioinformatica 29-09-2011-p1-introduction
 
Perl courseparti
Perl coursepartiPerl courseparti
Perl courseparti
 

Recently uploaded

State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
Prayukth K V
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
Sri Ambati
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
SOFTTECHHUB
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
KatiaHIMEUR1
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
Aftab Hussain
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
Ana-Maria Mihalceanu
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
Assure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyesAssure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
 
The Metaverse and AI: how can decision-makers harness the Metaverse for their...
The Metaverse and AI: how can decision-makers harness the Metaverse for their...The Metaverse and AI: how can decision-makers harness the Metaverse for their...
The Metaverse and AI: how can decision-makers harness the Metaverse for their...
Jen Stirrup
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Aggregage
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
Adtran
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
Guy Korland
 
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Nexer Digital
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
Ralf Eggert
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
UiPathCommunity
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance
 

Recently uploaded (20)

State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
Assure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyesAssure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyes
 
The Metaverse and AI: how can decision-makers harness the Metaverse for their...
The Metaverse and AI: how can decision-makers harness the Metaverse for their...The Metaverse and AI: how can decision-makers harness the Metaverse for their...
The Metaverse and AI: how can decision-makers harness the Metaverse for their...
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
 
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
 

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
  • 11. Open/Write a file 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
  • 15. Read from Files Paolo Marcatili - Programmazione 09-10
  • 16. Read open(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; 16 Paolo Marcatili - Programmazione 09-10
  • 17. Read with loops Problems: > It’s long > File size unknown solution: Use loops 17 Paolo Marcatili - Programmazione 09-10
  • 18. While loop Paolo Marcatili - Programmazione 09-10
  • 19. While while (condition){ do something… } 19 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
  • 23. Redirect outputs Paolo Marcatili - Programmazione 09-10
  • 24. Redirections #!/usr/bin/perl open(STDOUT, ">foo.out") || die "Can't redirect stdout"; open(STDERR, ">&STDOUT") || die "Can't dup stdout"; select(STDERR); $| = 1; # make unbuffered select(STDOUT); $| = 1; # make unbuffered close(STDOUT); close(STDERR); 24 Paolo Marcatili - Programmazione 09-10
  • 25. @ARGV 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
  • 31. Opendir, readdir 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