On Tue, Nov 6, 2018 at 1:24 PM Joseph Brenner doom...@gmail.com
<http://mailto:doom...@gmail.com> wrote:

How do you write code that detects whether it's running under the repl?
>
The variable $*PROGRAM (or $*PROGRAM-NAME; they interpolate identically so
I’m giving them raw to say below)—with the caveat that it isn’t perfect:

% perl6
To exit type 'exit' or '^D'
> say $*PROGRAM, " ", $*PROGRAM-NAME
"interactive".IO interactive
>
% perl6 -e 'say $*PROGRAM, " ", $*PROGRAM-NAME'
"-e".IO -e
% newscript /tmp/interactive perl6
% cat >> /tmp/interactive

say $*PROGRAM, " ", $*PROGRAM-NAME;
% cat /tmp/interactive
#!/usr/bin/env perl6

say $*PROGRAM, " ", $*PROGRAM-NAME;
% /tmp/interactive
"/tmp/interactive".IO /tmp/interactive
% cd /tmp
% ./interactive
"./interactive".IO ./interactive
% path+=.
% interactive
"interactive".IO interactive

But since $*PROGRAM is automatically an uninstantiated IO::Path object, you
could add a check $*PROGRAM.e which will usually work unless you

   1. have . in your PATH (not smart), and
   2. happen to be in the same directory as a file named *interactive*

(FYI, newscript is just a self-created shell function that creates a script
with a shebang line and makes it executable, and the path+=. is (I think?)
a Zsh-ism for path=$path . that doesn’t add . if it’s already a member.)

Reply via email to