detecting running under the repl

How do you write code that detects whether it's running under the repl?
0
doomvox
11/6/2018 6:24:10 PM
perl.perl6.users 1103 articles. 0 followers. Follow

1 Replies
13 Views

Similar Articles

[PageSpeed] 27

--0000000000009db456057a051ab9
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

On Tue, Nov 6, 2018 at 1:24 PM Joseph Brenner doomvox@gmail.com
<http://mailto:doomvox@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=E2=80=99m giving them raw to say below)=E2=80=94with the caveat that it i=
sn=E2=80=99t 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+=3D.
% 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+=3D. is (I think?=
)
a Zsh-ism for path=3D$path . that doesn=E2=80=99t add . if it=E2=80=99s alr=
eady a member.)

--0000000000009db456057a051ab9
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div class=3D"markdown-here-wrapper" style=3D""><p style=
=3D"margin:0px 0px 1.2em!important">On Tue, Nov 6, 2018 at 1:24 PM Joseph B=
renner <a href=3D"http://mailto:doomvox@gmail.com">doomvox@gmail.com</a> wr=
ote:</p>
<p style=3D"margin:0px 0px 1.2em!important"></p><div class=3D"markdown-here=
-exclude"><p></p><blockquote class=3D"gmail_quote" style=3D"margin:0px 0px =
0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">How do y=
ou write code that detects whether it&#39;s running under the repl?<br></bl=
ockquote><p></p></div><p style=3D"margin:0px 0px 1.2em!important"></p>
<p style=3D"margin:0px 0px 1.2em!important">The variable <code style=3D"fon=
t-size:0.85em;font-family:&quot;Fira Code&quot;,LiberationMono,Consolas,Inc=
onsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:=
pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(253,246,227=
);border-radius:3px;display:inline">$*PROGRAM</code> (or <code style=3D"fon=
t-size:0.85em;font-family:&quot;Fira Code&quot;,LiberationMono,Consolas,Inc=
onsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:=
pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(253,246,227=
);border-radius:3px;display:inline">$*PROGRAM-NAME</code>; they interpolate=
 identically so I=E2=80=99m giving them raw to <code style=3D"font-size:0.8=
5em;font-family:&quot;Fira Code&quot;,LiberationMono,Consolas,Inconsolata,C=
ourier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;b=
order:1px solid rgb(234,234,234);background-color:rgb(253,246,227);border-r=
adius:3px;display:inline">say</code> below)=E2=80=94with the caveat that it=
 isn=E2=80=99t perfect:</p>
<pre style=3D"font-size:0.85em;font-family:&quot;Fira Code&quot;,Liberation=
Mono,Consolas,Inconsolata,Courier,monospace;font-size:1em;line-height:1.2em=
;overflow:auto;margin:1.2em 0px"><code style=3D"font-size:0.85em;font-famil=
y:&quot;Fira Code&quot;,LiberationMono,Consolas,Inconsolata,Courier,monospa=
ce;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px soli=
d rgb(234,234,234);background-color:rgb(253,246,227);border-radius:3px;disp=
lay:inline;white-space:pre;overflow:auto;border-radius:3px;border:1px solid=
 rgb(204,204,204);padding:0.5em 0.7em;display:block!important">% perl6
To exit type &#39;exit&#39; or &#39;^D&#39;
&gt; say $*PROGRAM, &quot; &quot;, $*PROGRAM-NAME
&quot;interactive&quot;.IO interactive
&gt;=20
% perl6 -e &#39;say $*PROGRAM, &quot; &quot;, $*PROGRAM-NAME&#39;
&quot;-e&quot;.IO -e
% newscript /tmp/interactive perl6            =20
% cat &gt;&gt; /tmp/interactive        =20

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

