Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fails with pragma #3

Open
ghost opened this issue Jan 26, 2019 · 8 comments
Open

Fails with pragma #3

ghost opened this issue Jan 26, 2019 · 8 comments

Comments

@ghost
Copy link

ghost commented Jan 26, 2019

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"?).
@mklement0
Copy link
Owner

mklement0 commented Jan 26, 2019

(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'

@ghost
Copy link
Author

ghost commented Jan 26, 2019

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)>

@mklement0
Copy link
Owner

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).

@ghost
Copy link
Author

ghost commented Jan 26, 2019

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'

@mklement0
Copy link
Owner

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 mklement0 reopened this Jan 26, 2019
@ghost
Copy link
Author

ghost commented Jan 26, 2019

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
$

@mklement0
Copy link
Owner

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.

@ghost
Copy link
Author

ghost commented Jan 27, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant