There are many ways to refactor that code to make it easier to maintain. If the work to be done is really as light weight as is indicated then I'd tend toward a light weight object approach:
use strict; use warnings; my $obj = bless {}; $obj->processLine ($_) while <DATA>; $obj->report (); sub processLine { my ($self, $line) = @_; return if $line !~ /^M/; chomp $line; my $filename = (split '/', $line)[-1]; return if ! defined $filename; $self->{files}{$filename} ||= $line; } sub report{ my ($self) = @_; print "People not found\n" if ! exists $self->{files}{'people.pdf' +}; print "animal not found\n" if ! exists $self->{files}{'animal.pdf' +}; print "MSI not found\n" if ! exists $self->{files}{'setup.msi'}; } __DATA__ M/people.pdf M/wiggles.pdf M/setup.msi
Prints:
animal not found
which scales nicely as you need to detect more or fewer file names, but could easily be changed to use a dispatch table in the 'report' section to handle more work for each found file.
In reply to Re: Perlish while loop
by GrandFather
in thread Perlish while loop
by Sun751
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |