Perl syntax and my lack of knowledge.
New job requires me to learn perl. They've given me an assignment to write a little script so here is where i am so far.
Code:
#!/usr/local/bin/perl This is my second day working with perl so I have a long way to go. Like posted, this is what I have so far. Right now I'm having trouble trying to get it to output to the logfile (ARGV[1]) Any guidance or suggestions on methodology would be appreciated. |
Well I am no perl guru, but I have 2 suggestions:
1. I am not a fan of naming different types of data (in your example scalar and array) with the same name. I do follow that with $ or @ at the start it is kind of clear, but it can get confusing in large scripts to be forced to always look for these (IMHO) 2. Why not start simple and just do the single open for the output and write a single line to it (I am sure there will be millions of returns in your search engine on how to do this) |
There is a typo in OUTPUT, ">>ARGV[1]", should be OUTPUT, ">>$ARGV[1]"
Instead of looping through the patterns and the file lines, just use grep function like Code:
#!/usr/local/bin/perl Straight from example in: http://perldoc.perl.org/perlop.html#...Like-Operators Code:
my @patterns = (...); |
Well, I am not satisfied with my grep suggestion. There is no way to stop grep after a match is sucessful, so it continues to check matches through the @patterns/@compiled array even if it founds a match. Grep function does not provide boolean result after all its purpose is to change an array into another.
Just a basic for loop will do, and exit this loop if match is found Code:
while (<INPUT>) { |
For instance, foreach my $patterns (@patterns) ... You should not be using the same variable-name here.
Perl is all about various characters such as '$', '@', '%', which are quite powerful but which can have unexpected side-effects. It's actually a tiny language compared to monsters like PHP, because it relies heavily on external modules (use ...). Also: since you find yourself "in a Perl shop," be sure to ask your co-workers questions freely. Likewise your boss! Perl is a nice language but it has a substantial learning curve at first. Don't feel ashamed to ask "stupid" questions. (The only stupid question is the one you wouldn't ask.) Take a look at existing source-code. You should be surrounded by it now. And, while it's okay to ask for help on a forum like this, don't go here for substitutes to the assigned task of confronting the learning-curve yourself. That's what your new employer has tasked you to do ... and, they know that you can and will do it ... and you have the full sympathy and patient understanding of everyone. (There, and certainly here.) Perl, quirky though it is, has been called "the Swiss ArmyŽ Knife of pragmatic computer programming," and with good reason. Oh, yeah, one more thing: "meet Tim Toady," if you haven't yet. TMTOWTDI = There's More Than One Way To Do It.™ |
I rewrote it for a if / elsif to find the 3 conditions i was looking for. It works now. Just one regex away from putting it into production. Any suggestions on a perl regex to match a line like this?
Code:
From: "Name of Letter" <email@address.com> |
You could target the From at the start of the line with anchor and then store the data between the <> with grouping.
|
All times are GMT -5. The time now is 11:50 AM. |