>>>> Is there a way to discover the filename of the current file being read when
>>>> using the angle brackets (e.g. while (<>) {} )?
>>>
[stuff cut out]
>
> Bottom line, <> won't help you!
>
> I can think of ways of piping through ARGV but, really, you don't
> want to go there - believe me.
>
> Some points to help you.
>
> - Perl is very good at guessing what you want, but I don't
> think reading from a pipe is ever one of its options
>
> - Piping is often a bad way of doing something that Perl
> will do much more easily
>
> - There are many, like me, who will enjoy plumbing the depths
> of their knowledge in a particular direction which may be of
> no use to you
>
> - It's far better to describe your problem to the list than to
> ask how to implement your solution. Think "My wife hates me!"
> as opposed to "Where do I get Vallium?"
Good advice, Rob. Here goes my attempt at "My wife hates me!". =)
I'm writing a program ("showme") similar to grep, where the user sends data
and a pattern (possibly spanning multiple lines), and the script tells what
file the pattern is found in, and what it found. Very simple.
The problem is reading either out of a pipe or out of files. The following
is what I'd like it to output:
Assuming:
mytestfile: blah1\ndog\ncat\nblah2\n
mytestfile2: blah3\ncow\n
% showme
showme <pattern> [filenames]
- displays pattern matches in files
% showme 'h\d' mytestfile*
mytestfile: h1
mytestfile: h2
mytestfile2: h3
% echo "blah4" | showme
h4
Here's what I have so far:
**************************************
#!/usr/bin/perl -w
#
# showme, v0.1
#
sub process_file {
if (!-f || ($_ =~ /^\./)) { return; }
open(FILE, $_) || die("Couldn't open $_: $!\n");
undef $/;
$content = <FILE>;
close(FILE);
while ($content =~ /($showme)/mgi) { print "$_: $1\n"; }
}
if (!defined($showme = shift)) { print "showme <pattern> [filenames]\n" .
"- displays pattern matches in files\n"; exit(0); }
if (@ARGV) {
foreach (@ARGV) { process_file; }
}
else {
$content = <>;
while ($content =~ /($showme)/mgi) { print "$_: $1\n"; }
}
exit (0);
**************************************
... but it never hits the last else (obviously) because it's caught by the
first line. I don't know how to test to see if there's any data in the
pipe, so I'm not sure how to even start.
Any ideas?
TIA.
- Bryan
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]