Pass regex into perl subroutine

djthoms

The Situation

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.

The Issue

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 eval, however this is something that I would like to avoid due to convention.

The errors:

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.

The 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);
&writeLog("Test");

sub writeLog {
    open(my $fh, '>>', $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 = <>;
    chomp($resp);
}

sub repeat {
    my $pat = $_[0];
    my $resp = $_[1];
    print $pat . "\n";
    print $resp . "\n";
}

&repeat(/foo|bar/i, "y");

What I have tried:

Based on these sources:


sub repeat {
    my $pat =~ $_[0];
    my $resp = $_[1];
    if($pat !~ $resp) {
        print "foo\n";
    } else {
        print "bar\n";
    }
}

Any help is appreciated!

alex

To create a regular expression for use later, we use qr//:

my $regexp = qr/^Perl$/;

This compiles the regular expression for use later. If there's a problem with your regular expression, you'll hear about it immediately. To use this pre-compiled regular expression you can use any of the following:

# See if we have a match
$string =~ $regexp;

# A simple substitution
$string =~ s/$regexp/Camel/;

# Comparing against $_
/$regexp/;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Pass high number of arguments to Perl subroutine or?

From Dev

How to pass file names to a subroutine in perl?

From Dev

Pass an array and a hash to a subroutine for multiple threads in Perl

From Dev

how to pass a variable to a subroutine to send email in perl

From Dev

Perl pass hash reference to Subroutine from Foreach loop (Array of Hash)

From Dev

Perl pass hash reference to Subroutine from Foreach loop (Array of Hash)

From Dev

In Perl, how can I pass a variable as an argument to a subroutine?

From Dev

Perl subroutine arguments

From Dev

local $_ in a Perl subroutine

From Dev

Check if subroutine exists in Perl

From Dev

Perl - subroutine to translate variables

From Dev

perl foreach loop in subroutine

From Dev

Subroutine Prototype in PERL

From Dev

Perl - passing an array to subroutine

From Dev

Perl: prototype in anonymous subroutine

From Dev

Modify subroutine parameter (Perl)

From Dev

Perl subroutine timeout

From Dev

local $_ in a Perl subroutine

From Dev

Pass a class subroutine as a callback

From Dev

Matching regex on parameter in subroutine

From Dev

Matching regex on parameter in subroutine

From Dev

Perl subroutine arguments like a hash

From Dev

How to override a subroutine such as `length` in Perl?

From Dev

Perl subroutine returning unexpected value

From Dev

Running perl subroutine in parallel with Coro

From Dev

Variable change inside Perl subroutine

From Dev

Subroutine name beginning with underscore in Perl

From Dev

Returning file handle in Perl subroutine

From Dev

Writing subroutine to log file in perl