say $*PROGRAM, &quot; &quot;, $*PROGRAM-NAME;
% /tmp/interactive
&quot;/tmp/interactive&quot;.IO /tmp/interactive
% cd /tmp
% ./interactive
&quot;./interactive&quot;.IO ./interactive
% path+=3D.=20
% interactive
&quot;interactive&quot;.IO interactive
</code></pre><p style=3D"margin:0px 0px 1.2em!important">But since <code st=
yle=3D"font-size:0.85em;font-family:&quot;Fira Code&quot;,LiberationMono,Co=
nsolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;wh=
ite-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(2=
53,246,227);border-radius:3px;display:inline">$*PROGRAM</code> is automatic=
ally an uninstantiated <code style=3D"font-size:0.85em;font-family:&quot;Fi=
ra Code&quot;,LiberationMono,Consolas,Inconsolata,Courier,monospace;margin:=
0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,=
234,234);background-color:rgb(253,246,227);border-radius:3px;display:inline=
">IO::Path</code> object, you could add a check <code style=3D"font-size:0.=
85em;font-family:&quot;Fira Code&quot;,LiberationMono,Consolas,Inconsolata,=
Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;=
border:1px solid rgb(234,234,234);background-color:rgb(253,246,227);border-=
radius:3px;display:inline">$*PROGRAM.e</code> which will usually work unles=
s you</p>
<ol style=3D"margin:1.2em 0px;padding-left:2em">
<li style=3D"margin:0.5em 0px">have <code style=3D"font-size:0.85em;font-fa=
mily:&quot;Fira Code&quot;,LiberationMono,Consolas,Inconsolata,Courier,mono=
space;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px s=
olid rgb(234,234,234);background-color:rgb(253,246,227);border-radius:3px;d=
isplay:inline">.</code> in your <code style=3D"font-size:0.85em;font-family=
:&quot;Fira Code&quot;,LiberationMono,Consolas,Inconsolata,Courier,monospac=
e;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid=
 rgb(234,234,234);background-color:rgb(253,246,227);border-radius:3px;displ=
ay:inline">PATH</code> (not smart), and</li>
<li style=3D"margin:0.5em 0px">happen to be in the same directory as a file=
 named <em>interactive</em></li>
