While I don't doubt that your code works, I think you might have misunderstood me. To make your example like my problem, the package Foo would have a subroutine with the while(<main::DATA>), and the main script would have the __DATA__ token.
This is really too simplified though because Foo doesn't know what it's caller is ahead of time (it's not always main).