Perl Io Master
Upcoming SlideShare
Loading in...5
×
 

Perl Io Master

on

  • 1,022 views

 

Statistics

Views

Total Views
1,022
Views on SlideShare
971
Embed Views
51

Actions

Likes
0
Downloads
10
Comments
1

3 Embeds 51

http://biocomputing.it 33
http://arianna.bio.uniroma1.it 11
http://www.biocomputing.it 7

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Perl Io Master Perl Io Master Presentation Transcript

  • Re ad and Write File s w ith Pe rl Pao lo Marc atili - Pro grammazio ne 08-09
  • Age nda > Pe rl I O > Ope n a File > Write o n File s > Re ad fro m File s > While lo o p 2 Pao lo Marc atili - Pro grammazio ne 08-09
  • Pe rl I O (I me ans I O nput/Output) Pao lo Marc atili - Pro grammazio ne 08-09
  • Why I O? S inc e no w , Pe rl is #! /us r/bin/pe rl -w us e s tric t; <- ALWAYYYYYS S S S !!! my $ s tring=quot;I lo ve to mato e s !nquot;; fo r (my $ i=1 ;$ i<1 00;$ i++){ print $ s tring; } 4 Pao lo Marc atili - Pro grammazio ne 08-09
  • Why I O? But if w e w ant to do the s ame w ith a us e r-s ubmitte d s tring? 5 Pao lo Marc atili - Pro grammazio ne 08-09
  • Why I O? But if w e w ant to do the s ame w ith a us e r-s ubmitte d s tring? I c an do this ! O 6 Pao lo Marc atili - Pro grammazio ne 08-09
  • I type s O Main I nputs > Ke ybo ard > File > Erro rs Main o utputs > Dis play > File 7 Pao lo Marc atili - Pro grammazio ne 08-09
  • Mo re than to mato e s Le t’s try it: #! /us r/bin/pe rl -w us e s tric t; my $ s tring=<S T N>;fo r (my $ i=1 ;$ i<1 00;$ i+ DI +){print $ s tring;} 8 Pao lo Marc atili - Pro grammazio ne 08-09
  • Ratio nale Re ad fro m and w rite to diffe re nt me dia S T N me ans s tandard input (ke ybo ard) DI ^ this is a handle <S MT me ans H> “re ad fro m the s o urc e c o rre s po nding to handle SMTH” 9 Pao lo Marc atili - Pro grammazio ne 08-09
  • Handle s Handle s are jus t s tre ams “nic kname s ” S o me o f the m are fixe d: ST NDI <-de fault is ke ybo ard ST DOUT <-de fault is dis play ST DERR <-de fault is dis play S o me are us e r de fine d (file s ) 10 Pao lo Marc atili - Pro grammazio ne 08-09
  • Ope n/Write a file Pao lo Marc atili - Pro grammazio ne 08-09
  • o pe n We have to c re ate a handle fo r o ur file o pe n(OUT “>o ut.txt”) o r die (“Erro r o pe ning o ut.txt: $ !”); , ^ N.B. : it’s us e r de fine d, yo u de c ide it Tip “<o ut.txt” <- re ad fro m o ut.txt “>o ut.txt” <- w rite into o ut.txt “>>o ut.txt” <- appe nd to o ut.txt 12 Pao lo Marc atili - Pro grammazio ne 08-09
  • c lo s e Whe n finis he d w e have to c lo s e it: c lo s e OUT; I yo u do nt, Santa will bring no gift. f 13 Pao lo Marc atili - Pro grammazio ne 08-09
  • Print OUT #! /us r/bin/pe rl -w us e s tric t; o pe n(OUT quot;>o ut.txtquot;) || die (quot;Erro r o pe ning o ut.txt: $ !quot;); , print quot;type yo ur c laim:nquot;; my $ s tring=<ST N>;DI fo r (my $ i=1 ;$ i<1 00;$ i++){ print OUT $ s tring; } c lo s e OUT ; No w le t’s play w ith <,>,>> and file pe rmis s io ns 14 Pao lo Marc atili - Pro grammazio ne 08-09
  • Re ad fro m File s Pao lo Marc atili - Pro grammazio ne 08-09
  • Re ad o pe n(I “<s o ng.txt”) o r die (“Erro r o pe ning s o ng.txt: $ !”); N, print <IN>; print <IN>; print <I N>; print <I N>; print <I N>; print <I N>; print <I N>; print <I N>; print <I N>; print <I N>; print <I N>; print <I N>; print <I N>; print <I N>; c lo s e I N; 16 Pao lo Marc atili - Pro grammazio ne 08-09
  • Re ad w ith lo o ps Pro ble ms : > I lo ng t’s > File s ize unkno w n s o lutio n: Us e lo o ps 17 Pao lo Marc atili - Pro grammazio ne 08-09
  • While lo o p Pao lo Marc atili - Pro grammazio ne 08-09
  • While w hile (c o nditio n){ do s o me thing… } 19 Pao lo Marc atili - Pro grammazio ne 08-09
  • While - fo r diffe re nc e s While Fo r > Unde te rmine d > De te rmine d > No c o unte r > Co unte r 20 Pao lo Marc atili - Pro grammazio ne 08-09
  • While e xample Appro x. s o lutio n o f x^2-2=0 my $ e rr=1 ; my $ s o l=0; w hile ($ e rr>.1 ){ $ s o l+=.0001 ; $ e rr=$ s o l**2-2; } 21 Pao lo Marc atili - Pro grammazio ne 08-09
  • Re ad w ith w hile #! /us r/bin/pe rl -w us e s tric t; o pe n(MOD, quot;<I G.pdbquot;) || die (quot;Erro r o pe ning IG.pdb: $ !quot;); while (my $ line =<MOD>){ print s ubs tr($ line ,0,6).quot;nquot;; } c lo s e MOD; 22 Pao lo Marc atili - Pro grammazio ne 08-09
  • Re dire c t o utputs Pao lo Marc atili - Pro grammazio ne 08-09
  • Re dire c tio ns #!/us r/bin/pe rl o pe n(ST DOUT quot;>fo o .o utquot;) || die quot;Can't re dire c t s tdo utquot;; , o pe n(ST DERR, quot;>& DOUT || die quot;Can't dup s tdo utquot;; ST quot;) s e le c t(STDERR); $ | = 1 ; # make unbuffe re d s e le c t(STDOUT $ | = 1 ; ); # make unbuffe re d c lo s e (STDOUT); c lo s e (STDERR); 24