perli icon indicating copy to clipboard operation
perli copied to clipboard

Fails with pragma

Open ghost opened this issue 5 years ago • 8 comments

works as expected:

$ perl -e 'use bigint; print 255->to_hex'
ff

failure:

perli> use bigint
perli> 255->to_hex
Can't locate object method "to_hex" via package "255" (perhaps you forgot to
load "255"?).

ghost avatar Jan 26 '19 05:01 ghost

(I assume you meant as_hex.)

The man page states (emphasis added):

Pragmas such as use locale; only take effect for the line at hand; for pragmas to take effect globally, you must specify them on the command line when starting perli; e.g., perli -Mlocale.

Therefore, start perli with perli -Mbigint to apply the bigint pragma session-globally:

$ perli -Mbigint
perli> 255->as_hex
'0xff'

Alternatively, for a single input line:

# pragma bigint is only in effect for the line at hand.
perli> use bigint; 255->as_hex
'0xff'

mklement0 avatar Jan 26 '19 13:01 mklement0

no, i mean to_hex:

https://metacpan.org/pod/Math::BigInt#to_hex()

as to the rest of the response, hm, thats not ideal. as python handles it:

>>> import datetime
>>> datetime.date.today()
datetime.date(2019, 1, 26)

and Ruby:

irb(main):001:0> require 'date'
=> true
irb(main):002:0> Date.today
=> #<Date: 2019-01-26 ((2458510j,0s,0n),+0s,2299161j)>

ghost avatar Jan 26 '19 13:01 ghost

Yes, it's not ideal - I wish it worked the way you expected and the way Python and Ruby do it, but at least with the current implementation I couldn't make that happen (it's been quite a while since I've looked at it, though).

mklement0 avatar Jan 26 '19 14:01 mklement0

It looks like this works as expected with Devel::REPL:

$ re.pl
$ use bigint
$ 255->to_hex
ff

and Reply:

$ reply
0> use bigint
1> 255->to_hex
$res[0] = 'ff'
  • https://metacpan.org/pod/Devel::REPL
  • https://metacpan.org/pod/Reply

ghost avatar Jan 26 '19 15:01 ghost

Thanks, @cup. I'll reopen this and label it as an enhancement.

I've never looked at these other REPLs (as you can probably tell, I'm not really much of a Perl guy) - have you used them enough to know their relative strengths and how they compare to this project?

mklement0 avatar Jan 26 '19 17:01 mklement0

well maybe i am "doing it wrong" - but my primary use case is working from standard input. for example with python:

python3 -i -q <<eof
import datetime
datetime.date.today()
eof

Result:

>>> >>> datetime.date(2019, 1, 26)
>>>

"Reply" can sort of do this:

reply /dev/stdin <<eof
2 + 3
eof

but it prints a lot of extra garbage, plus it doesnt exit at EOF, notice carefully the 1> in the output:

0> do "\/dev\/stdin"
$res[0] = 5

1>

and Devel::REPL cant do standard input at all, and it doesnt exit either:

$ echo 2+3 | re.pl
$

$ echo 2+3 | re.pl -
$

$ echo 2+3 | re.pl /dev/stdin
$

ghost avatar Jan 26 '19 18:01 ghost

Yeah, perli's focus is definitely on interactive exploration, via commands typed one line at a time.

To get what you want with Perl, however, can't you just do the following?

perl # invoke Perl and make it wait for stdin input.

Then type your multi-line code and submit it with Ctrl-d You will need explicit output commands such as print, however.

mklement0 avatar Jan 26 '19 21:01 mklement0

i have another workaround. using REPL.pl:

#!/bin/perl
require $ARGV[0];
print "$ae\n";

You can use it with any input, as long as the input defines $ae as some point. For example:

REPL.pl /dev/stdin <<'eof'
use bigint;
$ae = 255->to_hex;
eof

Result:

ff

ghost avatar Jan 27 '19 05:01 ghost