I would also suggest to pass the path to the file as an argument. You can always define a default as a constant in the top of your script where it is easy to find and change if needed. That's more maintainable than hardcoding a path in a subroutine
use constant DEFAULT_FILE => "/etc/filesystems"; my $file = shift || DEFAULT_FILE; # e.g. to take file from command lin +e or use the default find_pattern($file, $pattern); sub find_pattern { my ($file, $pattern) = @_; # your code here }
Another general advise would be to separate the test and the action. Ideally, a subroutine "find_pattern" should return true if it found it and false if it didn't. For example next time you might want to terminate a script rather than print to STDOUT:
... die "oh no, it's not there!" unless find_pattern($file, $pattern); sub find_pattern { my ($file, $pattern) = @_; return `grep $pattern $file`; }
In reply to Re: subroutine to search a pattern in a file
by tospo
in thread subroutine to search a pattern in a file
by dilip_val
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |