At 15:15 +0000 22/12/05, James Harvard wrote:
> 
>   I'm trying to detect a file's line endings (\r\n for DOS, \r for Mac and \n
> for Unix as I'm sure y'all know).
> 
>   Is there any easy way to do this?
> 
> use Fcntl;
> 
> sub get_line_ending_for_file {
> my( $file ) = @_;
> 
> my $fh;
> sysopen( $fh, $file, O_RDONLY );
> sysread( $fh, $_, 33000 );
> close( $fh );
> 
> return /(\015\012|\015|\012)/ ? $1 : "\n";
> }
> 

Does this work on all platforms? When I try it it works fine on OSX/Linux
with MAC/DOS/UNIX line endings, but fails (reads the whole file) when
reading DOS line endings on WinXP... Here is my script

use Fcntl;

my $file = $ARGV[0];

open(INFILE, $file) || die "cannot open $file: $!\n";

{
 local $/ = get_line_ending_for_file($file);
   
 while(<INFILE>)
   {
    my $line = $_;
    chomp $line;
    
    print "\n\n".length($line)."\n\n";
    last;
   }
}

sub get_line_ending_for_file {
   my($file) = @_;

   my $fh;
   sysopen( $fh, $file, O_RDONLY );
   sysread( $fh, $_, 200 );
   close( $fh );
   
   return /(\015\012|\015|\012)/ ? $1 : "\n";
}


Thanks for any help

Adam


-- 
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.

Reply via email to