in reply to How to add input history

#!/usr/bin/perl # http://perlmonks.org/?node_id=1122584 use Term::ReadLine; use strict; (my $term = new Term::ReadLine 'dierolling')->ornaments(",,,"); print "Input the dice and modifier you want to roll like this: 2d9+100 +. To quit, press Ctrl-C.\n"; while (1) { defined(my $dice = $term->readline('? ')) or last; my @arr = split (/d/, $dice); my @arr2 = split (/\+/, $arr[1]); for my $i (1 .. $arr[0]){ print ((int(rand($arr2[0] - 1)) + 1 + $arr2[1]), "\n");}}

This is only during a single run. If you want history to be persistent from one execution to the next, please say so.

Replies are listed 'Best First'.
Re^2: How to add input history
by stevieb (Canon) on Apr 06, 2015 at 20:29 UTC

    Your code doesn't seem to do anything when I use the up-arrow...

    steve@spek ~ $ ./test.pl Input the dice and modifier you want to roll like this: 2d9+100 +. To quit, press Ctrl-C. ? 2d9+100 104 105 ? ^[[A ?

    -stevieb

      See the perldoc page DESCRIPTION for Term::ReadLine . You will need one of the Term::ReadLine::* packages.

      I have Term::ReadLine::Perl

      That's probably why it worked for me.

        That very well could be, but if I don't have a module installed, I would expect an error message.

        If I, someone who has used Perl since ~2001, doesn't get an error message from a missing package when I'm testing for valid output, how could one expect OP or a new user to know what to expect from code without any knowledge?

        Perhaps something in the upstream package should be told to say something. I'm not bitching, I'm just stating a point. There's something missing here :)

        -stevieb

        ps. I copied and pasted your exact code.

        pps. I suppose what I'm saying here is that if you are going to respond to a thread to help someone, either ensure the module you recommend errors out if dependencies aren't found, or put a clause in your post that says "may not work if X, Y, or Z isn't installed". Think about it... if you were a new user, would you immediately think to look up the perldoc info for a module if all you were looking for was code and you didn't see a responsible piece about "read perldoc for X module"?