But there is always a new line, so it should always be interpreted as true, which means the first case should work, not the second case. ...but that is not the case. The chomp is returning '1' and it's being interpreted as false.
Re^2: return value of chomp is false?
Replies are listed 'Best First'.
Ternary "?:" is the conditional operator, just as in C. It works much like an if-then-else. If the argument before the ? is true, the argument before the : is returned, otherwise the argument after the : is returned.
Coming from the STDIN handle, I agree that $_ will have a newline character. This means that chomp $_ will return 1. Since Perl treats 1 as a true value, the ternary operator will return the argument before the :.
In your first case, chomp $_ ?"":$_, the ternary operator returns an empty string.
In your second case, chomp $_ ?$_:"", the ternary operator returns $_.