Join us at IRC!
Imagination is more valuable than knowledge - Albert Einstein
Friday, May 25, 2012
Navigation
Members Online
Total Online: 33
Web Spiders: 13
Guests Online: 31
Members Online: 2

Registered Members: 70208
Newest Member: andresuran
Latest Articles
View Thread

HellBound Hackers | Computer General | Programming

Author

Perl handling of CR/LF and CRLF together

vienessewaltzer
Member

Posts: 6
Location:
Joined: 15.02.11
Rank:
Monster
Posted on 18-03-11 17:29
I was writing a scblockedript today in order to print out some data from a file. Maybe I'm retarded in picking perl to perform this job, but it was one of those times where I just picked a scblockedripting language out of a hat and went with it. I hadn't scblockedripted much in perl either so I figured I would give it a shot.

Anyway, the whole point of the scblockedript is to parse through a file line by line and perform a function based on that line. For debugging purposes, I simply printed the line with a carriage return(\n). When I started to run the scblockedript, it spat out a humungous, long line of text and then quit. Another additional piece of information is that the log file was created by another perl scblockedript I ran. This was run in windows, which by default uses the CR or carriage return. Given that I used the > operator at the command line instead using the OPEN function in perl to write to a log file on windows is the obvious reason this deviated from my expected result (unless I'm way off). The main reason I did that was pure laziness at the time. So, when I tried to parse through the file with perl on Linux, it spat out a huge line because all of the line breaks were CR and it was looking for LF to indicate the end of line.

I used sed to fix the line breaks (sed -e s/'\r'/'\n'/g disabled.txt).

The whole purpose of this scblockedript is to run a CLI tool with the line to delete users from our directory tree. The reasons behind this are incredibly convoluted so I won't go into them here, but the code I wrote can be seen below. I didn't know if it would hold any vested interest with the rest of the community considering that's probably documented somewhere, but I'm sure someone will run into it again. Just an interesting tidbit on how perl handle's those characters.


#!/usr/bin/perl
####### Written By vienessewaltzer ############
use strict;
use warnings;
use IO::File;
use Text::CSV;

#Set the log file to use
my $file = "disabled.txt";
my $test = "test.txt";


open (F, "<$file") or die ("Could not open $file!");
open (T, ">$test") or die ("could not open $test!");

while (my $line = <F>)
{
print $line, "\n";
print T $line;
}
close(F);
close(T);


Also worth noting, when you write to a file with perl functions it adds CRLF together.

Edited by vienessewaltzer on 18-03-11 17:40
Author

RE: Perl handling of CR/LF and CRLF together

starofale
Member



Posts: 213
Location: England
Joined: 05.12.07
Rank:
God
Posted on 18-03-11 18:22
Thanks for this. I've never thought about cross-platform compatibility when programming before - I normally just use LFs everywhere. Could be useful in the future.


Just thought I should point out some things:

vienessewaltzer wrote:
I simply printed the line with a carriage return(\n).
CR = \r
LF = \n

vienessewaltzer wrote:
This was run in windows, which by default uses the CR or carriage return.
Windows uses CRLF (\r\n)




Try a new search engine
Author

RE: Perl handling of CR/LF and CRLF together

vienessewaltzer
Member

Posts: 6
Location:
Joined: 15.02.11
Rank:
Monster
Posted on 18-03-11 20:23
starofale wrote:

vienessewaltzer wrote:
This was run in windows, which by default uses the CR or carriage return.
Windows uses CRLF (\r\n)



Thanks for the correction. Actually, I went back and looked at my other scblockedript, and when I printed anything out in it, I used \r so that explains it.
Guest
Username

Password

Remember Me


Bookmark This Page
Affiliates
Adverts

 

 

Links
By using, viewing or obtaining any information contained on this site, you agree to the disclaimer.

© HellBound Hackers 2008- 2009. Since 3rd December 2004.