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

About $/

by lddzjwwy (Novice)
on May 24, 2013 at 08:50 UTC ( #1035087=perlquestion: print w/replies, xml ) Need Help??
lddzjwwy has asked for the wisdom of the Perl Monks concerning the following question:

Hi guys, may I set two line end characters at the same time in Perl, sth. like $/ = "\r" or "\003"; ? Thanks in advance. BR Wei

Replies are listed 'Best First'.
Re: About $/
by Athanasius (Chancellor) on May 24, 2013 at 09:59 UTC

    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,

      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
Re: About $/
by LanX (Bishop) on May 24, 2013 at 09:53 UTC

    from perlvar

    Remember: the value of $/ is a string, not a regex. awk has to be better for something. :-)

    But you change $/ after each read line!

    Cheers Rolf

    ( addicted to the Perl Programming Language)

      Nested declaration may be what you are looking for.
      $/="\r"; my @splitted=(); while(<FH>){ local $/="\003"; push @splitted,split(/$\//,$_); }

        No need for the second local $/ and the first can be made even more local:

        my @splitted = do { local $/ = "\r"; map { split m/\003/ => $_, -1 } <>; };

        Enjoy, Have FUN! H.Merijn
        This way works very well! Thank you so much! :)
Re: About $/
by tinita (Parson) on May 24, 2013 at 11:45 UTC
Re: About $/
by kcott (Chancellor) on May 25, 2013 at 07:12 UTC

    G'day lddzjwwy,

    Perhaps something along these lines:

    $ perl -Mstrict -Mwarnings -E ' # Create test data use autodie qw{:all}; use File::Temp; my $fh = File::Temp->new(UNLINK => 1); print $fh "qwe\rasd\003zxc\r123"; seek $fh, 0, 0; # Read data splitting on "\r" or "\003" say for split /(?>\r|\003)/ => do { local $/; <$fh> }; ' qwe asd zxc 123

    I assume you've picked "\003" purely as an example; I've used the same in the code above. However, using constructs like \o{...}, \x{...}, etc. may save you some interpolation issues down the track (see escape sequences in perlop - Quote and Quote-like Operators).

    -- Ken

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1035087]
Front-paged by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2018-07-20 03:30 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (423 votes). Check out past polls.