using eval 
Author Message
 using eval

I'm trying to use eval to execute all the code contained in a separate
file.  I've tried the following:

eval "file.pl";

but, of course, that ends up trying to execute the string "file.pl" as
if it were code.

How can I make eval run the code contained in file.pl?

Thanks.

--
Ken



Thu, 22 Jan 2004 09:20:48 GMT  
 using eval


Quote:
> I'm trying to use eval to execute all the code contained in a separate
> file.  I've tried the following:

> eval "file.pl";

> but, of course, that ends up trying to execute the string "file.pl" as
> if it were code.

> How can I make eval run the code contained in file.pl?

eval {
        require "file.pl";

Quote:
};

 or

 open(F, "file.pl") || die;  
 {
        local $/;
        $a=<F>;
 }
 eval "$a";

 or, if you don't really need eval...

do "file.pl";

--
    Clinton A. Pierce            Teach Yourself Perl in 24 Hours  *and*

"If you rush a Miracle Man,     for details, see http://geeksalad.org    
        you get rotten Miracles." --Miracle Max, The Princess Bride



Thu, 22 Jan 2004 10:47:49 GMT  
 using eval


Quote:


>> I'm trying to use eval to execute all the code contained in a separate
>> file.  I've tried the following:

>eval {
>    require "file.pl";
>};

> or

> open(F, "file.pl") || die;  
> {
>    local $/;
>    $a=<F>;
> }
> eval "$a";

> or, if you don't really need eval...

>do "file.pl";

"do" may be better than "require" anyway, since they have another
important difference: "do" runs the code every time, and "require" runs
it only a single time.  So usually "do" is useful if you need to
actually run code, whereas "require" is useful if you want to do things
like define subroutines.

  - Logan
--
"Our grandkids love that we get Roadrunner and digital cable."
(Adverti{*filter*}t for Time Warner cable TV and internet access, July 2001)



Thu, 22 Jan 2004 14:55:31 GMT  
 using eval

Quote:





> >> I'm trying to use eval to execute all the code contained in a separate
> >> file.  I've tried the following:

> >eval {
> >       require "file.pl";
> >};

> > or

> > open(F, "file.pl") || die;  
> > {
> >       local $/;
> >       $a=<F>;
> > }
> > eval "$a";

> > or, if you don't really need eval...

> >do "file.pl";

> "do" may be better than "require" anyway, since they have another
> important difference: "do" runs the code every time, and "require" runs
> it only a single time.  So usually "do" is useful if you need to
> actually run code, whereas "require" is useful if you want to do things
> like define subroutines.

>   - Logan

Thanks, that helps.

--
Ken



Fri, 23 Jan 2004 01:10:37 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Test for eval() without using eval() ??

2. Using eval() for subroutine unwind

3. Using eval to iterate over a set of Perl variables

4. Using eval

5. Using Eval Statement To Trap Module Not Present Fatal Error

6. using eval to create formats

7. Using eval in regex

8. Using eval and $@

9. how to setup exit routine using eval

10. dynamic object instantiation using eval

11. problem using eval with array of file test operators

12. Bug when using eval

 

 
Powered by phpBB® Forum Software