my own perl "dos->unix"/"unix->dos"

my own perl "dos->unix"/"unix->dos"

Post by Robert Wal » Fri, 23 Jan 2004 00:24:10


i made a dos to unix, unix to dos program catered for my own purposes.

it's seems to work fine. small program, 26 lines

anyone see any potential problems with it? do you have a better way to
do it?
my next step is to slip in some code to automatically "detect" whether
its a unix or dos file.

#!/usr/bin/perl -w
use strict;

#setting based on filename argument.
# if sym link is dos-unix or if sym link is unix-dos
my ($from, $to);
my $option=substr($0,rindex($0,'/')+1,length($0)-1); # $0 gives full
path. this code gives just filename
if ($option eq "dos-unix"){
$from="\015\012";
$to ="\012";
} elsif ($option eq "unix-dos"){
$from="[^\015]\012";
$from="[^\015]\012";
$to ="\015\012";
} else {
print "only sym links \"unix-dos\" and \"dos-unix\" are allowed\n";
}

foreach my $file (@ARGV){
my @output;
open READ, $file or die "Could not open file '$file' $!";
while (<READ>){
$_=~s/\Q$from/$to/;
push @output, $_;
}
close (READ);

open WRITE, ">$file" or die "Could not open output file '$file' $!";
print WRITE @output;
close (READ);
}
 
 
 

my own perl "dos->unix"/"unix->dos"

Post by A. Sinan U » Fri, 23 Jan 2004 01:11:37


...


If you want your script to work on computers without symlinks, you might
want to handle this using command line options. Also, the line below you
comment is not related to your comment. If you keep with this commenting
style, your comments and code can get really out of sync when the script
gets bigger.


Why the repeated assignment to $from?


I would actually open a temp file (there is a module to do that safely
and cleanly), write my output to that file, and only delete the original
and rename the temp after everything is successfully completed. That
would keep the script's memory usage constant regardless of file size.



Ahem, if an error happens while you are writing to or closing the file,
you will have already clobbered the original. I don't see your script
asking the user if it is OK to nuke his file out of existence if the
conversion fails. Again, using a temporary file would handle this.

First, do no harm.

Sinan.
--
A. Sinan Unur
XXXX@XXXXX.COM (reverse each component for email address)

 
 
 

my own perl "dos->unix"/"unix->dos"

Post by Tad McClel » Fri, 23 Jan 2004 01:49:49


To make doubly sure that it is set to the correct value?


--
Tad McClellan SGML consulting
XXXX@XXXXX.COM Perl programming
Fort Worth, Texas
 
 
 

my own perl "dos->unix"/"unix->dos"

Post by Robert Wal » Fri, 23 Jan 2004 03:25:27


copy and paste from a terminal problem.

copy and paste, scroll down, copy and paste.
what's the big deal. sheesh...
 
 
 

my own perl "dos->unix"/"unix->dos"

Post by Paul Lall » Fri, 23 Jan 2004 03:35:30


Are you sure you don't want lookbehinds here? I haven't tried your code,
but it looks to me like you're going to end up replacing both \012 and
whatever character came before it with \015\012. I think you want to
search for \012 that was not preceded by \015, but don't actually match
(and therefore replace) whatever character did precede it. That involves
lookbehinds, I believe.

Paul Lalli
 
 
 

my own perl "dos->unix"/"unix->dos"

Post by Ben Morro » Fri, 23 Jan 2004 04:02:45


use warnings;

is better than -w.


Use File::Spec or File::Basename to do this sort of thing.


I try and keep code wrapped to a reasonable line length; part of doing
this means not putting long comments on the ends of lines.


Use qq{}.


Use lexical filehandles, and scope them rather than explicitly closing
where you can.

open my $READ, $file or die...;


ITYM close (WRITE)?


Ben

--
I've seen things you people wouldn't believe: attack ships on fire off the
shoulder of Orion; I've watched C-beams glitter in the darkness near the
Tannhauser Gate. All these moments will be lost, in time, like tears in rain.
Time to die. |-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-| XXXX@XXXXX.COM
 
 
 

my own perl "dos->unix"/"unix->dos"

Post by Gunnar Hja » Fri, 23 Jan 2004 08:28:49


Or capturing:

$from = "([^\015])\012";

...

$_ =~ s/$from/$1$to/;

(quoting meta seems not to be correct)

--
Gunnar Hjalmarsson
Email: http://www.yqcomputer.com/
 
 
 

my own perl "dos->unix"/"unix->dos"

Post by Michele Do » Sat, 24 Jan 2004 07:59:29

On Wed, 21 Jan 2004 10:24:10 -0500, Robert Wallace < XXXX@XXXXX.COM >



Have you ever heard of a (programmer's) virtue called lazyness?
Whenever I'm too lazy to remember which is which and I do not have any
particular requirement but converting to the native convention of the
OS I'm using, I resort to

perl -lpi -e '' <file(s)>

Well that is under Linux, under Win I have to do

perl -lpi.bak -e "" <file(s)>

and

perl -lpi.bak -e "BEGIN{@ARGV=map glob($_),@ARGV}" <file(s)>

if I need to use wildcards. (yes, I know there's an AS executable that
does shell wildcard expansion!)

My program is 1 line only! It is not equivalent to yours though...
;-)


HTH,
Michele
--
# This prints: Just another Perl hacker,
seek DATA,15,0 and print q... <DATA>;
__END__