in reply to Re^2: Problem with ReadKey
in thread Problem with ReadKey
open STDIN, "-"; is just reopening STDIN with the standard input, which is still redirected. I don't think that you can get at the terminal output that easy.
Just remove the redirection and use the backticks operator `find $HOME -name \*.txt` or File::Find inside your perl script to get at the filenames. No redirection, no problem
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Problem with ReadKey
by liverpole (Monsignor) on Nov 06, 2010 at 14:43 UTC | |
I agree with jethro that the best way to do this is to read the list of files in the program. You shouldn't have to specify STDIN in your Term::ReadKey calls, either. Also, consider putting "$| = 1" at the beginning of the program to flush STDOUT. Here's a small subroutine which will let you avoid using File::Find and just get the files ending in ".txt":
Of course this will require that you read each file individually, and somehow store their contents. If it matters which file the text came from, you could save the lines from each file in a separate array reference, and then return a reference to those array references (note my use of Data::Dumper, which is invaluable for visualizing your data!):
If you don't care which text came from which file, just throw it all in one big array:
In fact, in the last example, you could even combine the finding of files with the saving of each file's text, to create a single subroutine. I'm using a nifty trick here, which is to pass the array reference $a_text which is the aggregate of all text in each recursive call to read_textfile_lines; only at the top level is it undefined (but in that case, you initialize the array ref with: $a_text ||= [ ];):
Now you're ready to call some subtroutine process_text (or whatever), passing the ref to the array of all text $a_text. It will do something like:
As you see, I've left for you the fun of deciding what to print out in the subroutine print_some_random_text, as well as adding code for trapping relevant keys from the user in the subroutine process_text. Good luck! s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/ | [reply] [d/l] [select] |
by jimhenry (Acolyte) on Nov 07, 2010 at 00:47 UTC | |
(I'm setting ReadMode 3 near the beginning of the script, and setting ReadMode 0 in an END{} block.) The directory read function, with its helper want_file, looks like this: I originally had the recurse_dir making a local list of filenames, which it returns to its caller; but after seeing how long it takes to read my entire home directory with this (vs. the benchmarks mentioned in comments above using just a couple of directories with lots of text files), I added the code near the end there, which required making the recurse_dir function add files directly to the global @filenames. This seems to work very well now. Thanks for all your help. The complete script from which the above excerpts are taken is at http://jimhenry.conlang.org/scripts/textual-slideshow.zip. | [reply] [d/l] [select] |