local and auto flush

I=E2=80=99m very much a novice =E2=80=94 familiar with Learning Perl but =
finding Intermediate Perl an uphill slog.=20

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=E2=80=99m 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 =E2=80=94 but I=E2=80=99m =
not. So can you educate me?=20

#  $| =3D 1;=20
    local $| =3D 1; # autoflush output

    print "Content-Type: text/html\n\n";
    my $tt =3D Template->new(  =20
ect.

Many thanks!
Rick Triplett=
0
perl
5/15/2018 4:34:01 PM
perl.beginners 29287 articles. 3 followers. Follow

3 Replies
9 Views

Similar Articles

[PageSpeed] 25

--000000000000fd1d09056c4191b8
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

Hi Rick,

Regarding "local":

$| is a global* variable so you use local to isolate this change to only
where you need it.

To see what I mean, run this:

print "step1: $| \n";

{

    local $| =3D 1;


    print "step2: $| \n";

}

print "step3: $| \n";

and you'll get output:

step1: 0

step2: 1

step3: 0

while removing the 'local' you'll get

step1: 0

step2: 1

step3: 1

Does that answer your question?

Regarding autoflushing - that just means that it won't store the output in
a buffer and wait until the buffer is full (or until the process is
complete) before outputting it. This is a helpful description:

https://stackoverflow.com/questions/6303515/perl-1-what-is-this
<https://app.mailtag.io/link-event-v2?mt__id=3Dc6c5700a-5861-11e8-824e-06cf=
19e8c68c&mt__url=3Dhttps%3A%2F%2Fstackoverflow.com%2Fquestions%2F6303515%2F=
perl-1-what-is-this>

Sorry I can't think of an easy way to replicate buffering in a short
script, but it certainly makes sense when responding to an HTTP request.

Andrew

* It's more accurate to say that $| is a package variable of the main
package, but "global" is close enough for this discussion :-)



On Tue, May 15, 2018 at 5:34 PM, Rick T <perl@reason.net> wrote:

> I=E2=80=99m very much a novice =E2=80=94 familiar with Learning Perl but =
finding
> Intermediate Perl an uphill slog.
>
> The few lines below are from a subroutine that calls Template Toolkit. Th=
e
> 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=E2=
=80=99m
> 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 =E2=80=94 but I=E2=80=99m not. So can you=
 educate me?
