tags:

views:

78

answers:

2

Hi.. I need to understand the working of this particular program, It seems to be quite complicated, could you please see if you could help me understanding what this program in Perl does, I am a beginner so I hardly can understand whats happening in the code given on the following link below, Any kind of guidance or insights wrt this program is highly appreciated. Thank you...:) This program is called premove.pl.c

Its associated with one more program premove.pl, Its code looks like this:

#!perl

open (newdata,">newdata.txt") 
|| die("cant create new file\n");#create passwd file
$linedata = "";
while($line=<>){
chomp($line);
#chop($line);
print newdata $line."\n";
}
close(newdata);
close(olddata);

__END__

I am even not sure how to run the two programs mentioned here. I wonder also what does the extension of the first program signify as it has "pl.c" extension, please let me know if you know what it could mean. I need to understand it asap thats why I am posting this question, I am kind of short of time else I would try to figure it out myself, This seems to be a complex program for a beginner like me, hope you understand. Thank you again for your time.

+4  A: 

First, regarding the perl code you posted:

See:

This is some atrociously bad code. I rewrote it for you without all the obfuscation and foolishness. This should be a bit more understandable.

#!perl
use strict;
use warnings;
use IO::File;

my $newdata = IO::File->new( 'newdata.txt', '>' )
    or die "Unable to open newdata - $!\n";

while( my $line = <> ) {

    $newdata->print( $line );

}

The main thing that is likely to be confusing is the <> or null filehandle read. Here it grabs a line from any files listed in program args, or if no arguments provided, it reads STDIN.

This script is essentially cat filename > newdata.txt

As for premove.pl.c, I'm no internals expert, but it looks like the author took an example for how to embed a Perl interpreter in a C program and pasted it into an oddly named file.
It looks to me like its will compile down to something equivalent to perl. In short, another useless artifact. Was the person who produced this paid by the line?

If this is the state of the code you've inherited, I feel sorry for you.

daotoad
Hi..:) thank you for your answer. The code premove.pl, doesn't it just remove the new line characters ( like '\r' ) from any text and appends the newly generated text i.e. newdata with '\n'? In Your version of the modified program I can't see any 'chomp' keyword used, Have you removed it..?. It looks like its replacing the new line character in the original file with a '\n' ( say if earlier its a '\r' ) and appending it to newdata.txt. Is my interpretation of the program I have posted right?
mgj
@mgj, I removed `chomp` because it is redundant. `chomp` removes one occurrence of the global `$/` (aka `$INPUT_RECORD_SEPARATOR` if you `use English` ), which defaults to `\n`, at the end of a string. Since the original code chomps the line and then prints it with a `\n` concatenated at the end, its not doing anything. Note that I print `$line` unchanged--I do not remove `\n` so I do not have to add it back. `chomp` has no effect on the `\r` character unless `$/` is set to `\r`.
daotoad
K..:) Thank you for your answer daotoad, it looks like the code is not doing anything, but if I give the ip file which has \r at the end of the string, then it that case it chmops the \r and replaces is with \n right, then I think chomp no longer would be redundant... correct...?
mgj
+1  A: 

It looks like someone tried to run a Perl-to-C converter on your program so they could compile it to a C object. They probably thought this would make it faster. I'm guessing that you could ignore the .c file and use that Perl script directly.

brian d foy
Thank you for your answer Sir and also for your time..:)
mgj