in reply to Idiomatic Perl?

Code seems to work, but it is better in PERL way as below.

use strict; use warnings; print "RegEx Engine 1.0\n________________\n"; print "Gimme a string: "; my $str = <STDIN>; print "Gimme a RegEx: "; my $pattern = <STDIN>; my $answer = ($str =~ /$pattern/); if ($answer) { print "Yes!"; } else { print "No."; } print "\nkthxbye\n";

Code Change at line "my $answer = ($str =~ /$pattern/);" will look for the pattern in $pattern in the string $str and will result in yes or no.

Replies are listed 'Best First'.
Re^2: Idiomatic Perl?
by hippo (Archbishop) on Mar 20, 2018 at 16:34 UTC
    my $answer = ($str =~ /$pattern/); if ($answer) { print "Yes!"; } else { print "No."; }

    I think the ternary here would be even more idiomatic. You can replace those 6 lines with just one:

    print $str =~ /$pattern/ ? "Yes!" : "No.";