开发者

Perl program - Dynamic Bootstrapping code

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 signi开发者_开发知识库fy 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.


First, regarding the perl code you posted:

See:

  • perlfunc open
  • perlfunc chmop
  • perlop on IO Operators, especially the null filehandle.

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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