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

This node falls below the community's minimum standard of quality and will not be displayed.

Replies are listed 'Best First'.
Re: How to do loop in Perl (-ne)
by tye (Sage) on Oct 08, 2003 at 17:32 UTC

    Although some appear to not understand the phrase "in Perl" in a node title, this type of loop is rather easy to do in Perl:

    listTemplates | perl -ne 'chomp; unlink($_) or warn "Could not delete +$_: $!\n"'
    (where "listTemplates" would be whatever existing commands you use to list the template file names).

    perldoc perlrun has information on the -n and -e command-line options for perl. perl -ne '...' causes the '...' code to be run for each line read from STDIN (or from the listed files if I'd listed any files), with $_ set to be that line.

    My solution assumes that the file names are listed one per line with no extra characters so chomp removes the newline from $_ so that it contains just a file name. If the file names are listed in some other way, then you'll need to do a little more work to extract the file names.

    unlink deletes files. or checks whether unlink was successful and, if not, runs the following code which tells you *why* unlink failed (because I included $! in the message) and on which file.

    You could also do something similar using the 'xargs' command, but it would probably be slower and harder to get it to handle extra text in the output (if there is any).

    So I think you were wise to seek a Perl solution for this problem. Next time, mention "Perl" in the text of the node, not just the title, as some of us are a bit inobservant and/or cranky, unfortunately.

    Update: Originally had "Can't" in my error message which interferes with the 'quoting' I used. I could have replaced it with "Can\'t", but chose to avoid contractions instead.

                    - tye
Re: How to do loop in Perl
by Abigail-II (Bishop) on Oct 08, 2003 at 13:07 UTC
    Do you actually have a Perl question, or are you just posting your problem on a random forum?

    Abigail