Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

paragraph mode and <DATA>

by 7stud (Deacon)
on Mar 12, 2011 at 22:53 UTC ( #892874=perlquestion: print w/replies, xml ) Need Help??

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

Dear Monks,

I can't get paragraph mode to work in the following example:

$/ = ""; while (my $para = <DATA>) { print "*" x 20, "\n"; print $para; print "*" x 20, "\n"; } __DATA__ hello world bye hello world2 bye2 end

The code just slurps the whole file. I can read the file line by line--but not paragraph by paragraph. I also tried using $/ = "\n\n", but that doesn't work either.

Replies are listed 'Best First'.
Re: paragraph mode and <DATA>
by roboticus (Chancellor) on Mar 12, 2011 at 23:24 UTC

    7stud:

    I tried your program and had the same results. When I looked at it via:

    od -t x1 892874.pl

    I noticed it had DOS/Windows line endings (\r\n). I notice that if you remove the carriage returns, it works just fine:

    $ sed -b 's/\r//g' 892874.pl >t.pl $ perl t.pl ******************** hello world bye ******************** ******************** hello world2 bye2 ******************** ******************** end ********************

    It also worked fine when I set $/ to "\r\n\r\n".

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      I noticed it had DOS/Windows line endings (\r\n). I notice that if you remove the carriage returns, it works just fine:

      Or run it on Windows.

      ******************** hello world bye ******************** ******************** hello world2 bye2 ******************** ******************** end ********************

        ikegami:

        I did the whole song and dance because it's not enough to simply run it on Windows. When I reproduced it, I was running on Windows Vista. While the most common Windows builds (ActiveState & Strawberry) wouldn't have given this problem, there are builds (such as the one I use, cygwin) which act more like a unix version.

        Since I use an editor that doesn't add "\r" to the line ends, I don't normally encounter this issue, but I've encountered it a few times, and am sensitized to look for it.

        ...roboticus

        When your only tool is a hammer, all problems look like your thumb.

Re: paragraph mode and <DATA>
by Anonymous Monk on Mar 12, 2011 at 23:16 UTC
    works for me

      Dear anonymous monk

      Please specify perl version and platform/OS when posting "works for me". It is not very helpful to an OP to know that his code works "somewhere, out there". Very often problems like these are platform specific (as the next reply illustrated).

      Learning requires that people feel free to ask questions that may seem "dumb" to others. Although I'm sure your intentions were all the best, a curt "it works for me" without qualification of perl version and OS, may in fact come across as dismissive, as in "it works for me, so the problem must be you.".

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2020-07-05 14:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?