in reply to strip perl comment lines
merlyn beat me with some of his comments (I'm only on my second cup of coffee) but I would add that if you think it unlikely that here-docs (or multiline quoted strings) will contain comments, I know I have such programs. Additionally, it is also conceivable that a # character could be used as a delimiter for one of the quoting or regex operators:
$string =~ m# (some pattern) #x;
If I were to do this I would take merlyn's suggestion of using '-' (but also allow the second argument to be optional), open the output handle up front (using $! in the error message) and take care of output right in the while loop so we don't need to build up the output in memory -- something along the lines of:
#!/usr/bin/perl -w use strict; die <<USAGE unless @ARGV and @ARGV <= 2; $0 strips comment lines beginning with # from perl code usage: perl $0 infile [outfile] (output to stdout if no outfile given) USAGE my $infile = shift; my $outfile = shift || '-'; open(IN,"< $infile") or die "Couldn't open $infile: $!"; open(OUT, ">$outfile") or die "Couldn't open $outfile: $!"; my ($code, $comments) = (0,0); while(<IN>) { $comments++ and next if /^\s*#[^!]/; print OUT; $code++ } close IN; close OUT; my $total = $code + $comments; print<<SUMMARY; $total lines read from $infile $comments comment lines detected in $infile $code lines written to $outfile SUMMARY
But, in reality, I wouldn't really do this because it is destined to fail on some Perl code for reasons already given, and we haven't even mentioned accidentally stripping things that look like comments in POD sections.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: strip perl comment lines
by quinkan (Monk) on Mar 05, 2001 at 16:14 UTC | |
by danger (Priest) on Mar 05, 2001 at 20:35 UTC |