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

Pretty much what I had in mind, but to avoid race conditions due to parallel starts, I'd rather write to a temporary file and rename it at the end.

Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
Wikisyntax for the Monastery

  • Comment on Re^2: Is it possible to modify __DATA__ via the DATA file handle or otherwise?

Replies are listed 'Best First'.
Re^3: Is it possible to modify __DATA__ via the DATA file handle or otherwise?
by haukex (Archbishop) on Feb 10, 2018 at 12:44 UTC
    I'd rather write to a temporary file and rename it at the end

    Good thing I wrote a module for that ;-)

    use warnings; use strict; use File::Replace 'replace2'; my ($infh,$outfh) = replace2(__FILE__); while (<$infh>) { last if /^__DATA__$/; print $outfh $_; } print $outfh "__DATA__\n"; print $outfh "New stuff! ".gmtime." UTC\n"; close $infh; close $outfh;
      Still using the tell of the DATA filehandle is the far better approach.

      There is no guaranty to correctly match __DATA__ without having a full Perl parser.

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)
      Wikisyntax for the Monastery

        Still using the tell of the DATA filehandle is the far better approach.

        You are correct, and thank you for the inspiration :-) I just released v0.08 of File::Replace with a new copy method:

        use warnings; use strict; use File::Replace 0.08 'replace3'; my ($infh,$outfh,$repl) = replace3(__FILE__,':raw'); $repl->copy(tell(DATA)); print $outfh "New stuff! ".gmtime." UTC\n"; $repl->finish; __DATA__
Re^3: Is it possible to modify __DATA__ via the DATA file handle or otherwise?
by ikegami (Patriarch) on Feb 12, 2018 at 22:51 UTC

    Or use a locking mechanism (e.g. flock).