I don't think Safe.pm is such a good choice. But otherwise, it's a good idea. Our (yours, mine and bloonix's) suggestions are pretty much the same thing--use a sub to wrap the functions--with one key difference. In mine, the data structure contains the program. In yours, the program contains the program.

I've made the arguments to the wrapper functions the same to get an apples to apples comparison.

# Program In Program (PIP) safe_chdir('foo', 'Can't go to foo'); safe_system('foo --bar', 'foo didn\'t work'); safe_chdir('bar', 'bar is unreachable'); # Program In Data (PID) @program = ( [ chdir => 'foo', 'Can't go to foo' ], [ system => 'foo --bar', 'foo didn\'t work' ], [ chdir => 'bar', 'bar is unreachable' ], ); run_program(\@program); # Hiding the dispatch and loop processing in a + sub.

The program in program (PIP) approach is a bit less cumbersome and simpler than the program in data (PID) method. However, with PID, you can easily modify your program by changing the data structure.

Is this a strength or a weakness of PID? It really depends on the circumstances and goals of the project. I tend to lean towards a more data driven approach, because I like the flexibility it offers. However, in a program I've been working on over the last week, I use the technique you describe.

Reflecting upon how I decide on an approach, I find that when I have a small number distinct operations (chdir and system) that must be performed many times as a group, then I use the PID style. However if I have small chunks of repeated code sprinkled around the rest of the program, then I use PIP style.


TGI says moo


In reply to Re^2: Too many "or die" clauses? by TGI
in thread Too many "or die" clauses? 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.