|
Posted by Jonathan Clark on July 11, 2008, 7:43 pm
Please log in for more thread options
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"
|
|
Posted by Michael Carman on July 11, 2008, 9:42 pm
Please log in for more thread options
Jonathan Clark wrote:
> #!/usr/bin/perl
> use warnings;
> use strict;
So far, so good. :D
> undef $/; #read that this allows the whole file to be read in, rather
> than as lines or paragraphs.
Slurping is okay for small files but doesn't scale. It should be fine
for newsgroup postings.
> my $file = "@ARGV"; #Get the filename passed from the command line
If the user specifies multiple arguments this will concatenate them. You
only want one argument.
my $file = shift @ARGV;
You might want to throw an error if the user doesn't specify a file at
all. For example:
die "Usage: $0 <file>\n";
> open my $in, '<', $file or die "Can't read old file: $!";
> open my $out, '>', "$file.new" or die "Can't write new file: $!";
Good x 3. You're using three-arg open(), checking its return value, and
including the error in any message.
> while( <$in> )
Since you're slurping the file you don't need a loop at all.
$_ = <$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
I suspect the /g isn't necessary there as it appears you're trying to
remove all the headers in a single pass.
Since you aren't using $1 and don't need any grouping you can get rid of
the parentheses.
s/^Path.*?^Xref//sm;
> s/^: news.*\n//; #removes the rest of that Xref line
> print $out $_; #writes file
I think you can merge the two regular expressions into one.
[Note: untested]
s/^Path.*?^Xref:[^\n]*\n/sm;
Are you trying to delete *all* message headers? What if there's
something after Xref?
I'd process the file (without undef'ing $/) this way:
while (<$in>) {
next if /^[\w-]+:/ .. /^(?![\w-]+:)/; # skip header lines
print $out $_; # print first line of message
print $out <$in>; # print rest of file
}
> }
>
> close $out; #And, close the file handle
This isn't strictly necessary, although I prefer to explicitly close
handles. Be consistent though. Either close both $in and $out or neither.
> 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... :)
You need a loop. The idiomatic loop for perl is a "foreach" loop.
foreach my $file (@ARGV) {
next unless -f $file; # skip missing or non-text files
# (open files)
# (process file)
}
-mjc
|
|
Posted by Uri Guttman on July 11, 2008, 10:49 pm
Please log in for more thread options
MC> Jonathan Clark wrote:
>> #!/usr/bin/perl
>> use warnings;
>> use strict;
MC> So far, so good. :D
>> undef $/; #read that this allows the whole file to be read in,
>> rather than as lines or paragraphs.
MC> Slurping is okay for small files but doesn't scale. It should be fine
MC> for newsgroup postings.
the definition of small files has grown considerably. it used to mean
<1k and today 1mb can be condsidered small. most common text files
(sources, *ML, docs, configs, etc.) are easily slurped. and slurping if
done right is faster. of course doing it right means using
File::Slurp. it is cleaner code than undefing $/ and it is faster too.
MC> die "Usage: $0 <file>\n";
>> open my $in, '<', $file or die "Can't read old file: $!";
>> open my $out, '>', "$file.new" or die "Can't write new file: $!";
MC> Good x 3. You're using three-arg open(), checking its return value,
MC> and including the error in any message.
a good extra is to put the file name in the (improved) error string.
open my $in, '<', $file or die "Can't open $file: $!";
open my $out, '>', "$file.new" or die "Can't create $file.new: $!";
MC> Since you're slurping the file you don't need a loop at all.
MC> $_ = <$in>;
use File::Slurp ;
# no need to call open and <>. also no need to touch $/
my $post = read_file( $file ) ;
MC> Are you trying to delete *all* message headers? What if there's
MC> something after Xref?
then delete up to the first blank line (2 newlines or crlf
pairs). someone else commented on this already
MC> I'd process the file (without undef'ing $/) this way:
MC> while (<$in>) {
MC> next if /^[\w-]+:/ .. /^(?![\w-]+:)/; # skip header lines
what about continuation header lines?
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
|
|
Posted by Dr.Ruud on July 12, 2008, 4:50 pm
Please log in for more thread options Uri Guttman schreef:
> today 1mb can be condsidered small
millibits were already small.
--
Affijn, Ruud
"Gewoon is een tijger."
|
|
Posted by JustinSain on July 12, 2008, 6:48 pm
Please log in for more thread options > Uri Guttman schreef:
>
>> today 1mb can be condsidered small
>
> millibits were already small.
>
> --
> Affijn, Ruud
Roflmao ;-) Precisely !
|
| Similar Threads | Posted | | Parsing some input - easy to explain but lengthy to code - suggestions? | January 14, 2007, 9:13 am |
| grep: \" and " both Give Errors | December 28, 2005, 9:52 am |
| Larry or Linus? Give credit to everyone else... | January 1, 2007, 6:09 am |
| Why does undef-> give a warning and $undefined_var-> doesn't | January 3, 2008, 4:00 am |
| unicode: equal strings give different results? | September 28, 2004, 12:56 am |
| Give benefit of doubt to perl newbies | August 30, 2005, 12:46 pm |
| How to give an array to a function ? Whats wrong | December 21, 2006, 5:12 am |
| Please give me a good "rule-of-thumb" for back-slashing in character classes | May 9, 2007, 11:06 am |
| Any suggestions? | October 19, 2004, 3:47 pm |
| Any GUI Toolkit Suggestions? | September 6, 2008, 12:25 pm |
|