http://www.perlmonks.org?node_id=1208851


in reply to Is it possible to modify __DATA__ via the DATA file handle or otherwise?

DATA is a plain, ordinary file handle to the source file, but it's open for reading (not writing). I don't know how to convert a handle opened for reading into a handle opened for writing (if it's even possible), but you can open a new handle to the current file, and use tell(DATA) to find the location of the start of the data.

die("Not safe for setuid") if ${^TAINT}; open(my $fh, ">>", __FILE__) or die("open: $!\n"); ( my $data_pos = tell(DATA) ) >= 0 or die("tell: $!\n"); truncate($fh, $data_pos) or die("truncate: $!\n"); say($fh "bar") or die("say: $!\n"); __DATA__ foo