The use of Inline::Files and \*STDOUT was simply for demo purposes; hence the "I'll leave you to replace Inline::Files and \*STDOUT with more appropriate I/O.".

I'm not sure whether you've got a handle on this. If you have, you probably won't need the following; if not, here's some hints and tips that may prove useful for your real code. Ask if you need more information, further explanation, etc.

I'd recommend the autodie pragma. This will save a lot of effort checking whether files could be opened, created, read, written and so on. Put it near the top of your code; I usually write this:

... use strict; use warnings; use autodie; ...

Remove the 'use Inline::Files; line.

Use open to create your filehandles.

The output filehandle ($out_fh) is very straightforward:

open my $out_fh, '>', '/path/to/output_file';

The input filehandles are a little more complicated but still fairly easy. I wouldn't recommend creating all the filehandles in advance; instead, open and close one at a time. The \*FILE0, \*FILE1, etc. in my original code are filehandles for the Inline::Files: you'll see I didn't need to explicitly create those. Perhaps change:

my %doc_fh_for = (File0 => \*FILE0, File1 => \*FILE1, File2 => \*FILE2 +);

to something like this

my %source_file_for = (File0 => 'filename0', File1 => 'filename1', Fil +e2 => 'filename2');

and then further down

for (@feature_files) { open my $in_fh, '<', $source_file_for{$_}; write_xml_content($_, $in_fh, $out_fh, ' ' x 8); close $in_fh; }

I don't know whether you have a predetermined list of source files or if you have to find them in one or more directories; if the latter, you may find readdir useful for this. Also note that may need to prefix the filenames with directory paths: while simple concatenation may suffice, I'd normally use File::Spec as it's portable (it's also a core module so there's no need to install it).

-- Ken


In reply to Re^3: copy XML elements from one file to another by kcott
in thread copy XML elements from one file to another by Anonymous Monk

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.