#
# This sub depends on the Fcntl.pm :flock constants to
# function properly.
#

sub isValidUser {

  my( $givenUser, $givenPassword, $file ) = @_;


  # Do not clobber open FH file handles
  local *FH;

  open FH, $file or die qq(Cannot open "$file": $!);
  flock FH, LOCK_EX if $^O ne 'MSWin32';

  while ( <FH> ) {
    my( $user, $password ) = ( split /\t/, $_ )[1, 2];

    return 1 if     $user     =~ m/^$givenUser$/
                &&  $password =~ m/^$givenPassword$/;
  }
  close FH;
  return;

}

Example 4: Renamed subroutine with better parameter-passing behavior.

Back to Article