One possible method would be:
#!/usr/bin/perl -w
use strict;
my @list = qw(Charles Cody Gina);
# pass the array to the sub to avoid value reuse.
&action(@list);
sub action {
my @list = @_;
for my $i(@list) {
# tests the return value of decide and only prints
# if it is true (1)
print "$i\n" if decide($i);
}
}
sub decide {
my $i = shift;
# returns 1 if it is a match otherwise nothing
# is returned.
return 1 if $i =~ m/^C/;
# you could add an explicit return 0 if you want
# return 0;
}
If your conditional is more complex then the example you
would want to most likely put the
if before the
print and
then use an
else condition for a failed decide.