in reply to Saving to __DATA__

use strict; use warnings; my $dataStart = tell DATA; print while <DATA>; open my $clobber, '>', $0; seek $clobber, $dataStart, 0; print $clobber "Clobbered\n"; close $clobber; seek DATA, $dataStart, 0; print while <DATA>; __DATA__ Original contents

Prints:

Original contents Clobbered

which seems to be what you want. It's not something I'd want to do in production code however! Apart from anything else it may suffer somewhat from line end conversions that could make the $dataStart position rather dodgy.


Perl is environmentally friendly - it saves trees

Replies are listed 'Best First'.
Re^2: Saving to __DATA__
by Lawliet (Curate) on Jul 14, 2008 at 06:14 UTC

    That prints the expected, but then when I reload the file:

    Could not open the file adsf.pl using the Unicode (UTF-8) character co +ding.

    All is lost :(

    <(^.^-<) <(-^.^<) <(-^.^-)> (>^.^-)> (>-^.^)>

      I think you could fairly safely say that this particular solution to your problem is a "lost cause". If you tell us what your larger problem is we may be able to provide a better solution to it.


      Perl is environmentally friendly - it saves trees
Re^2: Saving to __DATA__
by Anonymous Monk on Jul 14, 2008 at 04:47 UTC
    not wise to mix up seek and <>

      Mixing <> and seek is fine.
      Mixing <> and sysseek is not.

      Trying to write what amounts to self modifying code is not wise in any case! It can be fun maybe, but it's not wise.


      Perl is environmentally friendly - it saves trees