| View previous topic :: View next topic |
| Author |
Message |
drax
Joined: Thu Dec 01, 2005 12:37 pm Posts: 6
|
Posted: Fri May 21, 2010 2:23 pm Post subject: Piping to perl script |
|
|
Hi,
I'm a perl newbie but it seems like it's now part of my job...
How can I capture the name of a file being piped to a perl script ?
I have this;
cat myfile | complicated.pl
It's a logfile analyser that's built around a while(<STDIN>) loop and outputs a digest. I need to be able to tell what myfile is within the script. I've tried variations on ARGV but I think they'd only work if the script was called like this;
complicated.pl myfile
Which I can't do, basically coz I'd have to rewrite the script and my level of perl knowledge is self evident from this post
It's also got to handle logrotation, I was thinking it might be easier to amend the logrotate script or is there a way I can capture a HUP within complicated.pl get it to close its currently open files and start crunching the new log file ? |
|
| Back to top |
|
 |
nelz Moderator

Joined: Mon Apr 04, 2005 12:52 pm Posts: 8036 Location: Warrington, UK
|
Posted: Fri May 21, 2010 10:58 pm Post subject: |
|
|
You can't do this reliably because your script is not getting the data from the file, it is getting it from standard input. It doesn't even know of the existence of the file.
As for the log rotation, why reinvent the wheel? Call the standard logrotate program to do this job. _________________ "Insanity: doing the same thing over and over again and expecting different results." (Albert Einstein) |
|
| Back to top |
|
 |
tweetiepooh

Joined: Fri Jun 30, 2006 5:02 pm Posts: 9
|
Posted: Tue Jun 22, 2010 12:54 pm Post subject: |
|
|
It's not that hard to change your script.
Simply put an open() call in. I've put the file name in a variable as later you can pass the file name in and just change how you set $filename.
| Code: | my $filename = "myfile";
open my $filehandle, '<', $filename or die "Can't open $filename\n"; |
Then change <STDIN> to use file handle
| Code: | while (my $line = <$filehandle>) {
# do something with $line
}
close $filehandle;
|
|
|
| Back to top |
|
 |
geek73666
Joined: Wed Mar 30, 2011 7:37 am Posts: 4
|
Posted: Wed Mar 30, 2011 7:59 am Post subject: |
|
|
You could always do something like cat file | complicated.pl file, and use $ARGV[0] as the file name.  |
|
| Back to top |
|
 |
| View previous topic :: View next topic |
|