>
> #  $| =3D 1;
>     local $| =3D 1; # autoflush output
>
>     print "Content-Type: text/html\n\n";
>     my $tt =3D Template->new(
> ect.
>
> Many thanks!
> Rick Triplett
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> http://learn.perl.org/
>
>
>


--=20
Andrew Solomon

Perl Trainer, Geekuni http://geekuni.com/
andrew@geekuni.com // +44 7931 946 062

--000000000000fd1d09056c4191b8
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><img alt=3D"" src=3D"https://app.mailtag.io/email-event/c6=
c4bbce-5861-11e8-9184-06cf19e8c68c.png?ts=3D353878245" style=3D"display: no=
ne;">Hi Rick,<div><br></div><div>Regarding &quot;local&quot;:</div><div><br=
></div><div>$| is a global* variable so you use local to isolate this chang=
e to only where you need it.</div><div><br></div><div>To see what I mean, r=
un this:</div><div><br></div><div>




<span></span>





<p class=3D"gmail-p1" style=3D"margin:0px;font-style:normal;font-variant:no=
rmal;font-weight:normal;font-stretch:normal;font-size:12px;line-height:norm=
al;font-family:Monaco;color:rgb(255,64,255);background-color:rgb(0,0,0)"><s=
pan class=3D"gmail-s1" style=3D"font-variant-ligatures:no-common-ligatures;=
color:rgb(236,236,21)">print</span><span class=3D"gmail-s2" style=3D"font-v=
ariant-ligatures:no-common-ligatures;color:rgb(245,245,245)"> </span><span =
class=3D"gmail-s3" style=3D"font-variant-ligatures:no-common-ligatures">&qu=
ot;step1: </span><span class=3D"gmail-s4" style=3D"font-variant-ligatures:n=
o-common-ligatures;color:rgb(40,239,239)">$|</span><span class=3D"gmail-s3"=
 style=3D"font-variant-ligatures:no-common-ligatures"> </span><span class=
=3D"gmail-s5" style=3D"font-variant-ligatures:no-common-ligatures;color:rgb=
(255,221,221)">\n</span><span class=3D"gmail-s3" style=3D"font-variant-liga=
tures:no-common-ligatures">&quot;</span><span class=3D"gmail-s2" style=3D"f=
ont-variant-ligatures:no-common-ligatures;color:rgb(245,245,245)">;</span><=
/p>
<p class=3D"gmail-p2" style=3D"margin:0px;font-style:normal;font-variant:no=
rmal;font-weight:normal;font-stretch:normal;font-size:12px;line-height:norm=
al;font-family:Monaco;color:rgb(245,245,245);background-color:rgb(0,0,0)"><=
span class=3D"gmail-s3" style=3D"font-variant-ligatures:no-common-ligatures=
">{</span></p>
<p class=3D"gmail-p2" style=3D"margin:0px;font-style:normal;font-variant:no=
rmal;font-weight:normal;font-stretch:normal;font-size:12px;line-height:norm=
al;font-family:Monaco;color:rgb(245,245,245);background-color:rgb(0,0,0)"><=
span class=3D"gmail-s3" style=3D"font-variant-ligatures:no-common-ligatures=
"><span class=3D"gmail-Apple-converted-space">=C2=A0 =C2=A0 </span></span><=
span class=3D"gmail-s1" style=3D"font-variant-ligatures:no-common-ligatures=
;color:rgb(236,236,21)">local</span><span class=3D"gmail-s3" style=3D"font-=
variant-ligatures:no-common-ligatures"> </span><span class=3D"gmail-s4" sty=
le=3D"font-variant-ligatures:no-common-ligatures;color:rgb(40,239,239)">$|<=
/span><span class=3D"gmail-s3" style=3D"font-variant-ligatures:no-common-li=
gatures"> =3D </span><span class=3D"gmail-s6" style=3D"font-variant-ligatur=
es:no-common-ligatures;color:rgb(255,64,255)">1</span><span class=3D"gmail-=
s3" style=3D"font-variant-ligatures:no-common-ligatures">;</span></p>
<p class=3D"gmail-p3" style=3D"margin:0px;font-style:normal;font-variant:no=
rmal;font-weight:normal;font-stretch:normal;font-size:12px;line-height:norm=
al;font-family:Monaco;color:rgb(245,245,245);background-color:rgb(0,0,0);mi=
n-height:16px"><span class=3D"gmail-s3" style=3D"font-variant-ligatures:no-=
common-ligatures"></span><br></p>
<p class=3D"gmail-p1" style=3D"margin:0px;font-style:normal;font-variant:no=
rmal;font-weight:normal;font-stretch:normal;font-size:12px;line-height:norm=
al;font-family:Monaco;color:rgb(255,64,255);background-color:rgb(0,0,0)"><s=
pan class=3D"gmail-s2" style=3D"font-variant-ligatures:no-common-ligatures;=
color:rgb(245,245,245)"><span class=3D"gmail-Apple-converted-space">=C2=A0 =
=C2=A0 </span></span><span class=3D"gmail-s1" style=3D"font-variant-ligatur=
es:no-common-ligatures;color:rgb(236,236,21)">print</span><span class=3D"gm=
ail-s2" style=3D"font-variant-ligatures:no-common-ligatures;color:rgb(245,2=
45,245)"> </span><span class=3D"gmail-s3" style=3D"font-variant-ligatures:n=
o-common-ligatures">&quot;step2: </span><span class=3D"gmail-s4" style=3D"f=
ont-variant-ligatures:no-common-ligatures;color:rgb(40,239,239)">$|</span><=
span class=3D"gmail-s3" style=3D"font-variant-ligatures:no-common-ligatures=
"> </span><span class=3D"gmail-s5" style=3D"font-variant-ligatures:no-commo=
n-ligatures;color:rgb(255,221,221)">\n</span><span class=3D"gmail-s3" style=
=3D"font-variant-ligatures:no-common-ligatures">&quot;</span><span class=3D=
"gmail-s2" style=3D"font-variant-ligatures:no-common-ligatures;color:rgb(24=
5,245,245)">;</span></p>
<p class=3D"gmail-p2" style=3D"margin:0px;font-style:normal;font-variant:no=
rmal;font-weight:normal;font-stretch:normal;font-size:12px;line-height:norm=
al;font-family:Monaco;color:rgb(245,245,245);background-color:rgb(0,0,0)"><=
span class=3D"gmail-s3" style=3D"font-variant-ligatures:no-common-ligatures=
">}</span></p>
<p class=3D"gmail-p1" style=3D"margin:0px;font-style:normal;font-variant:no=
rmal;font-weight:normal;font-stretch:normal;font-size:12px;line-height:norm=
al;font-family:Monaco;color:rgb(255,64,255);background-color:rgb(0,0,0)"><s=
pan class=3D"gmail-s1" style=3D"font-variant-ligatures:no-common-ligatures;=
color:rgb(236,236,21)">print</span><span class=3D"gmail-s2" style=3D"font-v=
ariant-ligatures:no-common-ligatures;color:rgb(245,245,245)"> </span><span =
class=3D"gmail-s3" style=3D"font-variant-ligatures:no-common-ligatures">&qu=
ot;step3: </span><span class=3D"gmail-s4" style=3D"font-variant-ligatures:n=
o-common-ligatures;color:rgb(40,239,239)">$|</span><span class=3D"gmail-s3"=
 style=3D"font-variant-ligatures:no-common-ligatures"> </span><span class=
