Sie sind auf Seite 1von 6

4/29/13

Perl File Open: Creating, Reading and Writing to Files in Perl | Cave of Programming

Perl File Open: Creating, Reading and Writing to Files in Perl


1

Dealing with files in Perl is very easy once you get used to the slightly odd syntax. Here are some examples to get you started.

Open a File in Perl


To open a file in Perl, just the open() subroutine. Heres an example of a program that opens a file, reads the file one line at a time and prints each line to the terminal. u s es t r i c t ; u s ew a r n i n g s ; #P u tt h ef i l en a m ei nas t r i n gv a r i a b l e #s ow ec a nu s ei tb o t ht oo p e nt h ef i l e #a n dt or e f e rt oi na ne r r o rm e s s a g e #i fn e e d e d . m y$ f i l e=" t e m p . t x t " ; #U s et h eo p e n ( )f u n c t i o nt oo p e nt h ef i l e . u n l e s s ( o p e nF I L E ,$ f i l e ){ #D i ew i t he r r o rm e s s a g e #i fw ec a n ' to p e ni t . d i e" \ n U n a b l et oo p e n$ f i l e \ n " ; } #R e a dt h ef i l eo n el i n ea tat i m e . w h i l e ( m y$ l i n e=< F I L E > ){ #W e ' l lj u s tp r i n tt h el i n ef o rn o w . p r i n t$ l i n e ; } #c l o s et h ef i l e . c l o s eF I L E ; If you want to read binary files in Perl, you need to set the binmode on the file handle.

Learn Perl By Doing It

www.caveofprogramming.com/perl/perl-file-open-creating-reading-and-writing-to-files-in-perl/

1/6

4/29/13

Perl File Open: Creating, Reading and Writing to Files in Perl | Cave of Programming

Get the complete course here Includes 11 completely free videos no subscription necessary.

The following example also illustrates how you can read an entire file in one go. Of course if the file is large and your memory limited, this might be a bad idea. Usually you probably want to read a file in chunks, writing out the chunks to another file as you go. u s es t r i c t ; u s ew a r n i n g s ; #P u tt h ef i l en a m ei nas t r i n gv a r i a b l e #s ow ec a nu s ei tb o t ht oo p e nt h ef i l e #a n dt or e f e rt oi na ne r r o rm e s s a g e #i fn e e d e d . m y$ f i l e=" t e m p . b i n " ; #U s et h eo p e n ( )f u n c t i o nt oo p e nt h ef i l e . u n l e s s ( o p e nF I L E ,$ f i l e ){ #D i ew i t he r r o rm e s s a g e #i fw ec a n ' to p e ni t . d i e" \ n U n a b l et oo p e n$ f i l e \ n " ; } #T e l lP e r lw ew a n tt or e a dd a t aa sb i n a r y . b i n m o d e ( F I L E ) ; #G e tr i do ft h el i n es e p a r a t o r . #T h i sa l l o w su st or e a de v e r y t h i n g #i no n eg o . u n d e f$ / ; #R e a dt h ee n t i r ef i l e .I fy o ud o n ' tw a n t #t or e a da l lo fi ta to n c e ,y o un e e dt h e #r e a d ( )s u b r o u t i n e . m y$ c o n t e n t s=< F I L E > ; p r i n t" R e a d".l e n g t h ( $ c o n t e n t s )."b y t e s \ n " ; #c l o s et h ef i l e . c l o s eF I L E ; R e a d5 2 0b y t e s If you are dealing with a text file, its often useful to set the file record separator $/ (set to the newline character by default) to some other value, such as a particular closing XML tag. Undefining it altogether, as in the above example, causes the entire file to be read at once.
www.caveofprogramming.com/perl/perl-file-open-creating-reading-and-writing-to-files-in-perl/ 2/6

4/29/13

Perl File Open: Creating, Reading and Writing to Files in Perl | Cave of Programming

Creating Files In Perl


To create a file in Perl, you also use open(). The difference is that you need to prefix the file name with a > character to make Perl open the file for writing. Any existing file with the name you supply to open() will be overwritten, unless you specify >> instead, which opens a file for appending. This program creates a file called temp.txt and writes two lines to it. u s es t r i c t ; u s ew a r n i n g s ; #P u tt h ef i l en a m ei nas t r i n gv a r i a b l e #s ow ec a nu s ei tb o t ht oo p e nt h ef i l e #a n dt or e f e rt oi na ne r r o rm e s s a g e #i fn e e d e d . m y$ f i l e=" t e m p . t x t " ; #U s et h eo p e n ( )f u n c t i o nt oc r e a t et h ef i l e . u n l e s s ( o p e nF I L E ,' > ' . $ f i l e ){ #D i ew i t he r r o rm e s s a g e #i fw ec a n ' to p e ni t . d i e" \ n U n a b l et oc r e a t e$ f i l e \ n " ; } #W r i t es o m et e x tt ot h ef i l e . p r i n tF I L E" H e l l ot h e r e \ n " ; p r i n tF I L E" H o wa r ey o u ? \ n " ; #c l o s et h ef i l e . c l o s eF I L E ;

Open A Text File, Process It and Write to Another File


Heres a complete example of a program that opens a text file in Perl, reads it line by line, carries out processing on each line and writes the results to another file. #A l w a y sd e f i n et h e s et w oa tt h et o p so f #y o u rs c r i p t s . u s es t r i c t ; u s ew a r n i n g s ; #T h i st u r n so f fo u t p u tb u f f e r i n g . #U s e f u lf o rr e a l t i m ed i s p l a yo f #p r o g r e s so re r r o rm e s s a g e s . $ | = 1 ; #T h i si sf o rg e t t i n gc o m m a n d l i n eo p t i o n s . #C o u l da l t e r n a t i v e l yu s e@ A R G Vi n s t e a d . u s eG e t o p t : : S t d ; #R e t u r nau s a g em e s s a g e .U s u a l l yw e ' di n d e n tt h e #c o n t e n t so fas u b r o u t i n e ,b u th e r ew ec a n ' ts i n c e #t h a tw o u l da f f e c tt h eu s a g em e s s a g e . s u bu s a g e{ r e t u r nq | u s a g e : p r o c e s s . p li< i n p u tf i l e >o< o u t p u tf i l e >
www.caveofprogramming.com/perl/perl-file-open-creating-reading-and-writing-to-files-in-perl/ 3/6

4/29/13

Perl File Open: Creating, Reading and Writing to Files in Perl | Cave of Programming

R e a d sa l ll i n e si n< i n p u tf i l e > ,c h a n g e sa l lo c c u r e n c e so f ' d o g 't o' c a t 'a n dw r i t e st h er e s u l t st o< o u t p u tf i l e > . < i n p u tf i l e >i t s e l fi sn o tc h a n g e d . | ; } s u bm a i n{ #C o l l e c tt h eia n doo p t i o n sf r o mt h ec o m m a n dl i n e . m y% o p t s ; #T h ec o l o n ss p e c i f yt h a tt h ep r e c e e d i n gf l a g st a k e #a r g u m e n t s( f i l en a m e si nt h i sc a s e ) g e t o p t s ( ' i : o : ' ,\ % o p t s ) ; #C h e c kw eg o tt h ef l a g sa n da r g u m e n t sw en e e d . u n l e s s ( $ o p t s { ' i ' }a n d$ o p t s { ' o ' } ){ d i eu s a g e ( ) ; } #N o ww e ' v eg o tt h ei n p u ta n do u t p u tf i l en a m e s . m y$ i n p u t=$ o p t s { ' i ' } ; m y$ o u t p u t=$ o p t s { ' o ' } ; #T r yt oo p e nt h ei n p u tf i l e . u n l e s s ( o p e nI N P U T ,$ i n p u t ){ d i e" \ n U n a b l et oo p e n' $ i n p u t ' \ n " ; } #T r yt oc r e a t et h eo u t p u tf i l e #( o p e ni tf o rw r i t i n g ) u n l e s s ( o p e nO U T P U T ,' > ' . $ o u t p u t ){ d i e" \ n U n a b l et oc r e a t e' $ o u t p u t ' \ n " ; } #R e a do n el i n ea tat i m ef r o mt h ei n p u tf i l e #t i l lw e ' v er e a dt h e ma l l . w h i l e ( m y$ l i n e=< I N P U T > ){ #C h a n g ed o gt oc a t $ l i n e= ~s / d o g / c a t / i g ; #W r i t et h el i n et ot h eo u t p u tf i l e . p r i n tO U T P U T$ l i n e ; #P r i n tap r o g r e s si n d i c a t o r . p r i n t' . ' ; } #C l o s et h ef i l e s . c l o s eI N P U T ; c l o s eO U T P U T ; } m a i n ( ) ;
All page s on this site are copyright 2013 John W . Purce ll

www.caveofprogramming.com/perl/perl-file-open-creating-reading-and-writing-to-files-in-perl/

4/6

4/29/13

Perl File Open: Creating, Reading and Writing to Files in Perl | Cave of Programming

Post to Facebook 1 1 1 11 1 1 Like One person likes this. 1 1 1 Leave Post a Reply Add to to LinkedIn Your email address will not be published. Required fields are marked * Add to Google Bookmarks Twitter Post to Delicious Name * Post to StumbleUpon Send via Gmail Email * Send via E-mail program Print with PrintFriendly Post to Digg WebsiteAdd to MySpace Add to Tumblr

Comment You may use these HTML tags and attributes: < ah r e f = " "t i t l e = " " >< a b b rt i t l e = " " > < a c r o n y mt i t l e = " " >< b >< b l o c k q u o t ec i t e = " " >< c i t e >< c o d e >< d e ld a t e t i m e = " " > < e m >< i >< qc i t e = " " >< s t r i k e >< s t r o n g >
Post Comment

Posted in Perl | Tagged file, perl, programming | C opyright 2013 John W . Purce ll Contact

Home

Learn Java Swing


Name Email Close Dialog

Contact

www.caveofprogramming.com/perl/perl-file-open-creating-reading-and-writing-to-files-in-perl/

5/6

4/29/13

Perl File Open: Creating, Reading and Writing to Files in Perl | Cave of Programming

Complete video course View 7 videos completely free Nearly all major widgets Databases Design patterns Animation

Web Programming: Servlets and JSPs

Complete video course View 7 videos completely free Create Java web apps Learn JSPs, servlets and JSTL Connect to Databases Discover how to deploy for free

www.caveofprogramming.com/perl/perl-file-open-creating-reading-and-writing-to-files-in-perl/

6/6

Das könnte Ihnen auch gefallen