J
Jonathan Clark
Hello, all.
I'm re-learning perl (again), and have put myself into a while loop which
i've just broken out of ( while $problemSolved != 1 {thinkOfSolution
();trySolution();} ), I'd like to post my code, complete with example
data, for you to look over, and give some suggestions on how to improve
my style.
Most of the hard stuff was figuring out the regexps I needed for this
task. It's essentially a two-rexexp solution at the moment, but was
wondering if it could be done.
I've created this with the help of perlfaq, other perldocs, Beginning
Perl, and reading this newsgroup. I did some copy-pasting from the
perldoc IIRC, for the 'framework' of the program.
Oh, yes, the purpose of this program is to strip headers from newsgroup
messages so that I can feed the files to my megahal bot later
------- headerstrip.pl --------
#!/usr/bin/perl
use warnings;
use strict;
undef $/; #read that this allows the whole file to be read in, rather
than as lines or paragraphs.
my $file = "@ARGV"; #Get the filename passed from the command line
open my $in, '<', $file or die "Can't read old file: $!";
open my $out, '>', "$file.new" or die "Can't write new file: $!";
#Standard file openings, opens the original for reading, and the new file
#(with .new extension added) to be changed...
while( <$in> )
{
s/^Path(.*?)^Xref//gsm; #clears out most of the headers,
from the Path line to the end of the word Xref, where Xref starts on it's
own line
s/^: news.*\n//; #removes the rest of that Xref line
print $out $_; #writes file
}
close $out; #And, close the file handle
--------- EOF ------------
\
Now, I'd like to be able to pass multiple files to it, and I can tell
from my limited knowledge of perl that a) it isn't gonna work yet, and b)
I'll probably have to stick my $file [...] close $out; into a while loop,
most likely with @ARGV (or a 'copy' of it) as the iterator... Or would
that be a for loop? hmm...
I'll leave you Perl gurus to pointing out my mistakes, and await your
suggestions
--
Clarjon1
Bingo, gas station, hamburger with a side order of airplane noise,
and you'll be Gary, Indiana. - Jessie in the movie "Greaser's Palace"
I'm re-learning perl (again), and have put myself into a while loop which
i've just broken out of ( while $problemSolved != 1 {thinkOfSolution
();trySolution();} ), I'd like to post my code, complete with example
data, for you to look over, and give some suggestions on how to improve
my style.
Most of the hard stuff was figuring out the regexps I needed for this
task. It's essentially a two-rexexp solution at the moment, but was
wondering if it could be done.
I've created this with the help of perlfaq, other perldocs, Beginning
Perl, and reading this newsgroup. I did some copy-pasting from the
perldoc IIRC, for the 'framework' of the program.
Oh, yes, the purpose of this program is to strip headers from newsgroup
messages so that I can feed the files to my megahal bot later
------- headerstrip.pl --------
#!/usr/bin/perl
use warnings;
use strict;
undef $/; #read that this allows the whole file to be read in, rather
than as lines or paragraphs.
my $file = "@ARGV"; #Get the filename passed from the command line
open my $in, '<', $file or die "Can't read old file: $!";
open my $out, '>', "$file.new" or die "Can't write new file: $!";
#Standard file openings, opens the original for reading, and the new file
#(with .new extension added) to be changed...
while( <$in> )
{
s/^Path(.*?)^Xref//gsm; #clears out most of the headers,
from the Path line to the end of the word Xref, where Xref starts on it's
own line
s/^: news.*\n//; #removes the rest of that Xref line
print $out $_; #writes file
}
close $out; #And, close the file handle
--------- EOF ------------
\
Now, I'd like to be able to pass multiple files to it, and I can tell
from my limited knowledge of perl that a) it isn't gonna work yet, and b)
I'll probably have to stick my $file [...] close $out; into a while loop,
most likely with @ARGV (or a 'copy' of it) as the iterator... Or would
that be a for loop? hmm...
I'll leave you Perl gurus to pointing out my mistakes, and await your
suggestions
--
Clarjon1
Bingo, gas station, hamburger with a side order of airplane noise,
and you'll be Gary, Indiana. - Jessie in the movie "Greaser's Palace"