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

Hi, 1.I have a run a command "p4 delete" on all the files present in a text file "deletefiles.txt",Is there a way this can be achieved in on line in perl or in a simpler way?

2.Also,i have a command file where I want to add the above command?can a perl statement be added in a command file? Thanks

Replies are listed 'Best First'.
Re: how to write a perl one liner?
by JavaFan (Canon) on Mar 08, 2011 at 10:34 UTC
    Having never used perforce before, 10 seconds of googling gives me: p4 -x deletefiles.txt delete.
Re: how to write a perl one liner?
by ikegami (Patriarch) on Mar 08, 2011 at 08:20 UTC

    How about a bash one liner?

    for f in `cat deletefiles.txt` ; do p4 delete "$f" ; done
    Or if p4 can take multiple args file names,
    p4 delete `cat deletefiles.txt`

    Note: Both assume the file names have no shell characters in them (e.g. spaces, "*"). I'm sure that restriction can be eliminated pretty simply, though. Anyone care to enlighten?

    Update: And the answer is:

    xargs -d \n p4 delete < deletefiles.txt

    I think it needs a -n 1 in there if p4 delete can only take one file name at a time.

    See xargs.

      Don't forget

      while read f; do p4 delete "$f"; done < deletefiles.txt

        I looked at it, but abandoned it when I remembered xargs. It has the same problem as my earlier solutions unless you unset env var IFS. (I think prefixing the whole thing with "IFS=" will do the trick.)

        Additionally, I think xargs will report non-zero exit codes, whereas the bash loop solutions don't without more code.

      Seems like there is no -d option.I get the following output

      OUTPUT:- Z:>xargs -d \n p4 delete < deletefiles.txt xargs: illegal option -- d Usage: xargs [-0prtx] [-e[eof-str]] [-i[replace-str]] [-l[max-lines]] [-n max-args] [-s max-chars] [-P max-procs] [--null] [--eof[=eo +f-str]] [--replace[=replace-str]] [--max-lines[=max-lines]] [--interact +ive] [--max-chars=max-chars] [--verbose] [--exit] [--max-procs=max-p +rocs] [--max-args=max-args] [--no-run-if-empty] [command [initial-arg +uments]]
        No, seems like there's no -d option in yours.

        Also, that doesn't look like a bash prompt. If you're going to switch shell, you'll also have to convert the command for the new shell. Does your shell convert «\n» to a newline? Although I think -d would also accept the two character string as equivalent.

Re: how to write a perl one liner?
by ikegami (Patriarch) on Mar 08, 2011 at 08:42 UTC

    The simplest Perl solution I can think of is

    perl -MIPC::System::Simple=system -nle'system p4 => delete => $_' dele +tefiles.txt

    IPC::System::Simple does error checking for you.

    Update: Shorter, but the error messages are a little weird:

    perl -Mautodie=system -nle'system p4 => delete => $_' deletefiles.txt
Re: how to write a perl one liner?
by planetscape (Chancellor) on Mar 09, 2011 at 00:54 UTC
Re: how to write a perl one liner?
by happy.barney (Friar) on Mar 08, 2011 at 08:15 UTC
    local @ARGV = ('deletefiles.txt'); unlink or warn "$_: $!\n" for <>;

      Where are we running "p4 delete" in the above code

        so replace unlink with your code :-) or better, follow ikegami's advice
        in the ,