Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

A readline + -e oddity: readline opens files even with -e --- need "readline STDIN" to read from console

by cxw (Scribe)
on Nov 30, 2020 at 15:32 UTC ( [id://11124400]=perlmeditation: print w/replies, xml ) Need Help??

(This is my first meditation. I am posting in hopes it will help someone down the line! I can't find another node about this, but that doesn't mean there isn't one :) .)

I needed to run a program and then wait for the user to hit Enter. I tried this:

$ perl -e 'system(@ARGV); readline' echo foo foo Can't open echo: No such file or directory at -e line 1. Can't open foo: No such file or directory at -e line 1.
Not what I expected!

After all, perlrun says that:

If -e is given, Perl will not look for a filename in the argument list.

What I was missing is that readline does look at the argument list. Readline:

reads ...from *ARGV if EXPR is not provided

So what I needed was to expressly read from STDIN to get my keypress:

# vvvvv $ perl -e 'system(@ARGV); readline STDIN' echo foo foo (<=== waited here for me to press Enter)

  • Comment on A readline + -e oddity: readline opens files even with -e --- need "readline STDIN" to read from console
  • Select or Download Code

Replies are listed 'Best First'.
Re: A readline + -e oddity: readline opens files even with -e --- need "readline STDIN" to read from console
by choroba (Cardinal) on Nov 30, 2020 at 16:47 UTC
    Another possibility would be to remove the consumed arguments from @ARGV:
    perl -e 'system splice @ARGV; readline' echo foo

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re: A readline + -e oddity: readline opens files even with -e --- need "readline STDIN" to read from console
by haukex (Archbishop) on Nov 30, 2020 at 16:50 UTC

    You might be interested in what Perl::Critic::Policy::InputOutput::ProhibitExplicitStdin and the corresponding part of PBP has to say about it. Also, you might want to consider checking via IO::Interactive to see if the terminal is actually interactive.

    use warnings; use strict; use IO::Interactive 'is_interactive'; use Term::ReadLine; print "Hello, World!\n"; if ( is_interactive() ) { print "Press Enter to continue...\n"; Term::ReadLine->new("")->readline(""); } else { warn "Noninteractive, continuing...\n" }

    This will prompt the user to press enter if connected to a terminal, but if the script is used in a non-interactive environment (simple example: perl script.pl | cat), it won't. See also other prompting alternatives.

Re: A readline + -e oddity: readline opens files even with -e --- need "readline STDIN" to read from console (perl -MO=Deparse)
by Anonymous Monk on Dec 01, 2020 at 00:27 UTC
Re: A readline + -e oddity: readline opens files even with -e --- need "readline STDIN" to read from console
by Anonymous Monk on Nov 30, 2020 at 19:43 UTC
    Why is this a "Meditation?" Sure looks to me like someone is "Seeking Perl Wisdom" ...
      Because I already fixed my problem ;) .

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://11124400]
Approved by LanX
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-04-25 08:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found