I have been slowly migrating old code to use Perl modules at work. I found that I was fixing the same bugs over and over in different scripts. I found it was better to fix the bug once, and then have the different scripts use those modules.

Here are my requirements:

My modules are traditional - just listing functions and exporting them for use in scripts. The modules seem to make sense - the SnapMirror functions are in the SnapMirror.pm module, the company-specific validation functions are in Company.pm, the functions relating to backups are in Backup.pm, and so on. The modules probably export more than they should, but I don't believe that to be the root of my problems.

Company.pm - functions specific to my company SnapMirrors.pm - functions specific to NetApp SnapMirrors

Basically, my code broke when I tried to make an enhancement. We wanted the is_clearcase() function to not only detect qtrees containing ClearCase data, but also to detect qtrees that are SnapMirror destinations for ClearCase data. This made is_clearcase() call is_snapmirror_destination() and get_snapmirror_source().

Searching around on the rest of the site, I see comments stating that circular dependencies are bad, but I don't have a good way of resolving this one.

I don't want to omit the error checking. It has helped us find errors in other scripts before. I also don't want to put more work on the other programmers. It's possible that every time that is_clearcase() is called, they could check for the is_snapmirror_destination() case, but it seems like this functionality belongs in the is_clearcase() function itself.

It looks like the code would work if is_clearcase() was in the SnapMirrors.pm module, but the problem is that the other coders wouldn't know to use SnapMirrors.pm when they wanted the is_clearcase() function - it doesn't seem to belong to that module.

I looked in the camel book, and they mentioned as an aside that one would use require instead of use "if you have two modules that each need a function from the other. (And we're not sure that's a good reason.)" They don't mention anything else about it.

There are other posts on this site about using require, import, and BEGIN blocks. They argue about design, but don't really provide helpful information, outside of "don't make circular dependencies."

Finally, I was messing around with a module that would use all the other ones, and then manually export functions that pointed to the correct functions. It works, but it also seems inelegant. It also seems unmaintainable.

I figure it would work if I put everything in one monolithic module, but that doesn't seem to be an elegant solution.

I seem to have coded/designed myself into a corner. Any help would be appreciated.

Thanks in advance,

Chris


In reply to Need help with circular dependencies by chayashida

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.