Re: 'safe' printing to the terminal

[email protected] (Michiel Beijen) Tue, 12 May 2026 14:02:21 +0000
Newsgroups perl.perl5.porters
Message-ID <Pf0xoDDuQdRI68q-TTuwbaVdROjnDn7kCpC3pTyt1XtkS_DoEY0I-aIDPXHwf6AfvRFELurzxAxmcWxby3nI05pPNP3p8LLnzp1rmttKgUo=@x14.nl>
Hi Ruud,

On Monday, May 11th, 2026 at 18:20, "Ruud H.G. van Tol" via perl5-porters <[email protected]> wrote:

> 
> On 2026-05-11 17:34, Michiel Beijen wrote:
>  > On Monday, May 11th, 2026 at 12:29, "Ruud H.G. van Tol" via
> perl5-porters <[email protected]> wrote:
>  >
>  >> printf has %q:
>  >>
>  >>    $  printf -v var "%q" "^[[4mtest^[[m"
>  >>    $  echo $var
>  >>    $  eval "echo $var"
>  >
>  > Yeah, that does what I want, but unfortunately this is not available
> in _perl_ printf, only in _bash_ printf.
>  >
>  > Still looking for advise on how to print 'untrusted' input to a TTY
> in a way that is sane!
> 
> This was about the shell's printf, not about Perl's printf.
> 
> What stops you from using it?

Well, I can't assume that all users of File::MimeInfo have bash installed; moreover I would think perl should have a solution for this of its own. I'm partially inclined to see if I can make a CPAN module that implements printf with %q ;-)

Also, I think maybe I'd have a go to create something that you could use as an IO layer?

I would like to add that the earlier suggestion for handling untrusted data in DIE and warnings is very true; ideally perl would not print escape characters from there to a terminal.

Last but not least, someone suggested:

> Have you considered B::perlstring() or B::cstring()? They are both really just to make strings human-readable. but B is core so it SHOULD be available. I do not know why these have not come up in the p5p discussion.

I think that's a good point and I might actually consider using that in the `mimetype` CLI to print the filename back to the terminal without causing issues; if anyone has second thoughts on that I'd like to hear it!

--
Michiel