Missing LF or getting an Extra LF ? 
Author Message
 Missing LF or getting an Extra LF ?

Hi all, sorry to pester you; but I think I
finally fixed that QP mime problem I was
having when posting a question; please bear
with me.

The following code duplicates the error
where join, et al, is printing either
a LF where one isn't wanted or forgetting
to print a LF when needed:

#!/usr/local/bin/perl -wT

use strict;
use diagnostics;

sub codec {

    $/ = '';

die "\n     In $0:\n (F) Too few args in call to 'codec' subr.
     I need 3 in the format of: 'codec sFile, tFile, a[D]R|C'
     (where s/tFile is the source/target files and aDRC is either
      'R'eplace or 'C'opy (required) during encode (default) or 'D'ecode).\n
     You gave ", $#_ + 1, ".\n\n" unless ($#_ == 2);

    my $source  = shift;
    my $target  = shift;
    my $action  = shift; # Only D used; R&C ign'ed this Relse.


    my $MAXCOLS = '';
    my $row     = '';
    my $col     = '';
    my $char    = '';

    open SOURCE, "<$source" or die "Can't: $!";
    open TARGET, ">$target" or die "Can't: $!";

    while ( <SOURCE> ) {
        chomp;
        $_ = pack("u*",$_) unless ($action =~ /D/i);

        $MAXCOLS = -1;


            $MAXCOLS = length if $MAXCOLS < length;
        }



            for ( $col = 0; $col < $MAXCOLS; $col++ ) {
                $char = ( length($lines[$row]) > $col  )
                      ? substr($lines[$row], $col, 1)
                      : ' ';
                substr($vlines[$col], $row, 1) = $char;
            }
        }


            s/(.)/$1/g;
            print TARGET (($action =~ /D/i) ?
                join("\n", unpack("u*",$_)) : $_ . "\n");
        }

        print TARGET "\n";
    }

    close(SOURCE) or die "Can't: $!";
    close(TARGET) or die "Can't: $!";

Quote:
}

&codec("perl_CoDec", "Encoded", "");
&codec("Encoded", "Decoded", "D");

__END__

This CoDec has a small bug:
This line -

print TARGET (($action =~ /D/i) ?
    join("\n", unpack("u*",$_)) . "\n" : $_ . "\n");

Causes the unpack to stop at about half way thru the
line with an extra LF...

But if I change the above to this line -

print TARGET (($action =~ /D/i) ?
    join("\n", unpack("u*",$_)) : $_ . "\n");

It now simply leaves off a needed LF...

Any Perl Mongers (or others) know why?  If so, please enlighten me!

Thx,
-Sneex-  :]



Mon, 07 Jan 2002 03:00:00 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. Converting LF to CR+LF

2. DOS Perl convering LF to CR/LF

3. $/ = CR LF?

4. <FILE> and CR-LF

5. Binary data and CR-LF's in MSDOS

6. CR before login or any help that would lf before login

7. CRLF->LF conversion happens even on Solaris

8. How to print LF (MSDOS perl)

9. CR/LF

10. how to remove cr/lf ???

11. Question about formatting/(CR/LF) in PERL

12. Perl and CR+LF

 

 
Powered by phpBB® Forum Software