> I vaguely understand that local restricts the auto flushing, though I’m
not sure how. Does local go out of scope when the TT is called? More
broadly, do I actually need local? Do I actually need auto flushing?

It basically creates a "local" copy of the global var - local meaning the
current lexical scope (perldoc -f local):
   local EXPR
               You really probably want to be using "my" instead, because
"local" isn’t what most people think of as "local".  See "Private Variables
via
               my()" in perlsub for details.

               A local modifies the listed variables to be local to the
enclosing block, file, or eval.  If more than one value is listed, the list
must be
               placed in parentheses.  See "Temporary Values via local()"
in perlsub for details, including issues with tied arrays and hashes.
so
$| = 1;
if ( $dont_flush eq 'true' ) {
  local $| = 0;
  # do unflushed work\
}    # if dont_flush
# $| is back to its value global value

so the "if" curlies create a sub lexical context.  If it's a whole script
or the main context, local won't make a difference. The idea is to not mess
w/ somebody else's setting of the same var - it doesn't have to be a Perl
built-in,
our $is_one = 1;
if ( $dont_flush eq 'true' ) {
  local $is_one = 0;
   # do something
}    # if dont_flush
# $is_one is "1" here.

but for that (see perldoc above) you'd use "my" and "our"
 my $is_one = 0;
 # do something

On Tue, May 15, 2018 at 11:34 AM, Rick T <p...@reason.net> wrote:

> I’m very much a novice — familiar with Learning Perl but finding
> Intermediate Perl an uphill slog.
>
> The few lines below are from a subroutine that calls Template Toolkit. The
> commented out line I simply copied from Perl Template Toolkit (PTT); the
> line after it is what I replaced it with after seeing Perl Best Practices
> (PBP) show the auto flush variable handled this way. Both lines work,
> though I worry that the first one may not always work or else the use of
> local would not be recommended.
>
> I vaguely understand that local restricts the auto flushing, though I’m
> not sure how. Does local go out of scope when the TT is called? More
> broadly, do I actually need local? Do I actually need auto flushing? The
> authors of PTT and PBP assumed that persons reading their books would be
> fairly accomplished programmers — but I’m not. So can you educate me?
>
> #  $| = 1;
>     local $| = 1; # autoflush output
>
>     print "Content-Type: text/html\n\n";
>     my $tt = Template->new(
> ect.
>
> Many thanks!
> Rick Triplett
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>


-- 

a

Andy Bach,
afb...@gmail.com
608 658-1890 cell
608 261-5738 wk

Reply via email to