Search code examples
perlack

Proper way to use Ack inside Perl scripts?


I've begun to use ack because it seems to be a nice way to implement unix rgrep in a Perl script, easily. Thought, I use it through a Perl back-quoted system command:

# Searching for 'use' something 
my $pattern = "^use ";
my $path = "./path/to/libs/";
#Only pm files
my $parameters = "--perl";

my @uses = `ack $parameters $pattern $path`;

I know ack is pure Perl, but it seems to be designed for a command line use, not like an API or a Perl module. Is there a way to use it from a Perl script, I mean a module that makes possible to use ack like a Perl function :

# My phantasm
use ack;

my @uses = ack($parameters $pattern $path);

... or another method, timtowtdi.


Solution

  • You can't.

    Maybe I should put "No user-serviceable parts inside" in the modules. It's really not meant for being used programatically. It really only uses modules for ease of maintainability for those working on it, and for ease of installing through the CPAN shell.

    The file finding part is pretty simple to do. It's just calls to an iterator from File::Next.

    Why do you want to call ack things from inside another Perl program? What's your use case?

    Maybe this would be better taken up on the ack-users mailing list over on Google Groups.