Lumy
Lumy

Reputation: 401

Use parameters as regex in Perl

I'm getting an regex expression as a parameter of my script. Simply like /stat.*\.log/.

I tried to use it with the builtin grep of Perl.

my @dots = readdir($dh);
@dots=grep($conf->{classifier_regx}, @dots);

where @dots is a full directory with files and sub folder. And $conf->{classifer_regx} is the regex giving in parameters.

Actually that returns me all the files and subfolders. Is it possible to grep on a value at runtime? How can I do that? Did I miss something? Man page?

Upvotes: 0

Views: 182

Answers (2)

Drill
Drill

Reputation: 547

If you are using a Bash script it is much easier, since you can use grep onside the script. However since you are using Perl you can do the following:

my @dots = readdir($dh);

$str = <STDIN>; //This will allow you to enter a string at runtime
chomp str;

foreach $i (@dots) { // Iterate through the array. If a value is found, print it
    if ($i eq $str) {
        print "$str";
    }
}

Upvotes: -2

m0skit0
m0skit0

Reputation: 25873

I think you're lacking // in your grep:

@dots = grep(/$conf->{classifier_regx}/, @dots);

If this still doesn't work, you can try with

my $value = $conf->{classifier_regx};
@dots = grep(/$value/, @dots);

Upvotes: 3

Related Questions