Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re: About $/

by Athanasius (Chancellor)
on May 24, 2013 at 09:59 UTC ( #1035100=note: print w/replies, xml ) Need Help??

in reply to About $/

No, because, as perlvar says, “the value of $/ is a string, not a regex.” — and a string can hold only one value at a time.

Perhaps you can proceed in this way: Pick one of the two end-line strings (say, \r) and assign it to $/. Then, split each line on the other end-line character (\003) and process each string as a “line.”

Or, if your file isn’t too big, you can slurp it all into memory and then split using a regex:

19:55 >perl -MData::Dump -wE "my $s = qq[fred\rwilma\003barney\rbetty\ +003]; my @lines = split /\r|\003/, $s; dd @lines;" ("fred", "wilma", "barney", "betty") 19:56 >

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Replies are listed 'Best First'.
Re^2: About $/
by FloydATC (Deacon) on May 24, 2013 at 10:52 UTC
    If all else fails, use sysread() to implement your own read buffering and split the incoming data on the fly.

    open(my $fh, '/my/huge/file') || die $!; binmode $fh; my $separator = "\r*\n|\t|whatever"; my $buffer = ''; my $block; while (sysread($fh, $block, 4096)) { $buffer .= $block; while ($buffer =~ /$separator/) { process_line($`.$&); # prematch + match $buffer = $'; # postmatch } } print "--\n"; process_line($buffer) if $buffer; # remainder, if any. close $fh;

    This gives you full control over how to split the data into lines, without significant memory overhead.

    -- Time flies when you don't know what you're doing

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1035100]
usemodperl enters an empty gate

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2018-06-23 19:00 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (125 votes). Check out past polls.