Your thread title is slightly misleading. You say "To import or not to import", which made me think of modules and namespaces. Anyway...

Writing functions (and reusing code) and using modules is a basic tenet of good programming practice. Good programming has never been about keeping code long or short -- it's about what is the most flexible, safe, or feature rich -- balancing requirements.

The only time to 'inline' something would be when the function call was expensive (to save CPU load or equivalent), and here running time is not a major concern. If you were doing math, this might be different.

Of course, quick and dirty scripts are allowed to have their way in many occasions, but you incur risks if they grow to be something important, so it all matters what your approach is.

If you are trying to write 'good' code though, 'good' and 'short' are orthogonal. Often good can be short, but short is not always good. Good is usually clean, neat, and non-kludgy however. If you find a lot of error checking code making your program harder to read, move it to a function so you can keep your code clean -- but don't remove that code just because it makes the code shorter. In the case of your CGI example, I really don't find that syntax ugly at all, though I usually format my code so the dashes and arrows line up, at minimum. It makes me feel good:

my $cookie = $q->cookie( -name => "cart_id", -value => 12345, -domain => ".oreilly.com", -expires => "+1y", -path => "/cgi", -secure => 1 ); print "Set-Cookie: $cookie\n";

Silly, yes, but it makes that long code block seem fun and awe-inspiring. Ok, maybe not, but at least it's more readable -- especially when you want to start putting dollar signs in there.


In reply to Re: When to use modules? by flyingmoose
in thread When to use modules? by wolfi

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.