mofo has asked for the wisdom of the Perl Monks concerning the following question:

Hi,
I've got a very weird problem that is floating high above my head. Here's the concept code below.

open(FD,"+< named.conf"); $domain="domain.com"; while(<FD>) { if (/^zone \"$domain\" \{/ .. /\}\;/) { print; #show me that config block } } close(FD);


As you can see I'm working with the infamous named.conf bind config file. The routine works great for what I want it to do (pull out each zone block). BUT, I want to then remove the config block from the file and rewrite it, it will print the config block plus everything around it, not the opposite of the block. See this example.

open(FD,"+< named.conf"); $domain="domain.com"; while(<FD>) { if ($_ !~ /^zone \"$domain\" \{/ .. /\}\;/) { print; #show me everything but what I asked for push (@outtext,$_); #put it away for later use } } close(FD);


What am I doing wrong to make it display EVERYTHING instead of the opposite of what I told it to ignore? (?)

Thanks,
-reid

Replies are listed 'Best First'.
Re: Removing lines from a file.
by buckaduck (Chaplain) on Apr 18, 2001 at 01:24 UTC
    Your logic is flawed. Instead of this:
    if ($_ !~ /^zone \"$domain\" \{/ .. /\}\;/)
    you should really have this:
    unless (/^zone \"$domain\" \{/ .. /\}\;/)

    Why? In your version you use the (/A/ .. /B/) syntax to extract a range of lines between /A/ being true and /B/ being true. When you test for ($_ !~ /A/ .. /B/) then you get everything between /A/ being false and /B/ being true. That's not what you want.

    buckaduck

      Yes! You nailed it! That makes perfect sense now. I will use unless more often now instead of making my life difficult. You reply really helped me out!
(ar0n: BIND::Conf_Parser) Re: Removing lines from a file.
by ar0n (Priest) on Apr 18, 2001 at 00:29 UTC
    I haven't read your post - shame on me - but I do know there's a BIND::Conf_Parser. More specifically, there's a $self->handle_zone( $name, $class, $type, \%options ) Could it be that this is what you want?

    Bow before the great CPAN!

    -- votes welcome

    ar0n ]

      Thanks much for the CPAN link, but I'm simply trying to avoid installing it on a machine just for the sake of deleting an entry. I might just use it though :)
Re: Removing lines from a file.
by arturo (Vicar) on Apr 18, 2001 at 00:41 UTC

    Go with two files, the original, and a scratch file. Read in from the original and just print that line out to the scratch file unless it's a line you're interested in. Mangle as appropriate (or ignore) if you are interested, and print out to scratch file. When you're all done, copy the scratch file over the original. Make backups beforehand as appropriate =)

    Philosophy can be made out of anything. Or less -- Jerry A. Fodor

Re: Removing lines from a file.
by Albannach (Monsignor) on Apr 18, 2001 at 01:07 UTC
    Just a couple of minor points (because I can't quite grok your last sentence this late in the day):
    • note that m// interpolates so your search will match on 'domainacom' as well as 'domain.com'
    • instead of  if(!something){} I find  unless(something){} more readable, and that can sometimes help with the logic

    --
    I'd like to be able to assign to an luser

Re: Removing lines from a file.
by michellem (Friar) on Apr 18, 2001 at 00:47 UTC
    This might not help much - but might it have to do with where you have your line breaks in the file?