=3D"gmail-s5" style=3D"font-variant-ligatures:no-common-ligatures;color:rgb=
(255,221,221)">\n</span><span class=3D"gmail-s3" style=3D"font-variant-liga=
tures:no-common-ligatures">&quot;</span><span class=3D"gmail-s2" style=3D"f=
ont-variant-ligatures:no-common-ligatures;color:rgb(245,245,245)">;</span><=
/p>


<br></div><div>and you&#39;ll get output:</div><div><br></div><div>




<span></span>





<p class=3D"gmail-p1" style=3D"margin:0px;font-style:normal;font-variant:no=
rmal;font-weight:normal;font-stretch:normal;font-size:12px;line-height:norm=
al;font-family:Monaco;color:rgb(245,245,245);background-color:rgb(0,0,0)"><=
span class=3D"gmail-s1" style=3D"font-variant-ligatures:no-common-ligatures=
">step1: 0<span class=3D"gmail-Apple-converted-space">=C2=A0</span></span><=
/p>
<p class=3D"gmail-p1" style=3D"margin:0px;font-style:normal;font-variant:no=
rmal;font-weight:normal;font-stretch:normal;font-size:12px;line-height:norm=
al;font-family:Monaco;color:rgb(245,245,245);background-color:rgb(0,0,0)"><=
span class=3D"gmail-s1" style=3D"font-variant-ligatures:no-common-ligatures=
">step2: 1<span class=3D"gmail-Apple-converted-space">=C2=A0</span></span><=
/p>
<p class=3D"gmail-p1" style=3D"margin:0px;font-style:normal;font-variant:no=
rmal;font-weight:normal;font-stretch:normal;font-size:12px;line-height:norm=
al;font-family:Monaco;color:rgb(245,245,245);background-color:rgb(0,0,0)"><=
span class=3D"gmail-s1" style=3D"font-variant-ligatures:no-common-ligatures=
">step3: 0<span class=3D"gmail-Apple-converted-space">=C2=A0</span></span><=
/p>


<br></div><div>while removing the &#39;local&#39; you&#39;ll get</div><div>=
<br></div><div>




<span></span>





