in reply to Inappropriate ioctl for device

You have several precedence problems where you're using || where you really want or instead. Your print line for instance parses like this:

print(SAVE ($output || die(((q[can't write to save ] . $!) . "\n"))));

Which means that if $output evaluates to a false value then you're going to see a spurious die with who knows what error message from who knows what transient value that got left in $!.

Update: OK, maybe s/several/a couple/; looking closer you have precedence problems with the print line and when you call open.