in reply to Search and replace again

Try parsing the XML with XML::Simple or XML::LibXML.

Replies are listed 'Best First'.
Re^2: Search and replace again
by ikegami (Patriarch) on Apr 19, 2010 at 21:42 UTC
    use strict; use warnings; use XML::LibXML qw( ); my $parser = XML::LibXML->new(); my $doc = $parser->parse_file($ARGV[0]); my $root = $doc->documentElement(); for my $node ($root->findnodes('//image[@scope="local"]')) { $node->removeAttribute('scope'); } binmode STDOUT; print $doc->toString();

    I'm not even gonna try an XML::Simple solution.

      I was curious why you binmode STDOUT in your code (and in your other example) - is it to ensure you have UNIX line endings (and not CRLFs) in the output if Perl is running on Windows?

        For starters, XML is a binary format. binmode definitely won't hurt anything.

        binmode doesn't just disable :crlf; it disables any :encoding too.

        You probably should always use binmode or equivalent (e.g. use open), either to remove layers* when you want to ensure the bytes are unmolested*, or to add some when you want to output text.

        * — These may be added via $ENV{PERLIO}, via -C, or by Perl itself as the case is for :crlf on Windows.