</ol>
<p style=3D"margin:0px 0px 1.2em!important">(FYI, <code style=3D"font-size:=
0.85em;font-family:&quot;Fira Code&quot;,LiberationMono,Consolas,Inconsolat=
a,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wra=
p;border:1px solid rgb(234,234,234);background-color:rgb(253,246,227);borde=
r-radius:3px;display:inline">newscript</code> is just a self-created shell =
function that creates a script with a shebang line and makes it executable,=
 and the <code style=3D"font-size:0.85em;font-family:&quot;Fira Code&quot;,=
LiberationMono,Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;pad=
ding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);backg=
round-color:rgb(253,246,227);border-radius:3px;display:inline">path+=3D.</c=
ode> is (I think?) a Zsh-ism for <code style=3D"font-size:0.85em;font-famil=
y:&quot;Fira Code&quot;,LiberationMono,Consolas,Inconsolata,Courier,monospa=
ce;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px soli=
d rgb(234,234,234);background-color:rgb(253,246,227);border-radius:3px;disp=
lay:inline">path=3D$path .</code> that doesn=E2=80=99t add <code style=3D"f=
ont-size:0.85em;font-family:&quot;Fira Code&quot;,LiberationMono,Consolas,I=
nconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-spac=
e:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(253,246,2=
27);border-radius:3px;display:inline">.</code> if it=E2=80=99s already a me=
mber.)</p>
<div title=3D"MDH:PGRpdiBkaXI9Imx0ciI+PGRpdiBkaXI9Imx0ciI+PGRpdiBjbGFzcz0iZ=
21haWxfZGVmYXVsdCIg
c3R5bGU9ImZvbnQtZmFtaWx5OiBtb25vc3BhY2UsIG1vbm9zcGFjZTsiPjxicj48L2Rpdj48YnI=
+
PGRpdiBjbGFzcz0iZ21haWxfcXVvdGUiPjxkaXYgZGlyPSJsdHIiPk9uIFR1ZSwgTm92IDYsIDI=
w
MTggYXQgMToyNCBQTSBKb3NlcGggQnJlbm5lciAmbHQ7ZG9vbXZveEBnbWFpbC5jb20mZ3Q7IHd=
y
b3RlOjxicj48L2Rpdj48YmxvY2txdW90ZSBjbGFzcz0iZ21haWxfcXVvdGUiIHN0eWxlPSJtYXJ=
n
aW46IDBweCAwcHggMHB4IDAuOGV4OyBib3JkZXItbGVmdDogMXB4IHNvbGlkIHJnYigyMDQsIDI=
w
NCwgMjA0KTsgcGFkZGluZy1sZWZ0OiAxZXg7Ij5Ib3cgZG8geW91IHdyaXRlIGNvZGUgdGhhdCB=
k
ZXRlY3RzIHdoZXRoZXIgaXQncyBydW5uaW5nIHVuZGVyIHRoZSByZXBsPzxicj48L2Jsb2NrcXV=
v
dGU+PGRpdj48YnI+PC9kaXY+PGRpdiBjbGFzcz0iZ21haWxfZGVmYXVsdCIgc3R5bGU9ImZvbnQ=
t
ZmFtaWx5OiBtb25vc3BhY2UsIG1vbm9zcGFjZTsiPuKAi1RoZSB2YXJpYWJsZSBgJCpQUk9HUkF=
N
YCAob3IgYCQqUFJPR1JBTS1OQU1FYDsgdGhleSBpbnRlcnBvbGF0ZSBpZGVudGljYWxseSBzbyB=
J
J20gZ2l2aW5nIHRoZW0gcmF3IHRvIGBzYXlgIGJlbG93KeKAlHdpdGggdGhlIGNhdmVhdCB0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tYXJ0KSwgYW5kPC9zcGFuPjw=
v
ZGl2PjxkaXYgY2xhc3M9ImdtYWlsX2RlZmF1bHQiPjxzcGFuIHN0eWxlPSJmb250LWZhbWlseTo=
g
bW9ub3NwYWNlLCBtb25vc3BhY2U7Ij4yLiBoYXBwZW4gdG8gYmUgaW4gdGhlIHNhbWUgZGlyZWN=
0
b3J5IGFzIGEgZmlsZSBuYW1lZCBfaW50ZXJhY3RpdmVfPC9zcGFuPjwvZGl2PjxkaXYgY2xhc3M=
9
ImdtYWlsX2RlZmF1bHQiPjxicj48L2Rpdj48ZGl2IGNsYXNzPSJnbWFpbF9kZWZhdWx0Ij48c3B=
h
biBzdHlsZT0iZm9udC1mYW1pbHk6IG1vbm9zcGFjZSwgbW9ub3NwYWNlOyI+KEZZSSwgYG5ld3N=
j
cmlwdGAgaXMganVzdCBhIHNlbGYtY3JlYXRlZCBzaGVsbCBmdW5jdGlvbiB0aGF0IGNyZWF0ZXM=
g
YSBzY3JpcHQgd2l0aCBhIHNoZWJhbmcgbGluZSBhbmQgbWFrZXMgaXQgZXhlY3V0YWJsZSwgYW5=
k
IHRoZSBgcGF0aCs9LmAgaXMgKEkgdGhpbms/KSBhIFpzaC1pc20gZm9yIGBwYXRoPSRwYXRoIC5=
g
IHRoYXQgZG9lc24ndCBhZGQgYC5gIGlmIGl0J3MgYWxyZWFkeSBhIG1lbWJlci4pPC9zcGFuPjw=
v
ZGl2PjwvZGl2PjwvZGl2PjwvZGl2Pg=3D=3D" style=3D"height:0;width:0;max-height:=
0;max-width:0;overflow:hidden;font-size:0em;padding:0;margin:0"></div></div=
></div>

--0000000000009db456057a051ab9--
0
trey
11/6/2018 8:46:52 PM
Reply: