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

I am going through the "Common Tasks" section of "Programming Perl" for Perl 4 and much of it is still relevant. Here was the program for de-hyphenating a file:
perl -pe 's/-\s*\n$//' FILES
But I have several questions about this solution:
  1. Why is $ necessary to anchor the carriage return? You can't have a carriage return anywhere but the end of a line right?
  2. Shouldn't this example use $/ instead of \n? We need not be so architecture-dependant even though Unix r0olz.

Carter's compass: I know I'm on the right track when by deleting something, I'm adding functionality

Replies are listed 'Best First'.
•Re: PP4: "de-hyphenating a file"
by merlyn (Sage) on Jul 17, 2003 at 17:12 UTC
    Having written that text, I can tell you with some authority...

    It's not necessary to anchor it. I put that in there to make it clear that it was happening at the end of the line.

    And, \n is always correct there, regardless of the underlying architecture, as the mapping of the local "line ending" character when reading text files. Binary files may show up local-line-ending conventions.

    Also realize that that text was written about two or three years before the first MS-DOS port.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

      And, \n is always correct there, regardless of the underlying architecture, as the mapping of the local "line ending" character when reading text files.

      So, does this mean that $/ is wrong there?

      Also, does this mean that $/ should be relegated only to print statements? Or, will \n also DWIM for other systems?

      ------
      We are the carpenters and bricklayers of the Information Age.

      Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

      Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

        I'm not sure where this idea originated but I find the latest perlport to be very guilty of making these things way too confusing...

        $\ is "\n" everywhere. You can change it if you want to, but that is how it starts out.

        Perhaps Re^4: Line Feeds (rumor control) will help clear some of this fog.

                        - tye
Re: PP4: "de-hyphenating a file"
by jdklueber (Beadle) on Jul 18, 2003 at 17:32 UTC
    Why is $ necessary to anchor the carriage return? You can't have a carriage return anywhere but the end of a line right?

    It's not germaine to this particular example, but if you set $/ to something other than \n (or undef'ed it entirely), you might also be wanting to ignore those linebreaks that weren't against your arbitrary record seperator. So, in this case? The $ is optional. But there might be cases where it isn't. :-)
    --
    Jason Klueber
    ookami@insightbb.com

    /(bb)|^b{2}/
    --Shakespeare