Re: unix2dos and dos2unix
[email protected] (William R Ward) 02 Apr 2002 12:59:32 -0800
| Newsgroups | perl.scripts |
|---|---|
| Message-ID | <[email protected]> |
[email protected] (Neil Bowers) writes: > So now how about wrapping this up in a module, and having the script > use that? That way Ronald's CGI could use it. Also I notice that > Mark Veltzer's Meta distribution has a subset of this functionality, > in Meta::Utils::Dos. Wouldn't be surprised if other people have modules > doing some of this stuff too. > > Text::CRLF? Interface? > > $crlf = Text::CRLF->new(); > > # default behaviour - convert to my platform, from STDIN to STDOUT > $crlf->convert(); > > # a dos2unix filter > $crlf->dos2unix($INFILE, $OUTFILE); > > # i don't care where it came from, just where it's going > $crlf->to_unix($INFILE, $OUTFILE); > > Hmm, you can't have a sub called 2unix(), so maybe the other methods > should be called dos_to_unix ...? > > I'll write the testsuite. OK, here's my idea for such a module... Its AUTOLOAD function supports methods named X2Y, X_to_Y, XtoY, X_toY, or Xto_Y. Or simply toY, to_Y, or _to_Y. Or convert() as in your example. You can use uppercase letters if you like; it's case-insensitive. X and Y are one of "crlf", "cr", "lf", or any of several aliases for these: * Instead of "crlf" you can say "dos," "windows," "win32," or "mswin." * For "lf" you can say "unix," "linux," "darwin," or "osx." * For "cr" you can say "mac" or "macos." Again, these are case-insensitive, so "DOS2UNIX" or "MacOS9ToDarwin". I got a little carried away with the Mac regexp's, I'm afraid. For OSX (Darwin), I match mac(intosh)?os(x|1[0-9]) For all other Mac's, it uses mac(intosh)?(os[1-9])? It seems to work, though... Also, if you don't feel like using OO syntax, you can also import methods. If you say "use Text::CRLF 'dos2unix'" then that function will be available in your current package. It uses the object $DEFAULT in the Text::CRLF package and calls the appropriate method. I need to write more comments, but here's the code anyway... package Text::CRLF; use strict; use vars qw($AUTOLOAD $DEFAULT); my $crlf = "\x0D\x0A"; my $cr = "\x0D"; my $lf = "\x0A"; my $myPlatform = $lf; $myPlatform = $cr if $^O =~ /mac/i; $myPlatform = $crlf if $^O =~ /mswin|dos/i; my %subs = ( convert => sub { s/($crlf|$cr|$lf)/$myPlatform/og }, crlf2lf => sub { s/$crlf/$lf/og }, lf2crlf => sub { s/$lf/$crlf/og }, cr2crlf => sub { s/$cr/$crlf/og }, cr2lf => sub { s/$cr/$lf/og }, crlf2cr => sub { s/$crlf/$cr/og }, lf2cr => sub { s/$lf/$cr/og }, "2crlf" => sub { s/($crlf|$cr|$lf)/$crlf/og }, "2cr" => sub { s/($crlf|$cr|$lf)/$cr/og }, "2lf" => sub { s/($crlf|$cr|$lf)/$lf/og }, ); sub DESTROY { } sub AUTOLOAD { my($self, @args) = @_; my($pack, $func) = ($AUTOLOAD =~ /^(.+)::(.+)$/); $func = lc $func; # Case insensitive $func =~ s/_?to_?/2/; # Allow "crlf_to_lf" if user prefers $func =~ s/unix|linux|darwin|mac(intosh)?os(x|1[0-9])/lf/g; $func =~ s/mac(intosh)?(os[1-9])?/cr/g; $func =~ s/dos|windows|win32|mswin/crlf/g; die qq/Can't locate object method "$func" via package "$pack"/ unless exists $subs{$func}; my $sub = $subs{$func}; foreach (@args) { $_ = join('', <$_>) if (ref $_ && (ref $_ eq 'GLOB') || $_->isa("IO::Handle")); &$sub; } wantarray ? @args : (@args ? \@args : $args[0]); } $SIG{ALRM} = sub { die "alarm" }; alarm(10); sub import { my($pkg, @funcs) = @_; my $callpkg = caller(0); $DEFAULT = new $pkg; foreach my $f (@funcs) { eval "sub exp_$f { \$DEFAULT->$f(\@_); }"; die "eval failed: $@" if $@; eval '*'.$callpkg.'::'.$f.' = \&exp_'.$f; die "eval failed: $@" if $@; } } sub new { my $class = shift; bless { @_ } => $class; } 1; -- William R Ward [email protected] http://www.wards.net/~bill/ ----------------------------------------------------------------------------- If you're not part of the solution, you're part of the precipitate.