Linux Format forums Forum Index Linux Format forums
Help, discussion, magazine feedback and more
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Piping to perl script

 
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming
View previous topic :: View next topic  
Author Message
drax



Joined: Thu Dec 01, 2005 12:37 pm
Posts: 6

PostPosted: Fri May 21, 2010 2:23 pm    Post subject: Piping to perl script Reply with quote

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 Smile

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
View user's profile Send private message
nelz
Site admin


Joined: Mon Apr 04, 2005 12:52 pm
Posts: 8457
Location: Warrington, UK

PostPosted: Fri May 21, 2010 10:58 pm    Post subject: Reply with quote

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
View user's profile Send private message
tweetiepooh



Joined: Fri Jun 30, 2006 5:02 pm
Posts: 10

PostPosted: Tue Jun 22, 2010 12:54 pm    Post subject: Reply with quote

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
View user's profile Send private message
geek73666



Joined: Wed Mar 30, 2011 7:37 am
Posts: 4

PostPosted: Wed Mar 30, 2011 7:59 am    Post subject: Reply with quote

You could always do something like cat file | complicated.pl file, and use $ARGV[0] as the file name. Smile
Back to top
View user's profile Send private message
View previous topic :: View next topic  
Display posts from previous:   
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Linux Format forums topic RSS feed 


Powered by phpBB © 2001, 2005 phpBB Group


Copyright 2011 Future Publishing, all rights reserved.


Web hosting by UKFast