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

Re: Pre-RFC: ${^OUTPUT_HANDLE}

Thread Previous
From:
Dan Book
Date:
November 9, 2021 17:12
Subject:
Re: Pre-RFC: ${^OUTPUT_HANDLE}
Message ID:
CABMkAVU-9QyxGvdQf6LmsDK=900W9jsQa2irJau7t+-u323b7A@mail.gmail.com
On Fri, Oct 22, 2021 at 11:02 AM Dan Book <grinnz@gmail.com> wrote:

> 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.
>

I was informed this exists already on CPAN:
https://metacpan.org/pod/Tie::Select

-Dan

Thread Previous


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