As haukex has already pointed out, it's probably best to avoid eval in this case as it could be manipulated to run arbitary Perl code in your example.

hippo has also made a valid suggestion about using the ternary if conditional to make the code easier to read.

Personally, I'd use both of these suggestions, add in the use of say rather than print when appropriate and move the display prompt and return input into a separate function, but that might be over-engineering this learning exercise.

Putting it all together gives the following:

use strict; use warnings; use 5.016; sub prompt_read { print shift; chomp( $_ = <STDIN> ); return $_; } say "RegEx Engine 1.0\n________________"; my $str = prompt_read("Gimme a string: "); my $pattern = prompt_read("Gimme a RegEx: "); say $str =~ /$pattern/ ? "Yes!" : "No!"; say "kthxbye";

In reply to Re: Idiomatic Perl? by mxb
in thread Idiomatic Perl? by thenextfart

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.