Perl Io Master

447 views
391 views

Published on

Published in: Education, Technology
1 Comment
0 Likes
Statistics
Notes
  • Be the first to like this

No Downloads
Views
Total views
447
On SlideShare
0
From Embeds
0
Number of Embeds
54
Actions
Shares
0
Downloads
11
Comments
1
Likes
0
Embeds 0
No embeds

No notes for slide

Perl Io Master

  1. 1. Re ad and Write File s w ith Pe rl Pao lo Marc atili - Pro grammazio ne 08-09
  2. 2. 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
  3. 3. Pe rl I O (I me ans I O nput/Output) Pao lo Marc atili - Pro grammazio ne 08-09
  4. 4. 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
  5. 5. 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
  6. 6. 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
  7. 7. 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
  8. 8. 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
  9. 9. 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
  10. 10. 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
  11. 11. Ope n/Write a file Pao lo Marc atili - Pro grammazio ne 08-09
  12. 12. 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
  13. 13. 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
  14. 14. 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
  15. 15. Re ad fro m File s Pao lo Marc atili - Pro grammazio ne 08-09
  16. 16. 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
  17. 17. 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
  18. 18. While lo o p Pao lo Marc atili - Pro grammazio ne 08-09
  19. 19. While w hile (c o nditio n){ do s o me thing… } 19 Pao lo Marc atili - Pro grammazio ne 08-09
  20. 20. 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
  21. 21. 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
  22. 22. 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
  23. 23. Re dire c t o utputs Pao lo Marc atili - Pro grammazio ne 08-09
  24. 24. 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

×