On Aug 25, 2007, at 4:53 PM, Yoyoyo Yoyoyoyo wrote:

Thanks, but quick question. If I do it from the command line it works fine. But if I add:

`perl -pi -we 's/\n/\r\n/' ./student.csv`;

to my perl script it doesn't make the change to the file. Is there a reason for this?

Sure, the literal between backquotes has the same semantics as strings in double quotes, there's interpolation and \t, \n, and friends are translated to their actual meaning. So, the shell sees a hard newline after s/, as if you pressed the return key in the command-line.

To fix that double the backslashes, the shell needs to see a "\n" verbatim, so you need to write "\\n" in that command. (That's regular English quotes, not Perl double quotes.)

If you are already in a Perl script you could do that much more easily without shelling out, for example using Tie::File:

  use Tie::File;

  tie my @csv, 'Tie::File', 'student.csv' or die $!;
  $_ .= "\r" for @csv;
  untie @csv;

Shelling out is OK though, Perl's about glueing, even to itself :-), you can choose the approach you prefer.

-- fxn


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to