perldoc Tk::Menu and perldoc Tk::Bind get you most of the way to the above.
$w is simply the container in which the event happened being shifted off the callback parameters, which you are then running the XEvent method on to get the location of the right-click.
The argument that gets shifted to $w is an inherent argument, due to object oriented nature of Tk.