Beruflich Dokumente
Kultur Dokumente
Perl File Open: Creating, Reading and Writing to Files in Perl | Cave of Programming
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.
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
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
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
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