Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Passing regex to a subroutine

by ll001 (Initiate)
on Oct 12, 2012 at 19:09 UTC ( #998774=perlquestion: print w/replies, xml ) Need Help??
ll001 has asked for the wisdom of the Perl Monks concerning the following question:

Happy Friday Monks! I was wondering how I would pass a regex like this:


as an argument to a subroutine which executes a command on the (one and only) path matching that regex? Sample below hopefully illustrates what I'm trying to do.

#!/usr/bin/perl -w use strict; my $regexStr = qr{[0-9]+}; my $suff = ".dn"; listThisFolder($regexStr); sub listThisFolder { my ($regexStr) = @_; my $getDir = ("c:\\sh\\"); # I know that the folder exists, but I don't know what the table # suffix or chunk suffix would be ( or table-001.t +n, etc). # $ls would hold the contents of the folder which expanded from th +e regex my $ls = `dir $getDir$regexStr$suff`; print "$ls\n"; #return($ls); }

Replies are listed 'Best First'.
Re: Passing regex to a subroutine
by aitap (Deacon) on Oct 12, 2012 at 19:33 UTC
    Did you mean my @list = grep { $_ =~ $regexStr } glob "$getDir/*";? (untested)
    Sorry if my advice was wrong.

      That's what was missing. I had created a chicken and egg situation for myself because I had not expanded it correctly. Thanks for the help. -J

        Actually your problem was that you passed the regex to a command (`dir`) that didn't understand it. Doing it all in Perl makes much more sense of course, but if the suffix is significant you'll still have to integrate it into the regex.

Re: Passing regex to a subroutine
by Lotus1 (Curate) on Oct 12, 2012 at 23:13 UTC

    This is not exactly what you asked for but another option is to use File::Find.

    #!perl; use warnings; use strict; use File::Find; my $path = 'c:\b\perlmonks\arrays'; my $regexStr = qr{([0-9])\1[0-9]*([0-9])\2}; my @files; find( sub { !-d and /$regexStr/ and print "<$_>\n" and push @files, $F +ile::Find::name }, $path ); print "1- $_\n" foreach glob("$path\\*"); print "2- $_\n" foreach @files; __END__ Output: <> <997882.txt> 1- c:\b\perlmonks\arrays\ 1- c:\b\perlmonks\arrays\ 1- c:\b\perlmonks\arrays\ 1- c:\b\perlmonks\arrays\ 1- c:\b\perlmonks\arrays\ 1- c:\b\perlmonks\arrays\ 1- c:\b\perlmonks\arrays\997882.txt 1- c:\b\perlmonks\arrays\fff2.txt 1- c:\b\perlmonks\arrays\junk 2- c:\b\perlmonks\arrays/ 2- c:\b\perlmonks\arrays/997882.txt
Re: Passing regex to a subroutine
by AnomalousMonk (Chancellor) on Oct 13, 2012 at 10:33 UTC

    Another debugging approach is to ask what all those strings look like when they're concatenated together inside the function.

    >perl -wMstrict -le "my $regexStr = qr{[0-9]+}; my $suff = '.dn'; my $getDir = 'c:\sh\\'; ;; my $concat = qq{$getDir$regexStr$suff}; print qq{for debug: '$concat'}; " for debug: 'c:\sh\(?^:[0-9]+).dn'

    The first thing that catches the eye is that  (?^:[0-9]+) stuff in the middle of everything. What's that about? Clearly, it derives from the regex, but how does it relate to the specification of a path and file name? (Of course, one quickly realizes it is the stringization of the  Regexp regex object – not a string at all – that  qr// produces.)

    Seeing the inappropriate nature of the data being fed to the Windoze  dir command is the first step toward thinking more clearly about what one ought to be doing.

Re: Passing regex to a subroutine
by kcott (Chancellor) on Oct 13, 2012 at 10:43 UTC

    G'day ll001,

    I see that ++aitap has identified your problem.

    Seeing your usage of $regexStr, I was wondering if you were aware that qr{...} does not return a string per se. It actually returns a Regexp object. When stringified, this is overloaded to produce something you may not have expected.

    $ perl -e 'my $regexStr = qr{[0-9]+}; print "$regexStr\n"; print ref($regexStr), "\n"; ' (?^:[0-9]+) Regexp

    See perlop - Regexp Quote-Like Operators for details.

    -- Ken

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://998774]
Approved by toolic
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2017-07-28 05:39 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (424 votes). Check out past polls.