hello
Aldebaran,
you can avoid the oneliner BEGIN block using the Maori-farewell like in perl -M'5; $counter=0' -nle ...
But you can avoid the counter at all:
>perl -nlE "say sprintf qq(caption%03s.txt),$count++" one.txt two.txt
+ three.txt
caption000.txt
caption001.txt
caption002.txt
PS you get the filename currently processed in $ARGV so you can play with it:
>perl -nlE "say sprintf q(caption%03s.txt),$1 if $ARGV =~ /.*([\d]+).*
+/" one01.txt one02.txt
caption001.txt
caption002.txt
PPS note that perl -n and perl -p both skip empty files!
ls -l
-rw-rw-r-- 1 io io 0 feb 2 13:28 one.txt
-rw-rw-r-- 1 io io 0 feb 2 13:28 three.txt
-rw-rw-r-- 1 io io 20 feb 2 13:37 two.txt
perl -nlE 'say "processed $ARGV" if eof' *.txt
processed two.txt
PPPS after choroba's nice below response I cant resist to update the title and to try it:
perl -MTie::Scalar -M"5;{package SN; use parent -norequire => 'Tie::S
+tdScalar';sub FETCH {print qq(Opening ${$_[0]}); ${$_[0]}}}; tie $ARG
+V, SN"
-nlE "say qq(\tprocessed $ARGV) if eof" 001.txt 002.txt 003.txt
Opening 001.txt
Opening 002.txt
Opening 002.txt
processed 002.txt
Opening 003.txt
L*
There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.