Hi,

I am simply trying to read all the (XML) files from within a directory and make a few text changes to each:

The changes are meant to change all occurrances of XML lines like:

<dc:subject xml:lang="en-US">18th century 17th century</dc:subject>

To:

<dc:subject xml:lang="en-US">17th century</dc:subject>
<dc:subject xml:lang="en-US">18th century</dc:subject>

And then to output the files to a new directory.

However, it seems like there are some errors in my code, esp. for opening/closing the files. Can anyone tell me what is wrong? This is my code:

#!/usr/bin/perl -w use warnings; use strict; my $record_element = 'dcterms:available'; my $filename_element = 'dc:identifier'; my ($file, $dir) = @ARGV; defined($file) && defined($dir) || usage(); chop($dir) if $dir =~ m#/$#; #$/ = "</$record_element>"; mkdir $dir; open(INPUT, "<$ARGV[0]") || die "Failed to open $ARGV[0]\n"; $file = "INPUT"; my $record_count = 0; my $header1 = '<?xml version="1.0" encoding="UTF-8"?>'; my $header2 = '<qualifieddc xmlns="http://example.org/appqualifieddc/" +'; my $header3 = 'xmlns:dc="http://purl.org/dc/elements/1.1/"'; my $header4 = 'xmlns:dcterms="http://purl.org/dc/terms/"'; my $header5 = 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"'; my $header6 = 'xsi:schemaLocation="http://example.org/appqualifieddc/' +; my $header7 = 'http://dublincore.org/schemas/xmls/qdc/2003/04/02/appqu +alifieddc.xsd"'; my $closing = '</qualifieddc>'; $/ ='terms/">'; <INPUT>; $/ = "</$record_element>"; while(<INPUT>){ next if m/^\s*$/; #Disregard 0+ trailing spaces s/^\s*//; #Collapse 0+ leading spaces $record_count++; if (m#<$filename_element xsi:type="dcterms:URI">(\w+) (\w+) (\w+)_(\ +w+)</$filename_element>#s) { msg("Just matched $1 $2 $3_$4"); my $outfile = "$dir/$4.xml"; open (OUTPUT, '>', $outfile) or die "Can't create/open $outfile fo +r writing"; print OUTPUT $header1, "\n", $header2, "\n", $header3, "\n", $ +header4, "\n", $header5, "\n", $header6, "\n", $header7, "\n"; print OUTPUT $_, "\n"; print OUTPUT $closing; close (OUTPUT) or die "Couldn't close $outfile after writing recor +d"; } else { warn("Couldn't find filename in record $record_count, skipping..." +); } } close (INPUT) or die "Couldn't close $file"; close (OUTFILE); $record_count--; msg("Processed $record_count records from $file"); sub msg { print @_, "\n"; } sub usage { msg("Usage: $0 <file> <directory>"); exit(1); }

In reply to Read/Change files in a Directory by LF

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.