Brandon McCaig wrote:
On Fri, Oct 29, 2010 at 12:57 AM, John W. Krahn<jwkr...@shaw.ca> wrote:
If this is run on DOS/Windows then Perl will automatically translate "^J^M"
to newline.
On Windows, shouldn't "\cM\cJ" be equal to "\n"? :-/
I'm sorry for using "^J^M" above but in your unix2dos.pl program you
used $line =~ s/\cJ/\cM\cJ/g; and I misread that as "\cJ\cM". Also, you
don't need a /g option on that substitution as there can only be one
newline per line (unless you change the value of $/ [the Input Record
Separator] which you didn't do). (I personally don't use any Microsoft
products.)
I'm making some
assumptions here. Of course, I'm also ignoring encoding issues as I
haven't really tackled those just yet. I just tried testing it out a
little bit and in Linux it worked so that was good enough for a taste.
I can't say for sure which Perl is installed on my Windows 7 machine
(I can't remember), but I tried testing it out there as well and the
results surprised me...
use strict;
use warnings;
print "crlf" if "\cM\cJ" eq "\n";
print "lf" if "\cJ" eq "\n";
print "cr" if "\cM" eq "\n";
__END__
That prints "lf" for me in Windows. :\
That is because "\cJ" does equal "\n" *inside* *the* *Perl* *program*.
The translation is made when the file is read from the disk where "\n"
is equal to "\cM\cJ". If you read the file in "binary" mode instead of
"text" mode then the translation does not take place hence the use of
the line "use open ':bytes';" for the ARGV and ARGVOUT filehandles.
I use Cygwin, but I'm running
this from the Windows Command Prompt and the --version and -V output
suggests it's not Cygwin. Then again, I don't see C:\MSYS or C:\MinGW
so I must have installed one of the native Windows ports and forgot
about it... It appears to be running from C:\Perl64. Either way, I was
surprised to see "lf" output.
Did you know that Perl has built-in idioms to handle multiple file
manipulation:
I am/was aware that there was a shorthand for processing files passed
on the command line, but I assumed I would be unable to determine the
file name of the given handle,
Inside the "while (<>) {}" loop the current file name is stored in the
$ARGV variable and before the while loop all the file names are stored
in the @ARGV array.
and without knowing about how to safely
write in-place I figured that I would need to write to a temporary
file and move for reliable results. Evidently Perl can handle that bit
for you. Thanks for showing me. :) Something I'll have to read up on
when I have a moment. I've been learning Perl in pieces here on the
beginners list and from what I've read of the FAQ and perldocs. :)
Practice makes perfect and I only get so much of it. :P
John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction. -- Albert Einstein
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/