Follow us on Twitter!
The measure of a mans life is not how well he dies, but how well he lives.
Sunday, April 20, 2014
Navigation
Home
HellBoundHackers Main:
HellBoundHackers Find:
HellBoundHackers Information:
Learn
Communicate
Submit
Shop
Challenges
HellBoundHackers Exploit:
HellBoundHackers Programming:
HellBoundHackers Think:
HellBoundHackers Track:
HellBoundHackers Patch:
HellBoundHackers Other:
HellBoundHackers Need Help?
Other
Members Online
Total Online: 22
Guests Online: 22
Members Online: 0

Registered Members: 82843
Newest Member: hx47
Latest Articles
View Thread

HellBound Hackers | Computer General | Programming

Author

Perl handling of CR/LF and CRLF together


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 18-03-11 17:29
I was writing a script 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 scripting language out of a hat and went with it. I hadn't scripted much in perl either so I figured I would give it a shot.

Anyway, the whole point of the script 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 script, 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 script 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 script 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.

Code
   
    #!/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 on 18-03-11 17:40
Author

RE: Perl handling of CR/LF and CRLF together

starofale
Member



Posts: 218
Location: England
Joined: 05.12.07
Rank:
Moderate
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


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
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 script, and when I printed anything out in it, I used \r so that explains it.