Skip to content

fix: Term::ReadLine - add missing methods, honour PERL_RL, fix ReadKey prototype#658

Merged
fglock merged 1 commit intomasterfrom
feature/fix-term-readline
May 2, 2026
Merged

fix: Term::ReadLine - add missing methods, honour PERL_RL, fix ReadKey prototype#658
fglock merged 1 commit intomasterfrom
feature/fix-term-readline

Conversation

@fglock
Copy link
Copy Markdown
Owner

@fglock fglock commented May 2, 2026

Summary

Fixes six bugs found while investigating ./jcpan -t Term::ReadLine::Perl.

Bug 1 — PERL_RL env var ignored

TermReadLine.initialize() pre-set %INC{"Term/ReadLine.pm"} at JVM startup, so scripts setting BEGIN{ $ENV{PERL_RL}='Perl' } could never load the CPAN implementation. Fixed by:

  • Adding src/main/perl/lib/Term/ReadLine.pm (the standard dist stub) so Term::ReadLine::Stub, ::TermCap, and ::Tk packages are available to the CPAN module.
  • Switching to super("Term::ReadLine", false) so %INC is not pre-set and the Perl file runs normally on use Term::ReadLine.
  • In newReadLine(), reading %ENV{PERL_RL} at call-time; when it says Perl, requiring Term::ReadLine::Perl and delegating via InheritanceResolver/RuntimeCode.apply (with fallback to the Java impl on failure).

Bug 2 — GetHistory / SetHistory methods not registered

Features hash claimed getHistory=1 and setHistory=1 but neither GetHistory nor SetHistory was callable. Added getHistoryList / setHistoryList static methods and registered them. Also registered AddHistory (camelCase alias).

Bug 3 — ornaments method missing

$term->ornaments(0) died. Added a no-op stub; added ornaments=1 to the features hash.

Bug 4 — newTTY method missing

$term->newTTY(\*IN, \*OUT) died. Added a no-op stub (stream rewiring is not possible via Perl globs on the JVM); added newTTY=1 to the features hash.

Bug 5 — readline ignores preput argument; feature absent

$term->readline($prompt, "exit") silently dropped the second argument. Now displays it as [text] in the prompt and adopts it when the user presses Enter on an empty line. Added preput=1 to the features hash.

Bonus fixes

  • appName was read from args[0] (the class name) instead of args[1]; Attribs->{appname} now reflects the name passed to new().
  • Term::ReadKey::ReadKey prototype changed from ";$" to ";$$" so readline.pm's two-arg call ReadKey(0, $fh) no longer fails with "Too many arguments" at compile time.

Test plan

  • make passes (all unit tests green)
  • ./jperl /tmp/test_readline_full.pl — all six method tests pass, features complete, PERL_RL=Perl returns Term::ReadLine::Perl
  • AUTOMATED_TESTING=1 ./jperl test.pl — emits 1..0 # skip (correct automated-test behaviour)
  • echo "exit" | ./jperl test.pl --no-print — runs interactively, exits cleanly on EOF

Generated with Devin

…y prototype

Five distinct bugs in the Term::ReadLine / Term::ReadKey Java backends:

Bug 1 – PERL_RL env var ignored
  TermReadLine used super("Term::ReadLine") which set %INC at JVM startup,
  preventing the companion Perl file from running and $ENV{PERL_RL}='Perl'
  from ever taking effect. Fix:
  - Add src/main/perl/lib/Term/ReadLine.pm (copy of the dist stub) so
    Term::ReadLine::Stub, ::TermCap and ::Tk packages are available.
  - Switch to super("Term::ReadLine", false) so %INC is not pre-set and
    the Perl file is executed normally on `use Term::ReadLine`.
  - In newReadLine(), check %ENV{PERL_RL}; when it contains "Perl", require
    Term::ReadLine::Perl from ~/.perlonjava/lib and delegate via
    InheritanceResolver / RuntimeCode.apply (with try/catch fallback).

Bug 2 – GetHistory / SetHistory never registered
  Features hash advertised getHistory/setHistory=1, but no Perl-callable
  methods existed. Added static getHistoryList / setHistoryList + registered
  them as GetHistory / SetHistory.  Also registered AddHistory as a
  camelCase alias for addhistory.

Bug 3 – ornaments method missing
  Added a no-op ornaments() stub so $term->ornaments(0) doesn't die.
  Added ornaments=1 to the features hash.

Bug 4 – newTTY method missing
  Added a no-op newTTY() stub (JVM cannot rewire streams via Perl globs,
  but the method must exist). Added newTTY=1 to the features hash.

Bug 5 – readline ignores preput argument; feature absent
  readline($prompt, $preput) second arg was silently dropped.  Now the
  preput text is appended to the displayed prompt as "[text]", and an
  empty reply from the user adopts the preput value. Added preput=1 to
  the features hash.

Bonus fixes:
  - appName was read from args[0] (the class name) instead of args[1];
    Attribs->{appname} now reflects the name passed to new().
  - args[2]/[3] corrected for the optional IN/OUT filehandle slots.
  - Term::ReadKey::ReadKey prototype changed from ";$" to ";$$" so that
    readline.pm's two-arg call ReadKey(0, $fh) no longer fails with
    "Too many arguments" at compile time.

Generated with [Devin](https://cli.devin.ai/docs)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@fglock fglock merged commit f0cabc6 into master May 2, 2026
2 checks passed
@fglock fglock deleted the feature/fix-term-readline branch May 2, 2026 09:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant