Assuming the user exists on the system:
#! /bin/perl
print "Enter a username: ";
chomp ($user = <>);
system "stty -echo";
print "Enter a password: ";
chomp($entry1 = <>);
print "\n";
### 5 - Verify entered password
CHECK: {
print "Re-enter the password: ";
chomp($entry2 = <>);
print "\n";
if ($entry1 ne $entry2){print "Passwords do not match!\n"; redo CHECK;}
}
system "stty echo";
### 6 - Encrypt the password/Create shadow entry
@SaltVals = ( 'A' .. 'Z', 'a' .. 'z', 0 .. 9, '.', '/' );
$salt = join("", map {$SaltVals[rand(scalar(@SaltVals))]} (1,2));
$Encr_Pass = crypt($entry1, $salt);
$days_epoch = int(time/(60*60*24));
$Shadow_Entry="${user}:${Encr_Pass}:${days_epoch}::::::";
### 7 - Create a 'HERE' shell script
open(SCRIPT, ">/tmp/.addpasswd.sh"

|| die "Can't open script file in /tmp: $!\n";
print SCRIPT <<HERE;
#! /bin/sh
# NOTE: this does not check if the shadow file is open.
# check for lock file or passwd command in use if desired
{
if egrep "^$user" /etc/passwd > /dev/null 2>&1; then
ed /etc/shadow << EOF > /dev/null 2>&1
/^$user:/
i
$Shadow_Entry
.
/^$user:/
d
w
q
EOF
if [ $? = 0 ] ; then
echo "Updated password for $user."
else
echo "Update password failed for $user."
fi
else
echo "The account $user does not exist."
fi
} > /tmp/.addpasswd.out
HERE
close(SCRIPT);
# Execute/remove the shell script and show results
system('sh /tmp/.addpasswd.sh');
sleep(2);
`rm /tmp/.addpasswd.sh`;
exec('cat /tmp/.addpasswd.out');