in reply to Re: Why do I get uninitialized value in substitution (s///) in my array?
in thread Why do I get uninitialized value in substitution (s///) in my array?

ig,
That worked the 2nd time that I tried it. I'm not sure why it didn't work the first time.
Thanks!
  • Comment on Re^2: Why do I get uninitialized value in substitution (s///) in my array?

Replies are listed 'Best First'.
Re^3: Why do I get uninitialized value in substitution (s///) in my array?
by ig (Vicar) on Aug 13, 2009 at 17:55 UTC

    I would probably write your script something like the following, avoiding the problem of how many passwords to generate.

    use strict; use warnings; my $users = "new.users"; open(my $in, '<', $users) or die "Cannot open $users: $!"; my $tmp = $users . '.tmp'; open(my $out, '>', $tmp) or die "Cannot open $tmp: $!"; foreach (<$in>) { if(/^userPassword: xyz456$/) { my $pw = generatePassword(10); s/^userPassword: xyz456/userPassword: $pw/; } print $out "$_"; } close($out); close($in); unlink($users) or die "unlink($users): $!"; rename($tmp, $users) or die "rename($tmp, $users): $!"; exit(0); sub generatePassword { my $length = shift; my $password = ""; my $possible = 'abcdefghijkmnpqrstuvwxyz23456789ABCDEFGHJKLMNPQRST +UVWXYZ'; while (length($password) < $length) { $password .= substr($possible, (int(rand(length($possible)))), 1); } return $password }