Friday, July 31, 2009

Parsing in data into a perl script file from an e-mail filter. How do I grab the data that is piped in?

I am trying to use an e-mail filter in Bluehost. I chose the option to pipe to a program and made a little testfile.pl What I want to do is grab the data from the Subject line, (that I chose as the parameter to filter the e-mails by). So in the testfile.pl, what do I do to get that data so as to manage it? I have for example:





open(IN,"unixcommand|") || die "Could not execute unixcommand\n";


while(%26lt;IN%26gt;){


command;


}


close IN;





Am I supposed to use this somehow? If so how might I go about it?





Thanks,





Brian

Parsing in data into a perl script file from an e-mail filter. How do I grab the data that is piped in?
What is the full name of the unixcommand file?





You need





open(IN,"%26lt;unixcommand.txt") || die "Could not execute


unixcommand\n";





This gives the full filename (needs full path) and also marks it as read-only by using %26lt;





For relative beginners you should probably start off by reading files this way:





open(IN,"%26lt;unixcommand.txt") || die "Could not execute


unixcommand\n";


while($line = %26lt;IN%26gt;){


chomp($line);


print "This line is : $line\n";


}


close (IN);





So this will go through the file line by line and store it in the variable $line which you can then manage.





If you want to grab data from the subject line, you may need to do a pattern match on the line, but you need to say what each line looks like by giving an example first.
Reply:Brian:


Your example shows us how to read a pipe in perl. Let's change it to a working example and see if that helps:





open(IN,"ps|") or die "Could not execute ps\n";


while(%26lt;IN%26gt;){


print;


}


close IN;





Assuming you are executing in an environment where you have a path to the ps command you do not need to give a fully qualified path to the ps in open. This perl script opens the ps command and reads its STDOUT storing each line in $_. It then prints $_. This perl script as it stands is the same as typing ps on the command line. Within the while loop, you could, of course, pattern match to identify any particular line, process each line in whatever way you'd like, etc.


So, if your Bluehost account gives you a command to retrieve a mail message, you could use that command in place of the ps command in the example and process it as you wish. You probably want to give the fully qualified path for the Bluehost command though. On my Mac ps lives in /b in/ps if you're using bash or ksh, just type "which command_name" to get the path.


Good Luck
Reply:@stuff = %26lt;IN%26gt;;


that command will slurp all the data in the file into the array @stuff.





foreach $line (@stuff)


{


print $line;


}





or just...


print @stuff





See where the Subject line is in the output. Then do a pattern match to retrieve the subject into a variable.


No comments:

Post a Comment