"Thanks for your help"

You're very welcome.

"I have traditionally turned on Autoflush globally when I need it ..., I notice you are scoping it as a local to the subroutine, is there an advantage to this method? or alternatively a disadvantage to using autoflush globally?"

Perl has a number of special variables (see perlvar). Some of these are set in a certain context ($a - sort; $1 - regex; $@ - eval; and so on). Others have default values which are usually fine and don't needed changing; code is written expecting these default values; changing them globally can mean that some code does not function as intended or expected. Changing them with local, in a limited scope, means the change does not affect other code:

$ perl -E 'say $|; { local $| = 1; say $| } say $|' 0 1 0

If you look in "Variables related to filehandles", you'll see (a few paragraphs down):

"You should be very careful when modifying the default values of most special variables described in this document. In most cases you want to localize these variables before changing them, since if you don't, the change may affect other modules which rely on the default values of the special variables that you have changed. ..."

That continues on with good and bad examples. A discussion of scope follows that:

"Usually when a variable is localized you want to make sure that this change affects the shortest scope possible. ..."

There are more good and bad examples; one showing use of an anonymous block.

Further related information can be found in perlsub; in particular, the sections "Temporary Values via local()" (especially the "Localization of special variables" subsection), and "When to Still Use local()".

— Ken


In reply to Re^3: Printing to stdout an array of strings and scalar references by kcott
in thread Printing to stdout an array of strings and scalar references 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.