The first one-liner will not have the intended effect:
perl -pi -e 's,^\s+?$,,' file.txt
only works on lines that consist of a newline. For lines that contain whitespace followed by a newline, the newline is not removed because of the non-greedy quantifier. (Although you could run the one-liner twice... ;)
In the second one-liner:
perl -pi -e 's,^(\n|\s+)$,,g' file.txt
the alternation and the /g are unnecessary.
This is sufficient:
perl -pi -e 's,^\s+$,,' file.txt