in reply to Re^2: Removing Newline Characters from File Data (chomp not working)
in thread Removing Newline Characters from File Data (chomp not working)

The purpose of the ? on the newline is to make it not essential that the line ends with a new line. This handles the edge case where the file is not ended with a newline character - otherwise, that wouldn't match the expression and it would be silently dropped. For some reference on regex construction, see perlre and perlretut.

The purpose of the parentheses is to put the regular expression in list context, and then the [0] index takes the first and only element, which is the expression in the parentheses. Without that, the regular expression would be in scalar context because of the split function, and would return only the true/false. Perl's ability to modify functional behavior's based upon context is one of its great strengths, though it can also cause significant confusion to new acolytes.

  • Comment on Re^3: Removing Newline Characters from File Data (chomp not working)

Replies are listed 'Best First'.
Re^4: Removing Newline Characters from File Data (chomp not working)
by mlux (Initiate) on Aug 27, 2009 at 17:20 UTC
    New acolytes like me, hah. Thanks, your explanations have really helped.