<p class=3D"gmail-p1" style=3D"margin:0px;font-style:normal;font-variant:no=
rmal;font-weight:normal;font-stretch:normal;font-size:12px;line-height:norm=
al;font-family:Monaco;color:rgb(245,245,245);background-color:rgb(0,0,0)"><=
span class=3D"gmail-s1" style=3D"font-variant-ligatures:no-common-ligatures=
">step1: 0<span class=3D"gmail-Apple-converted-space">=C2=A0</span></span><=
/p>
<p class=3D"gmail-p1" style=3D"margin:0px;font-style:normal;font-variant:no=
rmal;font-weight:normal;font-stretch:normal;font-size:12px;line-height:norm=
al;font-family:Monaco;color:rgb(245,245,245);background-color:rgb(0,0,0)"><=
span class=3D"gmail-s1" style=3D"font-variant-ligatures:no-common-ligatures=
">step2: 1<span class=3D"gmail-Apple-converted-space">=C2=A0</span></span><=
/p>
<p class=3D"gmail-p1" style=3D"margin:0px;font-style:normal;font-variant:no=
rmal;font-weight:normal;font-stretch:normal;font-size:12px;line-height:norm=
al;font-family:Monaco;color:rgb(245,245,245);background-color:rgb(0,0,0)"><=
span class=3D"gmail-s1" style=3D"font-variant-ligatures:no-common-ligatures=
">step3: 1<span class=3D"gmail-Apple-converted-space">=C2=A0</span></span><=
/p>


<br></div><div>Does that answer your question?</div><div><br></div><div>Reg=
arding autoflushing - that just means that it won&#39;t store the output in=
 a buffer and wait until the buffer is full (or until the process is comple=
te) before outputting it. This is a helpful description:</div><div><br></di=
v><div><a href=3D"https://app.mailtag.io/link-event-v2?mt__id=3Dc6c5700a-58=
61-11e8-824e-06cf19e8c68c&amp;mt__url=3Dhttps%3A%2F%2Fstackoverflow.com%2Fq=
uestions%2F6303515%2Fperl-1-what-is-this">https://stackoverflow.com/questio=
ns/6303515/perl-1-what-is-this</a><br></div><div><br></div><div>Sorry I can=
&#39;t think of an easy way to replicate buffering in a short script, but i=
t certainly makes sense when responding to an HTTP request.</div><div><br><=
/div><div>Andrew</div><div><br></div><div>* <span style=3D"color:rgb(34,34,=
34);font-family:arial,sans-serif;font-size:small;font-style:normal;font-var=
iant-ligatures:normal;font-variant-caps:normal;font-weight:400;letter-spaci=
ng:normal;text-align:start;text-indent:0px;text-transform:none;white-space:=
normal;word-spacing:0px;background-color:rgb(255,255,255);text-decoration-s=
tyle:initial;text-decoration-color:initial;float:none;display:inline">It&#3=
9;s more accurate to say that $| is a package variable of the main package,=
 but &quot;global&quot; is close enough for this discussion :-)</span></div=
><div><span style=3D"color:rgb(34,34,34);font-family:arial,sans-serif;font-=
size:small;font-style:normal;font-variant-ligatures:normal;font-variant-cap=
s:normal;font-weight:400;letter-spacing:normal;text-align:start;text-indent=
:0px;text-transform:none;white-space:normal;word-spacing:0px;background-col=
or:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:ini=
tial;float:none;display:inline"><br></span></div><div><span style=3D"color:=
rgb(34,34,34);font-family:arial,sans-serif;font-size:small;font-style:norma=
l;font-variant-ligatures:normal;font-variant-caps:normal;font-weight:400;le=
tter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;wh=
ite-space:normal;word-spacing:0px;background-color:rgb(255,255,255);text-de=
coration-style:initial;text-decoration-color:initial;float:none;display:inl=
ine"><br></span></div></div><div class=3D"gmail_extra"><br><div class=3D"gm=
ail_quote">On Tue, May 15, 2018 at 5:34 PM, Rick T <span dir=3D"ltr">&lt;<a=
 href=3D"mailto:perl@reason.net" target=3D"_blank">perl@reason.net</a>&gt;<=
