FAQ
Hi;

I need to find Makefiles that contain a backslash line continuation
character followed by a blank (or whitespace only) line. I tried a
regular expression first but just couldn't get it right. I then tried
comparing by a pair of strings, but that isn't right either.

First try:
############################################
#!/usr/bin/perl

use strict;
use warnings;
use diagnostics;

my $valid_string = "sample text\\n second line is valid\n";
my $invalid_string = "sample text \\n \n";
if ($invalid_string =~ m|\\\n\W$|) {
print "INVALID\n" ;
} else {
print "valid\n";
}
if ($valid_string =~ m|\\\n\W$|) {
print "INVALID\n" ;
} else {
print "valid\n";
}
############################################

Second try:
############################################
#!/usr/bin/perl

use strict;
use warnings;
use diagnostics;

foreach my $file (@ARGV) {
open (MAKEFILE, $file) or die "ERROR: Unable to open " . $file . "
for reading, $!";
my $line_before = "";
my $linecount;
while (my $line = <MAKEFILE>) {
chomp $line;
$linecount++;
print "DEBUG: " . ($linecount-1) . " line (before) = |" .
$line_before . "|\n";
print "DEBUG: " . $linecount . " line (current) = |" . $line
. "|\n\n";
if ($line_before =~ m|\\$|) {
print $file . " has a backslash continuation to a following
blank line at or near line " . $linecount . "\n" if ($line =~ m|^\W*$|);
} else {
$line_before = $line;
}
}
close (MAKEFILE);
}
############################################

The first one reports both strings as valid, so the regex is wrong?

The second one incorrectly reports that all Makefiles have one or more
lines with continuation (\) characters at the end of the line followed
by a blank (or whitespace only) line.

Please help,
Thanks in advance,
Ken Wolcott

Search Discussions

  • John W. Krahn at Aug 4, 2004 at 1:20 am

    Ken Wolcott wrote:
    Hi; Hello,
    I need to find Makefiles that contain a backslash line continuation
    character followed by a blank (or whitespace only) line. I tried a
    regular expression first but just couldn't get it right. I then tried
    comparing by a pair of strings, but that isn't right either.

    [snip code]
    This will do what you want:

    #!/usr/bin/perl
    use strict;
    use diagnostics;

    while ( <> ) {
    my $line_num = $.;
    if ( /\\\s*$/ ) {
    my $line_before;
    if ( ( $line_before = <> ) =~ /^\s*$/ ) {
    print "$ARGV has a backslash continuation to a following blank line at line $line_num\n";
    }
    else {
    $_ = $line_before;
    redo;
    }
    }
    }

    __END__



    John
    --
    use Perl;
    program
    fulfillment
  • Ken Wolcott at Aug 4, 2004 at 3:10 pm
    John;

    Thank you very much for the help...works like a charm...now I'll try
    to study it carefully to understand how to use this the next time I need
    something like this...

    Cool...

    Ken Wolcott
    On Tue, 2004-08-03 at 20:20, John W. Krahn wrote:
    Ken Wolcott wrote:
    Hi; Hello,
    I need to find Makefiles that contain a backslash line continuation
    character followed by a blank (or whitespace only) line. I tried a
    regular expression first but just couldn't get it right. I then tried
    comparing by a pair of strings, but that isn't right either.

    [snip code]
    This will do what you want:

    #!/usr/bin/perl
    use strict;
    use diagnostics;

    while ( <> ) {
    my $line_num = $.;
    if ( /\\\s*$/ ) {
    my $line_before;
    if ( ( $line_before = <> ) =~ /^\s*$/ ) {
    print "$ARGV has a backslash continuation to a following blank line at line $line_num\n";
    }
    else {
    $_ = $line_before;
    redo;
    }
    }
    }

    __END__



    John
    --
    use Perl;
    program
    fulfillment

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedAug 3, '04 at 5:01p
activeAug 4, '04 at 3:10p
posts3
users2
websiteperl.org

2 users in discussion

Ken Wolcott: 2 posts John W. Krahn: 1 post

People

Translate

site design / logo © 2022 Grokbase