Lawliet has asked for the wisdom of the Perl Monks concerning the following question:

I know there is a way to save the user's input in the perl file. For reasons not specified, no modules. I searched about saving to the __DATA__ portion, and this is what I scraped together.

if ($save == 1) { my $where = tell(DATA); open (CONF => '+<', $0); seek CONF => 0, 2; print CONF "Var1::$var1\nVar2::$var2\nVar3::$var3"; }

That simply appends the variables to the end of the file (as I expected it to). I tried seek CONF => $where, 1; but that yielded the same result.

So, after that length introduction, how can I save the user's input to the __DATA__ portion of my file?

Oh, and on a side note, If I compile (is that the correct verb for this situation) this into an executable, will the saving to __DATA__ still function correctly?

Also, please note that I do not tell at the end of the file. I am not sure if that has anything to do with it.

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

Replies are listed 'Best First'.
Re: Saving to __DATA__
by GrandFather (Saint) on Jul 14, 2008 at 04:41 UTC
    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

      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
      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
Re: Saving to __DATA__
by ysth (Canon) on Jul 14, 2008 at 05:47 UTC
    Surely there's some other way to meet your goals? Why must the data go into your source file?
    Oh, and on a side note, If I compile (is that the correct verb for this situation) this into an executable, will the saving to __DATA__ still function correctly?
    I wouldn't think so.
Re: Saving to __DATA__
by zentara (Cardinal) on Jul 14, 2008 at 12:48 UTC
    See Re: chooseDirectory set Default. You have to watch out for someone hitting control-c, etc. to make sure your data gets saved. Also, you need to read in your data somehow. Saving to data can be tricky, one mistake and you can wipe out your script. Inline::Files is probably the better way to go.

    I'm not really a human, but I play one on earth CandyGram for Mongo
Re: Saving to __DATA__
by apl (Monsignor) on Jul 14, 2008 at 11:49 UTC
    I wouldn't write the user input to DATA; I'd append (or over-write) a file you designate for the purpose.

    That way, when your program starts, it could check for the existence of the file, read in the contents and do whatever you need to do with it. It makes testing a little easier, as well as assisting in providing an audit trail if you have to research what exactly your program did.

Re: Saving to __DATA__
by massa (Hermit) on Jul 14, 2008 at 12:35 UTC
    I know there is a way to save the user's input in the perl file. For reasons not specified, no modules. I searched about saving to the __DATA__ portion, and this is what I scraped together.
    ...
    how can I save the user's input to the __DATA__ portion of my file?

    As a CPAN advocate, I'll tell you to use Inline::Files and not to reinvent the wheel;

    Oh, and on a side note, If I compile (is that the correct verb for this situation) this into an executable, will the saving to __DATA__ still function correctly?

    Supposing you are talking about using something like PAR, no. You would have to re-pack all your modules again after writing, and this would be kind of complicated.

    []s, HTH, Massa
Re: Saving to __DATA__
by Lawliet (Curate) on Jul 14, 2008 at 12:44 UTC

    Hmm, I suppose I will switch to using another file (due to the overwhelming evidence that that method is superior). Thanks for all your help everyone.

    Oh, and Massa, yes, I knew about Inline::Files but, as mentioned in my initial node, no modules. (Sometimes the wheel needs to be reinvented if the pre-existing one could not be used :P)

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