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

I have a sub that is run on everytime a script gets a message, the scipt runs and stays running, when it gets something comming in, it runs the sub. The sub just includes another file. my issues is that if I change the file, i have to restart the master program. I want to be able to change the file and have it load the file every time the sub is run. Does anyone know how to do this?

Replies are listed 'Best First'.
Re: sub Issue include
by Zaxo (Archbishop) on Dec 17, 2004 at 21:03 UTC

    The do builtin will take care of that, my $data = do $filepath; $data will hold the last perl value in the file. that is commonly an anonymous hashref within {} braces. The output of Data::Dumper is suitable and often used for this.

    After Compline,
    Zaxo

Re: sub Issue include
by Old_Gray_Bear (Bishop) on Dec 17, 2004 at 20:53 UTC
    How does the subroutine "just includes another file"? Are you using  use or  require? Or are you opening the file and reading it each time the subroutine is called?

    ----
    I Go Back to Sleep, Now.

    OGB

      require
        You can use do to execute the code from a file; unlike require it doesn't check to see if the file has already been loaded.

        (More generally, one could extract the logic from Apache::StatINC to create an importable require() function that did the same check-for-changes trick to reload the code only when needed; I'm surprised this hasn't already been done.)

Re: sub Issue include
by Stevie-O (Friar) on Dec 17, 2004 at 20:56 UTC
    What do you mean, 'your sub just includes another file'?

    Do you mean it reads a file into memory, and you want to reread that file every time in the subroutine? If so, that's easy. Just move the code that reads the file into your sub.

    Or do you mean it loads a *module*? If that's the case, you need to do a little more work. You need to tell Perl to forget that it's loaded the module, and then re-use the module;

    sub foo { delete $INC{'My/Cool/Module.pm'); no warnings 'redefined'; require My::Cool::Module; # etc. etc. etc. }

    And if it's neither of those things, I have no idea what you're trying to do.

    --Stevie-O
    $"=$,,$_=q>|\p4<6 8p<M/_|<('=> .q>.<4-KI<l|2$<6%s!<qn#F<>;$, .=pack'N*',"@{[unpack'C*',$_] }"for split/</;$_=$,,y[A-Z a-z] {}cd;print lc
      No its just another perl script.
        You're being exceedingly brief here.

        Please paste some actual code so that we may understand just what it is you're doing.

        --Stevie-O
        $"=$,,$_=q>|\p4<6 8p<M/_|<('=> .q>.<4-KI<l|2$<6%s!<qn#F<>;$, .=pack'N*',"@{[unpack'C*',$_] }"for split/</;$_=$,,y[A-Z a-z] {}cd;print lc