I've run across an obstacle in Perl that I didn't expect. Reading through the module tutorial, it appears that this obstacle is intended. Seems odd to me, since the Perl motto is TMTOWTDI.

The way I'd like to do it seems to me the most logical, but Perl, apparently, says I can't do it this way :( Mayhap my gray-matter processing unit is defective...

This is the problem : I have a set directory tree I'm working with, and a number scripts that process the files.

Seems to me the most straight-forward way to handle this would be to load the directory structure, or specific branches of it, into arrays.

I'd like to make these arrays, and their contents, available to a script by coding them in a module, as I've done with hard-coded variables. Is this something that Perl doesn't want me to do ?

I've considered either script to traverse the directory tree, or callable sub-routines, but these seem like overkill for a set directory tree. Either one seem to me to be "making an easy thing hard" :P

An example, with a subset of the directory tree:

The initial files are downloaded to the DownLoad folder. These are update files for data sets I've already started.

The files in the DownLoad folder are moved to the appropriate "raw" folder, retained as an archive.

The "raw" folder files, which are updates, are appended to the existing data from the files in the "processed" folders. The merged data overwrites the files in the "processed" folders.

The "processed" files are analyzed, with summary reports placed in the "analysis" folders.

Sample tree structure:

\DownLoad (top folder, initial downloaded files)

\DownLoad\Weekly\raw (downloaded Weekly files moved here)
\DownLoad\Weekly\processed (merged files)
\DownLoad\Weekly\analysis (file summary reports)

\DownLoad\Daily\raw (downloaded Daily files moved here)
\DownLoad\Daily\processed (merged files)
\DownLoad\Daily\analysis (file summary reports)

I'm writing scripts to process files in those directories. Then, for example:

foreach $RawFileName ( $DownLoadFolder )
{ distribution script, files moved to "raw" folders }

foreach $AppendFileName ( @WeeklyDirTreeArray ) { (script) }
or
foreach $AnalyzeFileName ( @DailyDirTreeArray ) { (script) }

This works, but I'm only able to do this by pasting the array declaration and initialization code in each script. Seems cumbersome and kludgy. If I change or add to the tree in the future, I'll have to propagate the changes manually to each script - more kludgy copy-and-paste.

Unlike hard-coded variables which most or all of the scripts use, I can't declare the arrays in a module and fill them - which seems to me the logical way to handle hard-coded arrays.

Writing script to traverse the directory tree would work, but seems like unnecessary overhead for set-in-place traversing routines. And the traversing code would be copied-and-pasted into each script, which still seems kludgy. Likewise if I change or add to the directory - manual editing of the traverse code in each script.

Creating a callable sub-routine in a module, which would declare and initialize the arrays, would work, but seems to be unnecessarily complicated overhead for a basic programming problem. Again, "making an easy thing hard", which just don't seem very Perl-like :)


In reply to Fill an array in a module ? by DarrenSol

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.