title: Pass regex into perl subroutine
body: <h2>The Situation</h2>
  
  <p>I am in the process of creating a simple template file that will aid in creating future scripts for doing various tasks via command line on *nix systems. As part of this, I might like to ask the user to input data which needs to validated against a regular expression that is supplied in the source code.</p>
  
  <h2>The Issue</h2>
  
  <p>Errors are begin generated when I attempt to run the Perl code via command line. I am attempting to pass a regular expression into the repeat subroutine and I'm not sure how to exactly do this. I am aware that I can execute a string using <a href="http://perldoc.perl.org/functions/eval.html" rel="nofollow noreferrer">eval</a>, however this is something that I would like to avoid due to convention.</p>
  
  <h3>The errors:</h3>
  
  <pre><code>Use of uninitialized value $_ in pattern match (m//) at scripts/template line 40.
  Use of uninitialized value $resp in concatenation (.) or string at scripts/template line 37.
  </code></pre>
  
  <h3>The code:</h3>
  
  <pre><code>#!/usr/bin/env perl
  
  use strict;
  use warnings;
  use Cwd;
  use Term::ANSIColor;
  use Data::Dumper;
  
  my $log = "template.log";
  my $task = "template";
  my $cwd = getcwd();
  my $logPath = $cwd . "/". $log;
  
  print ucfirst($task) . " utility starting...\n";
  system("cd ~/Desktop");
  system("touch " . $log);
  &amp;writeLog("Test");
  
  sub writeLog {
      open(my $fh, '&gt;&gt;', $logPath) or die "Could not open file '$log' $!";
      print $fh $_[0] . localtime() . "\n";
      close $fh;
      return 1;
  }
  
  sub ask {
      my $question = $_[0];
      my $input = $_[1];
      my $resp = &lt;&gt;;
      chomp($resp);
  }
  
  sub repeat {
      my $pat = $_[0];
      my $resp = $_[1];
      print $pat . "\n";
      print $resp . "\n";
  }
  
  &amp;repeat(/foo|bar/i, "y");
  </code></pre>
  
  <h3>What I have tried:</h3>
  
  <p>Based on these sources:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/3652527/match-regex-and-assign-results-in-single-line-of-code">Match regex and assign results in single line of code</a></li>
  <li><a href="https://stackoverflow.com/questions/6278143/how-to-assign-result-of-a-regex-match-to-a-new-variable-in-a-single-line">How to assign result of a regex match to a new variable, in a single line?</a></li>
  </ul>
  
  <hr>
  
  <pre><code>sub repeat {
      my $pat =~ $_[0];
      my $resp = $_[1];
      if($pat !~ $resp) {
          print "foo\n";
      } else {
          print "bar\n";
      }
  }
  </code></pre>
  
  <p>Any help is appreciated!</p>