Some suggestions for improvement:
- Use HERE-docs
- Use the available special variables ($. == line number)
- Include the error variable $! in your die statements
- Use a while loop instead of a foreach loop as the latter reads the whole file into memory. This is totally unnecessary here.
- Translate your variable names from German ;-)
Having said this ... here is how I'd do it:
#!/usr/local/bin/perl -w
use strict;
sub usage {
print << "USAGE";
'REMoveREMarks'!
remrem.pl deletes blank lines or lines starting with a '#' from a give
+n file-one and prints it to STDOUT or writes it to file-two
Syntax :
$0 inputfile
# STDOUT, try $0 inputfile | less
$0 inputfile outputfile
#Output to <outputfile>
USAGE
exit;
}
usage() if (!@ARGV or $ARGV[0] eq '--help');
open(FUN,"<$ARGV[0]") || die "Can't open file $ARGV[0] for reading: $!
+";
if ($ARGV[1]) {
open(GUN,">>$ARGV[1]") || die "Can't open file $ARGV[1] for writing:
+ $!"
}
while (<FUN>) {
if (!/^#/ and /\w/) {
if ($ARGV[1]) {
print GUN $_;
}
else {
print "$.: $_"
}
}
}
if ($ARGV[1]) {
close(GUN) || die "Can't close file $ARGV[1]: $!"
}
close(FUN) || die "Can't close file $ARGV[0]: $!"
-- Hofmator
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.