Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

perl read a file on Windows is very strange

by perlisfun (Sexton)
on Nov 22, 2010 at 21:57 UTC ( [id://873053]=perlquestion: print w/replies, xml ) Need Help??

perlisfun has asked for the wisdom of the Perl Monks concerning the following question:

PerlIsFun

i have simple log file on my PC. it is as below:


PeopleSoft Project Command Line Copy To File Project Name: PT_TEST_STAT_EXP Tools Release: 8.49.24 2010-11-22 : 16.00.29 -CT Source Database Type = ORACLE -CD Source Database Name = PSQSTG -CO Source Database Operator = PSAPI -FP File Path = Q:\Salem_RE\csr_export -AF Keep Target Audit Flags = 0 -DDL Keep Target DDL = 1 -CFD Keep Target Chartfield PageField Display Size = 0 -CFF Keep Target Chartfield DbField Format = 0 -PPL Keep Target Portal Structure Permission List = 0 Project Q:\Salem_RE\csr_export\PT_TEST_STAT_EXP already exists, contin +uing processing, switch set to overwrite. Records Application Upgrade Copy started: 2010-11-22-16.00.30 Records Application Upgrade Copy ended: 2010-11-22-16.00.30 Fields Application Upgrade Copy started: 2010-11-22-16.00.30 Fields Application Upgrade Copy ended: 2010-11-22-16.00.30 Saving Project Definition... Saving Project Definition...Complete. Command line process successfully completed.

It looks normal via notepad or wordpad. I wrote a very simple script to just print this file out. Following is what i got:


P e o p l e S o f t P r o j e c t C o m m a n d L i n e C o p y T o F i l e P r o j e c t N a m e : P T _ T E S T _ S T A T _ E X P T o o l s R e l e a s e : 8 . 4 9 . 2 4 2 0 1 0 - 1 1 - 2 2 : 1 6 . 0 0 . 2 9 - C T S o u r c e D a t a b a s e T y p e = O R A C L E - C D S o u r c e D a t a b a s e N a m e = P S Q S T G - C O S o u r c e D a t a b a s e O p e r a t o r = P S A P + I - F P F i l e P a t h = Q : \ S a l e m _ R E \ c s r _ e x p + o r t - A F K e e p T a r g e t A u d i t F l a g s = 0 - D D L K e e p T a r g e t D D L = 1 - C F D K e e p T a r g e t C h a r t f i e l d P a g e F i e + l d D i s p l a y S i z e = 0 - C F F K e e p T a r g e t C h a r t f i e l d D b F i e l d + F o r m a t = 0 - P P L K e e p T a r g e t P o r t a l S t r u c t u r e P + e r m i s s i o n L i s t = 0 P r o j e c t Q : \ S a l e m _ R E \ c s r _ e x p o r t \ P T _ T + E S T _ S T A T _ E X P a l r e a d y e x i s t s , c o n t i +n u i n g p r o c e s s i n g , s w i t c h s e t t o o v e + r w r i t e . R e c o r d s A p p l i c a t i o n U p g r a d e C o p y s t + a r t e d : 2 0 1 0 - 1 1 - 2 2 - 1 6 . 0 0 . 3 0 R e c o r d s A p p l i c a t i o n U p g r a d e C o p y e n + d e d : 2 0 1 0 - 1 1 - 2 2 - 1 6 . 0 0 . 3 0 F i e l d s A p p l i c a t i o n U p g r a d e C o p y s t a + r t e d : 2 0 1 0 - 1 1 - 2 2 - 1 6 . 0 0 . 3 0 F i e l d s A p p l i c a t i o n U p g r a d e C o p y e n d + e d : 2 0 1 0 - 1 1 - 2 2 - 1 6 . 0 0 . 3 0 S a v i n g P r o j e c t D e f i n i t i o n . . . S a v i n g P r o j e c t D e f i n i t i o n . . . C o m p l e t + e . C o m m a n d l i n e p r o c e s s s u c c e s s f u l l y c + o m p l e t e d .

perl guru, take look at my code. where i can go wrong?

Thanks,

#!perl open(F, "<", "C:\\TEMP\\PT_TEST_STAT_EXP_export.log"); while ( <F> ) { print "$_"; } close(F);

Replies are listed 'Best First'.
Re: perl read a file on Windows is very strange
by Corion (Patriarch) on Nov 22, 2010 at 22:10 UTC

    This looks as if your file is in UTF-16LE encoding. Use the following to read it:

    my $filename = "C:\\TEMP\\PT_TEST_STAT_EXP_export.log"; open my $fh, '<:encoding(UTF-16LE)', $filename or die "Couldn't read '$filename': $! / $^E"; while (<$fh>) { ... };

      That will fail to convert CR LF to LF even though :crlf is used, and it will corrupt a number of character combinations. (Specifically, the character U+0A0D and pairs of the form U+0Dxx U+xx0A.)

      Fix if you want CR LF ⇒ LF conversion:

      open my $fh, '<:raw:perlio:encoding(UTF-16LE):crlf', $filename or die "Couldn't read '$filename': $! / $^E";

      Fix if you want to leave CR LF intact:

      open my $fh, '<:raw:perlio:encoding(UTF-16LE)', $filename or die "Couldn't read '$filename': $! / $^E";

      (Mind you, corruption is very unlikely to occur since you'd have to be dealing with Malayalam characters. The non-functioning :crlf layer a more likely problem.)

      It's unfortunate that one has to go through these shenanigans when dealing with non-ASCII encodings.

        PerlIsFun
        Thank you very much. It works like charm!
Please don't use <pre>
by tod222 (Pilgrim) on Nov 23, 2010 at 03:01 UTC

    In the section Tags You Should NOT Use, Markup in the Monastery says:

    At first blush, the <pre> ...</pre pair may look like an alternative to code tags... BUT DON'T USE IT HERE chiefly because <pre>...</pre> tags will not wrap lines that are too long for the viewer's browser window.
Re: perl read a file on Windows is very strange
by fisher (Priest) on Nov 23, 2010 at 14:13 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://873053]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2024-04-25 16:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found