develooper Front page | perl.perl5.porters | Postings from October 2021

Re: Pre-RFC: ${^OUTPUT_HANDLE}

Thread Previous | Thread Next
From:
Dan Book
Date:
October 22, 2021 15:03
Subject:
Re: Pre-RFC: ${^OUTPUT_HANDLE}
Message ID:
CABMkAVUy7GHaxagLXmLL4NnfVoiSKPWRkuqL-ROEWYSe4BVoJQ@mail.gmail.com
On Fri, Oct 22, 2021 at 10:50 AM Paul "LeoNerd" Evans <
leonerd@leonerd.org.uk> wrote:

> Many items of perl interpreter state are exposed as scalar variables.
> This allows you to temporarily change its value, perhaps by a `local`
> or `dynamically` assignment:
>
>   {
>     local $, = " and ";
>     print @values;
>   }
>
> The "currently selected output filehandle" as used by print/printf is
> not so exposed; instead requiring the (honestly-bizarrely named)
> select() dance:
>
>   {
>     my $oldh = select $newh;
>     print "Things";   # this goes to $newh;
>
>     select $oldh; # restore the old one.
>   }
>
> This is bad because in the event of an exception, return, goto or
> loopex, the previous value is not restored because that second select()
> statement is never reached. This can't happen with the `local` or
> `dynamically` assignments, because those are reliably restored even in
> those events.
>
> I propose the addition of a new perlvar; perhaps named
>
>   {
>     local ${^OUTPUT_HANDLE} = $newh;
>     print "Things";
>   }
>
> to embody the same concept as the previous block involving two select()
> expressions, but without that unreliability.
>
>
> Thoughts?
>
> Or maybe this is sufficiently small and uncontentious as to not bother
> with writing an RFC and just go ahead and implement it?
>

A similar mechanism is available for the similarly fatally-global chdir
function via the CPAN module https://metacpan.org/pod/File::chdir, and an
equivalent for select would be quite useful for the case where you want to
set it for a dynamic scope (affecting sub calls within the scope etc).
Perhaps worth prototyping on CPAN as that should be rather simple.

-Dan

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About