is there something more elegant to convert Dos to unix in subroutine?

Discussion in 'Perl Misc' started by Andrew, May 2, 2004.

  1. Andrew

    Andrew Guest

    sub toUnixFile() {
    my ($file) = @_;
    my ($temp_file) = $file . ".tmp";
    move($file, $temp_file);
    open(in, "<$temp_file");
    open(out, ">$file");
    while(<in>) {
    chomp;
    ~ s/\r$//;
    print out "$_\n";
    }
    close in;
    close out;
    unlink $temp_file;
    return 0;
    }
     
    Andrew, May 2, 2004
    #1
    1. Advertising

  2. Andrew

    Uri Guttman Guest

    Re: is there something more elegant to convert Dos to unix insubroutine?

    >>>>> "A" == Andrew <> writes:

    A> sub toUnixFile() {
    A> my ($file) = @_;
    A> my ($temp_file) = $file . ".tmp";
    A> move($file, $temp_file);
    A> open(in, "<$temp_file");
    A> open(out, ">$file");
    A> while(<in>) {
    A> chomp;
    A> ~ s/\r$//;
    A> print out "$_\n";
    A> }
    A> close in;
    A> close out;
    A> unlink $temp_file;
    A> return 0;
    A> }

    perl -I.bak -pe 'tr/\r\n/\n/s'

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
     
    Uri Guttman, May 2, 2004
    #2
    1. Advertising

  3. Andrew wrote:
    >
    > [ Subject: is there something more elegant to convert Dos to unix
    > in subroutine? ]
    >
    > sub toUnixFile() {
    > my ($file) = @_;
    > my ($temp_file) = $file . ".tmp";
    > move($file, $temp_file);
    > open(in, "<$temp_file");
    > open(out, ">$file");
    > while(<in>) {
    > chomp;
    > ~ s/\r$//;
    > print out "$_\n";
    > }
    > close in;
    > close out;
    > unlink $temp_file;
    > return 0;
    > }


    Yes. Subroutines that do what they are supposed to do are always more
    elegant.

    Please post working code, or rephrase your question.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, May 2, 2004
    #3
  4. Andrew

    Ala Qumsieh Guest

    Uri Guttman wrote:

    > perl -I.bak -pe 'tr/\r\n/\n/s'


    I'm really confused. I thought your shift keys were permanently broken,
    but now I see a -I when you actually mean -i! What is happening to this
    world?

    FWIW, I like the following:

    perl -pi.bak -e 'y/\cM//d' file_name

    --Ala
     
    Ala Qumsieh, May 3, 2004
    #4
  5. On 2 May 2004 14:35:44 -0700, (Andrew) wrote:

    >sub toUnixFile() {

    [snip]

    FWIW I customarily use

    perl -lpi.bak -e '' <files>


    Michele
    --
    you'll see that it shouldn't be so. AND, the writting as usuall is
    fantastic incompetent. To illustrate, i quote:
    - Xah Lee trolling on clpmisc,
    "perl bug File::Basename and Perl's nature"
     
    Michele Dondi, May 3, 2004
    #5
  6. Michele Dondi <> wrote:
    > On 2 May 2004 14:35:44 -0700, (Andrew) wrote:
    >
    >>sub toUnixFile() {

    > [snip]
    >
    > FWIW I customarily use
    >
    > perl -lpi.bak -e '' <files>



    Or, if you are playing golf:

    perl -lpi.bak -e1 <files>


    Or, if you are confident and playing to win:

    perl -lpi -e1 <files>


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, May 3, 2004
    #6
  7. On Mon, 3 May 2004 07:53:16 -0500, Tad McClellan
    <> wrote:

    >> perl -lpi.bak -e '' <files>

    >
    >Or, if you are playing golf:
    >
    > perl -lpi.bak -e1 <files>


    Not at all, just lazy, and even if now I *do* remember which
    convention is which, I always prefer to have "someone" else do that
    for me. Someone who I am confindent that will remember correctly too,
    BTW!

    And under Win* it's:

    perl -lpi.bak -e "" <files>

    Incidentally, nice to notice that your golf-like solution wouldn't
    require different conventions on the cmd line too!

    >Or, if you are confident and playing to win:
    >
    > perl -lpi -e1 <files>


    D'Oh! Under Win* it seems that I cannot be confident! ;-)


    Michele
    --
    you'll see that it shouldn't be so. AND, the writting as usuall is
    fantastic incompetent. To illustrate, i quote:
    - Xah Lee trolling on clpmisc,
    "perl bug File::Basename and Perl's nature"
     
    Michele Dondi, May 3, 2004
    #7
  8. Andrew

    Matija Papec Guest

    Re: is there something more elegant to convert Dos to unix in subroutine?

    X-Ftn-To: Uri Guttman

    Uri Guttman <> wrote:
    > A> close out;
    > A> unlink $temp_file;
    > A> return 0;
    > A> }
    >
    >perl -I.bak -pe 'tr/\r\n/\n/s'


    tr/\r//d should be more straightforward?



    --
    Matija
     
    Matija Papec, May 3, 2004
    #8
  9. Andrew

    Uri Guttman Guest

    Re: is there something more elegant to convert Dos to unix insubroutine?

    >>>>> "MP" == Matija Papec <> writes:

    MP> Uri Guttman <> wrote:
    >>
    >> perl -I.bak -pe 'tr/\r\n/\n/s'


    MP> tr/\r//d should be more straightforward?

    it could be. better golf score for sure.

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
     
    Uri Guttman, May 3, 2004
    #9
  10. Andrew

    Andrew Guest

    Hm, my subroutine is actually working just fine, prove me wrong, it
    conversts DOS files to UNIX just fine.
    All examples given involve calling perl from code, I don't like it, I
    would like a subroutine or function which can be included in my perl
    code. I don't like calling perl from perl.
    Thanks,


    Gunnar Hjalmarsson <> wrote in message news:<c73qfu$hqsum$-berlin.de>...
    > Andrew wrote:
    > >
    > > [ Subject: is there something more elegant to convert Dos to unix
    > > in subroutine? ]
    > >
    > > sub toUnixFile() {
    > > my ($file) = @_;
    > > my ($temp_file) = $file . ".tmp";
    > > move($file, $temp_file);
    > > open(in, "<$temp_file");
    > > open(out, ">$file");
    > > while(<in>) {
    > > chomp;
    > > ~ s/\r$//;
    > > print out "$_\n";
    > > }
    > > close in;
    > > close out;
    > > unlink $temp_file;
    > > return 0;
    > > }

    >
    > Yes. Subroutines that do what they are supposed to do are always more
    > elegant.
    >
    > Please post working code, or rephrase your question.
     
    Andrew, May 3, 2004
    #10
  11. Andrew

    Paul Lalli Guest

    Re: is there something more elegant to convert Dos to unix insubroutine?

    [please post your reply below the quoted material - doing otherwise is
    considered rude]

    On Mon, 3 May 2004, Andrew wrote:

    > Gunnar Hjalmarsson <> wrote in message news:<c73qfu$hqsum$-berlin.de>...
    > >
    > > Andrew wrote:
    > > >
    > > > [ Subject: is there something more elegant to convert Dos to unix
    > > > in subroutine? ]
    > > >
    > > > sub toUnixFile() {
    > > > my ($file) = @_;
    > > > my ($temp_file) = $file . ".tmp";
    > > > move($file, $temp_file);
    > > > open(in, "<$temp_file");
    > > > open(out, ">$file");
    > > > while(<in>) {
    > > > chomp;
    > > > ~ s/\r$//;
    > > > print out "$_\n";
    > > > }
    > > > close in;
    > > > close out;
    > > > unlink $temp_file;
    > > > return 0;
    > > > }

    > >
    > > Yes. Subroutines that do what they are supposed to do are always more
    > > elegant.
    > >
    > > Please post working code, or rephrase your question.

    >
    > Hm, my subroutine is actually working just fine, prove me wrong, it
    > conversts DOS files to UNIX just fine.


    I *think* the bit Gunnar was complaining about is the line right below
    chomp. Presumably, your goal with this line is to perform a search and
    replace on $_. The bizzare part is the random ~ in front of it. Why is
    that there? While not a syntax error, it is a most unusual logic error.
    (And indeed, if you enable warnings, you'll see Perl yell at you about
    "useless use of one's complement" or similar). The fact that the code
    still works is more of a side affect of that line than anything else.

    (Gunnar, feel free to point out if there's another bit that I'm missing)

    > All examples given involve calling perl from code, I don't like it, I
    > would like a subroutine or function which can be included in my perl
    > code. I don't like calling perl from perl.
    > Thanks,


    In the future, you should probably specify important details like that at
    the beginning. The reason everyone sent you those tiny short programs was
    that you never specified this would be a small part of a larger program.

    What, out of curiousity, is the cause of your disliking calling a Perl
    one-liner from within a larger program?

    Paul Lalli
     
    Paul Lalli, May 3, 2004
    #11
  12. Andrew

    Ala Qumsieh Guest

    "Michele Dondi" <> wrote in message
    news:...
    > On 2 May 2004 14:35:44 -0700, (Andrew) wrote:
    >
    > >sub toUnixFile() {

    > [snip]
    >
    > FWIW I customarily use
    >
    > perl -lpi.bak -e '' <files>


    Correct me if I'm wrong, but this doesn't work on *nix systems since the
    auto chomp() will only remove \n characters, leaving \r's intact.

    --Ala
     
    Ala Qumsieh, May 3, 2004
    #12
  13. [ Please put quoted text *before* your own comments. ]

    Andrew wrote:
    > Gunnar Hjalmarsson wrote:
    >> Andrew wrote:
    >>>
    >>> [ Subject: is there something more elegant to convert Dos to
    >>> unix in subroutine? ]
    >>>
    >>> sub toUnixFile() {
    >>> my ($file) = @_;
    >>> my ($temp_file) = $file . ".tmp";
    >>> move($file, $temp_file);
    >>> open(in, "<$temp_file");
    >>> open(out, ">$file");
    >>> while(<in>) {
    >>> chomp;
    >>> ~ s/\r$//;
    >>> print out "$_\n";
    >>> }
    >>> close in;
    >>> close out;
    >>> unlink $temp_file;
    >>> return 0;
    >>> }

    >>
    >> Yes. Subroutines that do what they are supposed to do are always
    >> more elegant.
    >>
    >> Please post working code, or rephrase your question.

    >
    > Hm, my subroutine is actually working just fine, prove me wrong,


    Sure.

    #!/usr/bin/perl

    sub toUnixFile() {
    my ($file) = @_;
    }

    toUnixFile('/path/to/file');

    Resulting error message:
    "Too many arguments for main::toUnixFile"

    Since your subroutine requires an argument, the code won't even compile.

    Are you still claiming that your sub is working just fine? ;-)

    > All examples given involve calling perl from code, I don't like it,
    > I would like a subroutine or function which can be included in my
    > perl code.


    Right, and you said so in the subject line. Note that you might have
    got more accurate responses if you had repeated your question in the
    body of the message.

    If the files to be processed aren't too big, you can always slurp them
    into a scalar:

    sub toUnixFile {
    my $file = shift;
    local(*FH, $/);
    open FH, "+< $file" or die $!;
    $_ = <FH>;
    tr/\r//d;
    seek FH, 0, 0;
    truncate FH, 0;
    print FH;
    }

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, May 3, 2004
    #13
  14. Andrew

    Uri Guttman Guest

    Re: is there something more elegant to convert Dos to unix insubroutine?

    >>>>> "A" == Andrew <> writes:

    A> Hm, my subroutine is actually working just fine, prove me wrong, it
    A> conversts DOS files to UNIX just fine.

    it has several bugs.

    A> All examples given involve calling perl from code, I don't like it, I
    A> would like a subroutine or function which can be included in my perl
    A> code. I don't like calling perl from perl.

    huh? what calling perl from code are you talking about? the answers were
    all one liners and it is trivial to convert any of them to a sub.

    >> >
    >> > sub toUnixFile() {
    >> > my ($file) = @_;
    >> > my ($temp_file) = $file . ".tmp";


    and what if that file already existed?

    >> > move($file, $temp_file);
    >> > open(in, "<$temp_file");
    >> > open(out, ">$file");


    and what if either of those open calls fails?

    >> > while(<in>) {
    >> > chomp;
    >> > ~ s/\r$//;


    what is that naked ~ doing there?

    >> > print out "$_\n";
    >> > }
    >> > close in;
    >> > close out;
    >> > unlink $temp_file;
    >> > return 0;


    why the return 0? you don't return from anywhere else.

    so your sub it not 'actually working just fine'. proving it wrong was
    too easy. you just didn't get the answers.

    now fix your sub and use the ideas shown and post new code. you have to
    do some of the work too.

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
     
    Uri Guttman, May 3, 2004
    #14
  15. Re: is there something more elegant to convert Dos to unix in subroutine?

    Paul Lalli wrote:
    >> Gunnar Hjalmarsson wrote:
    >>> Yes. Subroutines that do what they are supposed to do are
    >>> always more elegant.

    >
    > I *think* the bit Gunnar was complaining about is the line right
    > below chomp.


    <snip>

    > (Gunnar, feel free to point out if there's another bit that I'm
    > missing)


    My main "complaint" is the prototype that disallows that arguments are
    passed to the sub.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, May 3, 2004
    #15
  16. Andrew

    Anno Siegel Guest

    Re: is there something more elegant to convert Dos to unix in subroutine?

    Gunnar Hjalmarsson <> wrote in comp.lang.perl.misc:
    > Paul Lalli wrote:
    > >> Gunnar Hjalmarsson wrote:
    > >>> Yes. Subroutines that do what they are supposed to do are
    > >>> always more elegant.

    > >
    > > I *think* the bit Gunnar was complaining about is the line right
    > > below chomp.

    >
    > <snip>
    >
    > > (Gunnar, feel free to point out if there's another bit that I'm
    > > missing)

    >
    > My main "complaint" is the prototype that disallows that arguments are
    > passed to the sub.


    I wonder if that bug was masked by a gratuitous ampersand in the call

    "&toUnixFile( $some_file)"

    would work (as far as that goes), even with the empty prototype.

    Anno
     
    Anno Siegel, May 4, 2004
    #16
  17. Re: is there something more elegant to convert Dos to unix in subroutine?

    Anno Siegel wrote:
    > Gunnar Hjalmarsson wrote:
    >> My main "complaint" is the prototype that disallows that
    >> arguments are passed to the sub.

    >
    > I wonder if that bug was masked by a gratuitous ampersand in the
    > call
    >
    > "&toUnixFile( $some_file)"
    >
    > would work (as far as that goes), even with the empty prototype.


    Hmm.. Didn't think of that possibility.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, May 4, 2004
    #17
  18. Andrew

    Andrew Guest

    Thanks for constructive remarks, here is the fixed one, which, again,
    works just fine :)

    #
    # Converts DOS to UNIX file
    # if trim is true will trim leading and trailing spaces and remove
    empty lines
    #
    sub toUnixFile {
    my ($file, $trim) = @_;
    -f $file || die "Can't open \"$file\": $! \n";
    my $temp_file = $file . ".tmp.$$.$^T";
    move($file, $temp_file);
    local *in;
    local *out;
    open(in, "<$temp_file");
    open(out, ">$file");
    while(<in>) {
    chomp;
    s/\r$//;
    if ($trim eq "true") {
    s/^\s*//;
    s/\s*$//;
    unless ( -z $_ ) {
    print out "$_\n";
    }
    } else {
    print out "$_\n";
    }
    }
    close in;
    close out;
    unlink $temp_file;
    }



    Uri Guttman <> wrote in message news:<>...
    > >>>>> "A" == Andrew <> writes:

    >
    > A> Hm, my subroutine is actually working just fine, prove me wrong, it
    > A> conversts DOS files to UNIX just fine.
    >
    > it has several bugs.
    >
    > A> All examples given involve calling perl from code, I don't like it, I
    > A> would like a subroutine or function which can be included in my perl
    > A> code. I don't like calling perl from perl.
    >
    > huh? what calling perl from code are you talking about? the answers were
    > all one liners and it is trivial to convert any of them to a sub.
    >
    > >> >
    > >> > sub toUnixFile() {
    > >> > my ($file) = @_;
    > >> > my ($temp_file) = $file . ".tmp";

    >
    > and what if that file already existed?
    >
    > >> > move($file, $temp_file);
    > >> > open(in, "<$temp_file");
    > >> > open(out, ">$file");

    >
    > and what if either of those open calls fails?
    >
    > >> > while(<in>) {
    > >> > chomp;
    > >> > ~ s/\r$//;

    >
    > what is that naked ~ doing there?
    >
    > >> > print out "$_\n";
    > >> > }
    > >> > close in;
    > >> > close out;
    > >> > unlink $temp_file;
    > >> > return 0;

    >
    > why the return 0? you don't return from anywhere else.
    >
    > so your sub it not 'actually working just fine'. proving it wrong was
    > too easy. you just didn't get the answers.
    >
    > now fix your sub and use the ideas shown and post new code. you have to
    > do some of the work too.
    >
    > uri
     
    Andrew, May 8, 2004
    #18
  19. Andrew

    Uri Guttman Guest

    Re: is there something more elegant to convert Dos to unix insubroutine?

    >>>>> "A" == Andrew <> writes:

    A> Thanks for constructive remarks, here is the fixed one, which, again,
    A> works just fine :)

    and looks ugly too!

    A> sub toUnixFile {
    A> my ($file, $trim) = @_;
    A> -f $file || die "Can't open \"$file\": $! \n";

    lose the \ there. use an alternative delimiter

    A> my $temp_file = $file . ".tmp.$$.$^T";

    use the File::Temp module

    A> move($file, $temp_file);
    A> local *in;
    A> local *out;

    gack! use lexical filehandles. and glob handles are upper case by tradition
    A> open(in, "<$temp_file");
    A> open(out, ">$file");

    you check for the file with -f but not here? always check your open
    calls for failures

    A> while(<in>) {
    A> chomp;
    A> s/\r$//;

    why the chomp AND s///? s/[\r\n]+$// would work anywhere.

    A> if ($trim eq "true") {
    A> s/^\s*//;
    A> s/\s*$//;
    A> unless ( -z $_ ) {

    huh??!! what do you think -z does? this is perl, not shell.

    and if i get your logic (which is coded incorrectly), you want to not
    print lines that had only blanks. that means this is not a true dos2unix
    program since that is not part of the typical spec for that.

    A> print out "$_\n";
    A> }
    A> } else {
    A> print out "$_\n";
    A> }

    redundant code always bothers me. if you used next you could have that
    one print line for both cases.


    and you still top post.

    >> >>>>> "A" == Andrew <> writes:

    >>

    A> Hm, my subroutine is actually working just fine, prove me wrong, it
    A> conversts DOS files to UNIX just fine.
    >>
    >> it has several bugs.


    it still has several bugs

    A> All examples given involve calling perl from code, I don't like it, I
    A> would like a subroutine or function which can be included in my perl
    A> code. I don't like calling perl from perl.

    >> huh? what calling perl from code are you talking about? the answers were
    >> all one liners and it is trivial to convert any of them to a sub.


    you never answered that question.

    your skill levels demonstrate that you don't know when to call perl from
    perl. so your saying you don't like it doesn't hold any water.

    >> >> >
    >> >> > sub toUnixFile() {
    >> >> > my ($file) = @_;
    >> >> > my ($temp_file) = $file . ".tmp";

    >>
    >> and what if that file already existed?


    nevr fixed that bug.

    >> >> > move($file, $temp_file);
    >> >> > open(in, "<$temp_file");
    >> >> > open(out, ">$file");

    >>
    >> and what if either of those open calls fails?


    never fixed that bug

    >> so your sub it not 'actually working just fine'. proving it wrong was
    >> too easy. you just didn't get the answers.


    and most of the bugs i pointed out are still there. you don't listen
    well it seems.

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
     
    Uri Guttman, May 8, 2004
    #19
  20. On Mon, 03 May 2004 20:27:34 GMT, "Ala Qumsieh"
    <> wrote:

    >> FWIW I customarily use
    >>
    >> perl -lpi.bak -e '' <files>

    >
    >Correct me if I'm wrong, but this doesn't work on *nix systems since the
    >auto chomp() will only remove \n characters, leaving \r's intact.


    [ashamed] Tes, you're right. I was implicitly doing a wrong
    assumption. Though, what I do *really* customarily use (and *really*
    works!) is

    perl -lpi.bak -e "" <files>

    in Win* environments on (possibly) *nix-line-terminated text files...


    Michele
    --
    you'll see that it shouldn't be so. AND, the writting as usuall is
    fantastic incompetent. To illustrate, i quote:
    - Xah Lee trolling on clpmisc,
    "perl bug File::Basename and Perl's nature"
     
    Michele Dondi, May 8, 2004
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kamilche
    Replies:
    7
    Views:
    292
    Peter Hansen
    Jun 29, 2004
  2. john san
    Replies:
    19
    Views:
    743
    Diez B. Roggisch
    Feb 18, 2005
  3. Replies:
    5
    Views:
    391
  4. Lilith
    Replies:
    8
    Views:
    351
    Dave Rahardja
    Sep 2, 2005
  5. Robert Wallace

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

    Robert Wallace, Jan 21, 2004, in forum: Perl Misc
    Replies:
    7
    Views:
    281
    Michele Dondi
    Jan 22, 2004
Loading...

Share This Page