sub foo { print($__KLUDGE::phase eq 'END' ? 'END' : 'not END', $/); } # Needs to be at the beginning of the program to be reliable. BEGIN { $__KLUDGE::phase = 'BEGIN'; } # Needs to be at the end of the program to be reliable. END { $__KLUDGE::phase = 'END'; } # ... END { foo(); } # ...