mascip has asked for the wisdom of the Perl Monks concerning the following question:

Hello :o)
I have read here that one could use ack to search all replace all occurrences of a word in the files of a directory, by doing

$ perl -i -p -e's/foo/bar/g' $(ack -f --php)

This seems very nice, but it doesn't work "as it is" under Windows. Would anybody know how to make it work ?

My aim is simple : having a "template" directory for my modules, that i could copy and modify easily each time i want to start a new module.

Thank you for suggestions =)

Replies are listed 'Best First'.
Re: "ack" and replace in a directory... in Windows?
by BrowserUk (Patriarch) on Feb 29, 2012 at 03:37 UTC

    Substitute the ack command for the dir command the following:

    for /f %i in ('dir /b *.pl') do @perl -i*.bak -pe"s/foo/bar/g" %i

    Note the requirement for specifying the extension with the -i switch.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

      It works perfectly on my computer, thank you!

        You already have perl good fellow, don't do the batch file twist, it'll hurt your brain (did mine)

Re: "ack" and replace in a directory... in Windows?
by petdance (Parson) on Feb 29, 2012 at 12:56 UTC
    Your command doesn't work because the $(....) construct is something only in the Unix shell. Windows doesn't have that.

    Also, if you're normally using a template to build modules from boilerplate, you may want to look at Module::Start or Dist::Zilla.

    xoxo,
    Andy

      I have just started to use Module::Starter::PBP, but there are a few details i like to change.
      So; i was thinking : if i just "ack and replace" a module created with Module::Starter::PBP, it should do.

      I'm not sure whether it works or not, nor whether it is stupid or not.

Re: "ack" and replace in a directory... in Windows?
by Anonymous Monk on Feb 29, 2012 at 02:19 UTC

      It tries, but fails due to

      Can't open foo.pm (and others) : Invalid argument.

      Thank you though :o)

        chomp @ARGV;

        Also, «-e '» should have been «-e "».

        - tye