I'd make one more change.

Add the 'o' option to the substitutions.

my %subs = (
  crlf     => sub { s/[\015\012]+/$myPlatform/o },
  dos2unix => sub { s/$dos/$unix/o },
  unix2dos => sub { s/$unix/$dos/o },
  mac2dos  => sub { s/$mac/$dos/o  },
  mac2unix => sub { s/$mac/$unix/o },
  dos2mac  => sub { s/$dos/$mac/o  },
  unix2mac => sub { s/$unix/$mac/o },
);

So that we don't recompile the regexp for each line in the program.

G. Wade

-----Original Message-----
From: Joshua Polterock [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, March 27, 2002 1:50 PM
To: William R Ward
Cc: Jarkko Hietaniemi; [EMAIL PROTECTED]
Subject: Re: unix2dos and dos2unix


On Wed, 27 Mar 2002, William R Ward wrote:

> Joshua Polterock writes:
> >Apologies, I consider myself an intermediate Perl programmer at best, but
> >would something like this fit into this category. I realize it has a U*ix
> >bent.
> >
> >A Perl script called crlf with the following.
> [...]
>
> A sound implementation, but I think that all those evals will slow it
> down too much.  How about something like this instead:
>
> #!/usr/bin/perl -w
>
> use strict;
> use File::Basename;
>
> my $dos        = "\015\012";
> my $mac        = "\015";
> my $unix       = "\n";
> my $myPlatform = "$unix";
> my $iam        = basename($0);
> my $regexp     = "";
>
> $myPlatform = $mac if $^O =~ /mac/i;
> $myPlatform = $dos if $^O =~ /win|dos/i;
>
> my %subs = (
>   crlf     => sub { s/[\015\012]+/$myPlatform/ },
>   dos2unix => sub { s/$dos/$unix/ },
>   unix2dos => sub { s/$unix/$dos/ },
>   mac2dos  => sub { s/$mac/$dos/  },
>   mac2unix => sub { s/$mac/$unix/ },
>   dos2mac  => sub { s/$dos/$mac/  },
>   unix2mac => sub { s/$unix/$mac/ },
> );
>
> die "I do not recognize my own name."
>   unless exists ($subs{$iam});
>
> my $sub = $subs{$iam};
> while(<STDIN>) {
>   &$sub;
>   print;
> }
>
>
> --
> William R Ward            [EMAIL PROTECTED]
http://www.wards.net/~bill/
>
----------------------------------------------------------------------------
-
>      If you're not part of the solution, you're part of the precipitate.
>

Much better. This scales nicely.

Josh

Reply via email to