in reply to Help with cause of "Modification of a read-only value attempted at ..."
The first thing you should do is show what print Dumper $cfg; says inside your list_hosts function.
If in doubt, print it out!