perl -le'print +(-1,1)[rand 2]' # or as a sub sub toggle { (-1,1)[rand 2] }