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

Replies are listed 'Best First'.
Re: executing code in a file
by valdez (Monsignor) on Aug 24, 2002 at 21:45 UTC

    But you still don't know why it didn't work :)
    You were reading only the first line of the file, so the line with print statement wasn't executed. Here is the proposed change:

    local $/; eval <FILE>;

    Ciao, Valerio

Re: executing code in a file
by sauoq (Abbot) on Aug 24, 2002 at 21:39 UTC

    Or:

    do "/home/samn/cgi-bin/$page.rs";
    -sauoq
    "My two cents aren't worth a dime.";
    
Re: executing code in a file
by djantzen (Priest) on Aug 24, 2002 at 21:45 UTC

    eval can take either a block of code to trap runtime errors or a string to parse and sometimes execute at runtime. You're attempting to do the latter, but rather than pass a string as an argument you're passing a filehandle (<update>without the line delimiter implicitly undefined as in valdez's example</update>).

    ehdonhon and sauoq's approaches are better if all you've got is Perl in those files, but this'll work too:

    my $code; open FILE, "/home/samn/cgi-bin/$page.rs" or die 'Monkeys!'; while (<FILE>) { $code .= $_; } eval $code;
    which places all the text in your file into a single string and eval's it.

Re: executing code in a file
by tantarbobus (Hermit) on Aug 24, 2002 at 21:52 UTC

    You might want to look at using CGI::Application and AutoLoader.

    You can then tell CGI::Applicaton the set the functions that you want the cgi to call based on 'run_modes'. And when you enter that 'run_mode' perl auto load the function and call it.

    I have not tried this, but in theory it should work, I think.

Re: executing code in a file
by ehdonhon (Curate) on Aug 24, 2002 at 21:29 UTC

    How about:

    require "/home/samn/cgi-bin/$page.rs";
      That probably will not work because, IIRC, require will only execute the code the first time :)
192595
by Samn (Monk) on Aug 24, 2002 at 21:48 UTC