In Perl, it\'s often nice to be able to assign an object, but specify some fall-back value if the variable being assigned from is \'undef\'. For instance:
my $x
Most of the solutions relying on if statements don't work for the case where x is 0 or negative.
>>> x = 0
>>> y = 2
>>> a = x or y
>>> a
2
>>>
If you knew the name of the variable ahead of time you could look for like so:
if 'x' in dir():
a = x
except:
a =y
However that solution seems kind of sloppy to me. I believe the best method is to use a try except block like so:
try:
a = x
else:
a = y