in reply to Read files not subdirectories

To get a list of all the .txt file in the current directory, use:

my @files = <*.txt>;

To process every line of those files one at a time, use:

{ local @ARGV = <*.txt>; while( <> ) { // $_ contains the lines of the files one at a time; for each +file in turn } }

Update:To display file(line no): line for every .txt file in the current directory use:

{ local @ARGV = <*.txt>; while( <> ) { chomp; print $ARGV, '(', $., '):', $_; } }

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". I'm with torvalds on this
In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked

Replies are listed 'Best First'.
Re^2: Read files not subdirectories
by Discipulus (Canon) on Jan 30, 2015 at 13:10 UTC
    wow. never realized all such things at glance! this is DynamitePerl (maybe it is worth a new section.. ;=) ).
    Anyway, with the excuse of explaining your code to the OP, i'll try to describe it further with the hope to retain it in memory..
    my @files = <*.txt>;
    This is almost easy, but not completely. The clue is in a big section of perlop concerning IO Operators:
    while speaking about diamond operator <>

    If what's within the angle brackets is neither a filehandle nor a simple scalar variable containing a filehandle name, typeglob, or typeglob reference, it is interpreted as a filename pattern to be globbed, and either a list of filenames or the next filename in the list is returned, depending on context.

    So,if no all glitter is gold, not all diamonds contain filehandle..
    The keyword in the above sentece result to be glob and in his own manpage is stated:
    This is the internal function implementing the <*.c> operator, but you can use it directly. If EXPR is omitted, $_ is used.

    This lead to the second code:
    #To process every line of those files one at a time, use: { local @ARGV = <*.txt>; while( <> ) { # $_ contains the lines of the files one at a time; for each f +ile in turn # To display file(line no): line for every .txt file in the cu +rrent directory use: chomp; print $ARGV, '(', $., '):', $_; } }
    Docs state:
    The null filehandle <> is special: it can be used to emulate the behavior of sed and awk, and any other Unix filter program that takes a list of filenames, doing the same to each line of input from all of them.

    ..

    Here's how it works: the first time <> is evaluated, the @ARGV array is checked,..

    ..read also the missing part..

    You can modify @ARGV before the first <> as long as the array ends up containing the list of filenames you really want.


    So the first diamond is used on a (localized!) @ARGV to fill it whit a list of files created via glob and then the second diamond is that special one.

    HtH
    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.
Re^2: Read files not subdirectories
by wrkrbeee (Scribe) on Jan 29, 2015 at 21:48 UTC
    Thanks BrowserUk! Stupid question: your suggestions replace my WHILE NEXT statements, right? If so, then how do your suggestions affect my OPEN and FOREACH statements? Sorry for the low level questions.