in reply to Modification of a read-only value attempted

In each case, the variable, $sub, is aliased to what's in the parens on the foreach line. In the second case, that's a read-only value, not a variable. For more, have a look at Common Causes for "Modification of a read-only value attempted".