in reply to Default variable $_ is not getting overwritten

Why should the inner "for" replace the list the outer "for" created? That's not what $_ contains, it only contains one line from the input.

But even moving the print "Current line is $_"; after the inner "for" still works, i.e. the whole line is printed, not just its last entry. That's because the "foreach" loop localizes its loop variable (see Foreach Loops).

BTW, one usually uses the diamond operator with while rather than for .

Update: See $_ getting clobbered by inner loop. for an example where the problem really happens.

($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,