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.


In reply to Re: regexp s/mode quicktest by oko1
in thread regexp s/mode quicktest by halfcountplus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.