in reply to regexp s/mode quicktest
Rather than using individual scalars and temporary variables for each of the items, you should consider using a hash. In addition, it would be a good idea to provide a graceful exit out of the loop (e.g., typing 'exit' or 'quit' at any of the input lines should terminate it) and you should check for errors during the evaluation (which your double 'eval' swallows up.) Here's a quick cut at a revision:
#!/usr/bin/perl -w use strict; use Term::ReadLine; my $term = new Term::ReadLine 'regexp Smode'; my $OUT = $term -> OUT || \*STDOUT; my %p; while (1){ for my $key (qw/string replace with suffix/){ $p{$key} = $term->readline("\u$key: "); exit if $p{$key} =~ /^(?:quit|exit)$/i; } eval "\$p{string} =~ s/\$p{replace}/\$p{with}/$p{suffix}"; print $OUT $@ ? "ERROR: $@\n" : "$p{string}\n\n"; }
I've been using a somewhat similar gadget for years now - but I prefer actually writing out the regex, so I only read two parameters ('String' and 'Regex'). This also allows me to test 'tr's as well as 's///' operators. Thanks to your idea, I've just added 'Term::Readline' functionality to it, which makes it even more useful.
|
|---|