While I have one hard condition that the script must be usable standalone and remain directly usable at the pipe level, turning it into a dual-nature function-library-style module that can still be invoked like a script isn't too difficult. A bit tasteless maybe: That's not a proper refactoring (rather a minor cleanup at best) plus I don't really see many programmatic advantages of 'use' over 'system' for a fairly 'slow' command.

CPAN modules are normally used as building blocks. The advantage of giving your script a functional interface is that it makes it easier to use as a building block. You currently use your script as part of a pipeline, but your callers may have other ideas about how they want to attach input and output streams to your program. By providing a routine that accepts an input and output file handle, you give your future users design options other than a system pipeline. For example, they may wish to have code that runs between the point where the input file handle is created and your routine is called. They may have a single output file handle and a family of input file handles they want to feed to your routine or vice versa.

You also make your program more portable. The system command is not available on all platforms and even if it is implemented, it may not always be possible to request a sequence of commands each piping their output into the next. See perlport for details.

Best, beth


In reply to Re: cpan and (dual-nature) scripts? by ELISHEVA
in thread cpan and (dual-nature) scripts? by jakobi

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.