/span> wrote:<br><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8=
ex;border-left:1px #ccc solid;padding-left:1ex">I=E2=80=99m very much a nov=
ice =E2=80=94 familiar with Learning Perl but finding Intermediate Perl an =
uphill slog. <br>
<br>
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 li=
ne after it is what I replaced it with after seeing Perl Best Practices (PB=
P) 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 woul=
d not be recommended.<br>
<br>
I vaguely understand that local restricts the auto flushing, though I=E2=80=
=99m not sure how. Does local go out of scope when the TT is called? More b=
roadly, do I actually need local? Do I actually need auto flushing? The aut=
hors of PTT and PBP assumed that persons reading their books would be fairl=
y accomplished programmers =E2=80=94 but I=E2=80=99m not. So can you educat=
e me? <br>
<br>
#=C2=A0 $| =3D 1; <br>
=C2=A0 =C2=A0 local $| =3D 1; # autoflush output<br>
<br>
=C2=A0 =C2=A0 print &quot;Content-Type: text/html\n\n&quot;;<br>
=C2=A0 =C2=A0 my $tt =3D Template-&gt;new(=C2=A0 =C2=A0<br>
ect.<br>
<br>
Many thanks!<br>
Rick Triplett<br>
<span class=3D"HOEnZb"><font color=3D"#888888">-- <br>
To unsubscribe, e-mail: <a href=3D"mailto:beginners-unsubscribe@perl.org">b=
eginners-unsubscribe@perl.org</a><br>
For additional commands, e-mail: <a href=3D"mailto:beginners-help@perl.org"=
>beginners-help@perl.org</a><br>
<a href=3D"http://learn.perl.org/" rel=3D"noreferrer" target=3D"_blank">htt=
p://learn.perl.org/</a><br>
<br>
<br>
</font></span></blockquote></div><br><br clear=3D"all"><div><br></div>-- <b=
r><div class=3D"gmail_signature" data-smartmail=3D"gmail_signature"><div di=
r=3D"ltr"><div><div dir=3D"ltr"><div><div dir=3D"ltr">Andrew Solomon<div><s=
pan style=3D"font-size:12.8px"><br></span></div><div><span style=3D"font-si=
ze:12.8px">Perl Trainer, Geekuni=C2=A0</span><a href=3D"http://geekuni.com/=
" style=3D"font-size:12.8px" target=3D"_blank">http://geekuni.com/</a><br><=
/div><div><a href=3D"mailto:andrew@geekuni.com" target=3D"_blank">andrew@ge=
ekuni.com</a> // +44 7931 946 062</div></div></div></div></div></div></div>
</div>

--000000000000fd1d09056c4191b8--
0
andrew
5/15/2018 5:02:40 PM
--0000000000005509ab056c4194eb
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

> I vaguely understand that local restricts the auto flushing, though I=E2=
=80=99m
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=E2=80=99t what most people think of as "local".  See "Private V=
ariables
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
$| =3D 1;
if ( $dont_flush eq 'true' ) {
  local $| =3D 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 =3D 1;
if ( $dont_flush eq 'true' ) {
  local $is_one =3D 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 =3D 0;
 # do something

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

> I=E2=80=99m very much a novice =E2=80=94 familiar with Learning Perl but =
finding
> Intermediate Perl an uphill slog.
>
> The few lines below are from a subroutine that calls Template Toolkit. Th=
e
> 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=E2=
=80=99m
> 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 =E2=80=94 but I=E2=80=99m not. So can you=
 educate me?
>
> #  $| =3D 1;
>     local $| =3D 1; # autoflush output
>
>     print "Content-Type: text/html\n\n";
>     my $tt =3D Template->new(
> ect.
>
> Many thanks!
> Rick Triplett
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> http://learn.perl.org/
>
>
>


--=20

a

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

--0000000000005509ab056c4194eb
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div>&gt; I vaguely understand that local restricts the au=
to flushing, though I=E2=80=99m=20
not sure how. Does local go out of scope when the TT is called? More=20
broadly, do I actually need local? Do I actually need auto flushing?</div><=
div><br></div><div>It basically creates a &quot;local&quot; copy of the glo=
bal var - local meaning the current lexical scope (perldoc -f local):</div>=
<div>=C2=A0=C2=A0 local EXPR<br>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 You really probably want to be u=
sing &quot;my&quot; instead, because &quot;local&quot; isn=E2=80=99t what m=
ost people think of as &quot;local&quot;.=C2=A0 See &quot;Private Variables=
 via<br>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A0 my()&quot; in perlsub for details.<br><br>=C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 A =
local modifies the listed variables to be local to the enclosing block, fil=
e, or eval.=C2=A0 If more than one value is listed, the list must be<br>=C2=
=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=
=C2=A0 placed in parentheses.=C2=A0 See &quot;Temporary Values via local()&=
quot; in perlsub for details, including issues with tied arrays and hashes.=
<br></div><div>so<br></div><div>$| =3D 1;</div><div>if ( $dont_flush eq &#3=
9;true&#39; ) {</div><div>=C2=A0 local $| =3D 0;</div><div>=C2=A0 # do unfl=
ushed work\</div><div>}=C2=A0=C2=A0=C2=A0 # if dont_flush</div><div># $| is=
 back to its value global value</div><div><br></div><div>so the &quot;if&qu=
ot; curlies create a sub lexical context.=C2=A0 If it&#39;s a whole script =
or the main context, local won&#39;t make a difference. The idea is to not =
mess w/ somebody else&#39;s setting of the same var - it doesn&#39;t have t=
o be a Perl built-in, <br></div><div>our $is_one =3D 1;</div><div>if ( $don=
t_flush eq &#39;true&#39; ) {</div><div>=C2=A0 local $is_one =3D 0;</div><d=
iv>=C2=A0=C2=A0 # do something<br></div>}=C2=A0=C2=A0=C2=A0 # if dont_flush=
<div></div><div># $is_one is &quot;1&quot; here.</div><div><br></div><div>b=
ut for that (see perldoc above) you&#39;d use &quot;my&quot; and &quot;our&=
quot;<br></div><div>=C2=A0my $is_one =3D 0;</div><div>=C2=A0# do something<=
br></div></div><div class=3D"gmail_extra"><br><div class=3D"gmail_quote">On=
 Tue, May 15, 2018 at 11:34 AM, Rick T <span dir=3D"ltr">&lt;<a href=3D"mai=
lto:perl@reason.net" target=3D"_blank">perl@reason.net</a>&gt;</span> wrote=
:<br><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-le=
ft:1px #ccc solid;padding-left:1ex">I=E2=80=99m very much a novice =E2=80=
=94 familiar with Learning Perl but finding Intermediate Perl an uphill slo=
g. <br>
<br>
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 li=
ne after it is what I replaced it with after seeing Perl Best Practices (PB=
P) 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 woul=
d not be recommended.<br>
<br>
I vaguely understand that local restricts the auto flushing, though I=E2=80=
=99m not sure how. Does local go out of scope when the TT is called? More b=
roadly, do I actually need local? Do I actually need auto flushing? The aut=
hors of PTT and PBP assumed that persons reading their books would be fairl=
y accomplished programmers =E2=80=94 but I=E2=80=99m not. So can you educat=
e me? <br>
<br>
#=C2=A0 $| =3D 1; <br>
=C2=A0 =C2=A0 local $| =3D 1; # autoflush output<br>
<br>
=C2=A0 =C2=A0 print &quot;Content-Type: text/html\n\n&quot;;<br>
=C2=A0 =C2=A0 my $tt =3D Template-&gt;new(=C2=A0 =C2=A0<br>
ect.<br>
<br>
Many thanks!<br>
Rick Triplett<br>
<span class=3D"HOEnZb"><font color=3D"#888888">-- <br>
To unsubscribe, e-mail: <a href=3D"mailto:beginners-unsubscribe@perl.org">b=
eginners-unsubscribe@perl.org</a><br>
For additional commands, e-mail: <a href=3D"mailto:beginners-help@perl.org"=
>beginners-help@perl.org</a><br>
<a href=3D"http://learn.perl.org/" rel=3D"noreferrer" target=3D"_blank">htt=
p://learn.perl.org/</a><br>
<br>
<br>
</font></span></blockquote></div><br><br clear=3D"all"><br>-- <br><div clas=
s=3D"gmail_signature" data-smartmail=3D"gmail_signature"><br>a<br><br>Andy =
Bach,<br><a href=3D"mailto:afbach@gmail.com" target=3D"_blank">afbach@gmail=
..com</a><br>608 658-1890 cell<br>608 261-5738 wk</div>
</div>

--0000000000005509ab056c4194eb--
0
afbach
5/15/2018 5:03:20 PM
Many thanks for the very helpful comments from Andrew and Andy. For the =
first time, I have a clear idea of what local and auto flushing are =
doing for me. I know I=E2=80=99d get educated here!=
0
perl
5/19/2018 4:25:24 PM
Reply: