Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^4: Get record separator of a file

by davido (Archbishop)
on Nov 14, 2012 at 00:08 UTC ( #1003723=note: print w/ replies, xml ) Need Help??


in reply to Re^3: Get record separator of a file
in thread Get record separator of a file

Now we're getting somewhere (I think). You should be able to take advantage of Perl's :crlf IO layer to handle the problem for you. I'll let you test this yourself I've tested this, and here is how I think it would work out.

First, Tie::File seems to be "layers" unaware, which is fine, except that you'll have to open the file explicitly, and close it again when you're done, rather than letting Tie::File handle those operations. This gives you control over what layers are applied to the file handle.

use strict; use warnings; use Tie::File; use Scalar::Util qw( weaken ); open my $fh, '+<:crlf', 'filename.ext' or die $!; my @array; my $t = tie @array, 'Tie::File', $fh; weaken $t; # tie holds its own ref. We don't want a mem leak. # Work, work, work... untie @array; close $fh or die $!;

The relevant explanation of ':crlf' from the POD is: " On read converts pairs of CR,LF to a single "\n" newline character. On write converts each "\n" to a CR,LF pair." Since this happens behind the scenes, it should play nice with Tie::File, but I would test on some copies of the files first to be sure.

Updated: Added weaken to eliminate a potential memory leak, since tie also holds a ref to its own object.


Dave


Comment on Re^4: Get record separator of a file
Select or Download Code
Replies are listed 'Best First'.
[SOLVED]Re^5: Get record separator of a file
by karlgoethebier (Vicar) on Nov 14, 2012 at 13:38 UTC

    Update:

    Sorry, i saw it to late. Cool, i didn't know this.

    Works when called with $fh. Very nice!

    Thank you very much and best regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

Re^5: Get record separator of a file
by karlgoethebier (Vicar) on Nov 14, 2012 at 19:00 UTC

    Just thank you very much, Karl

    «The Crux of the Biscuit is the Apostrophe»

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1003723]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (13)
As of 2015-07-30 14:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (271 votes), past polls