Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

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:

(/opt/backups/maindb[0-9]+.db/table[0-9]+.tn/chunk[0-9]+.cn/)

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 (table-129.tn 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); }

Comment on Passing regex to a subroutine
Select or Download Code
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 (Chaplain) 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.pl> <997882.txt> 1- c:\b\perlmonks\arrays\964788.pl 1- c:\b\perlmonks\arrays\966409.pl 1- c:\b\perlmonks\arrays\970295.pl 1- c:\b\perlmonks\arrays\997235.pl 1- c:\b\perlmonks\arrays\997829.pl 1- c:\b\perlmonks\arrays\997882.pl 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/997882.pl 2- c:\b\perlmonks\arrays/997882.txt
Re: Passing regex to a subroutine
by AnomalousMonk (Abbot) 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 (Abbot) 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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://998774]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (8)
As of 2014-10-01 21:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    What is your favourite meta-syntactic variable name?














    Results (39 votes), past polls