Hello noviceuser. Your code looks OK and will work, although there are a couple of things you might want to look at if interested.
open(MY_LOG, $log) or die "Could not open file 'log' $!";
Bareword filehandles like MY_LOG are apparently being phased out of the language. Instead you can use a lexical variable to hold the filehandle. eg:
open(my $lfh, $log) or die "Could not open file 'log' $!";
It's good that you have the conditional die statement, but it doesn't contain the filename which is another good piece of info to provide to the user on error, so I would include that too:
open(my $lfh, $log) or die "Could not open log file '$log': $!";
Presumably you are only opening the file for reading, so it's worth popping that in there as well:
open(my $lfh, '<', $log) or die "Could not open log file '$log': $!";
And finally, you don't need the brackets - they do no harm but you might find that the fewer punctuation characters there are on a line the easier it is to read.
open my $lfh, '<', $log or die "Could not open log file '$log': $!";
The other slightly odd thing is that you are chomping the lines read. Why is that? If the only action is to match against a pattern and conditionally print then it makes more sense not to chomp in this particular case.
These are all just "bonus" points but if you are just starting out with Perl now is a great time to try to reinforce good habits and best practices that will serve you well for a long time to come.
🦛
In reply to Re: print complete line along side line number, when a pattern is matched in log file
by hippo
in thread print complete line along side line number, when a pattern is matched in log file
by noviceuser
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |