For things like this, it's nice to be familiar with some of the more esoteric shortcuts perl offers. I typically do something like that with a one-liner, probably something like:
perl -i.old -lne 'if (/^[\s#]*Port/i) { print "Port 1234" unless $done
+++ } else { print }' sshd_config
Although given that the order of directives in the sshd_config file doesn't matter, that could just be
perl -i.old -lne 'print unless /^[\s#]*Port/i; END{print "Port 1234"}'
+ sshd_config
I don't see a whole lot of point in prompting for the port number, but if you want it separated out of the script you could use
PORT=1234 perl -i.old -lne 'print unless /^[\s#]*Port/i; END { print "
+Port $ENV{PORT}" }' sshd_config
or
perl -i.old -lne 'BEGIN { $sshport = pop }; print unless /^[\s#]*Port/
+i; END { print "Port $sshport" }' sshd_config 1234
All of these are untested, sorry.