Thread: eval and use

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Posts
    210
    Rep Power
    17

    eval and use


    hi folks,
    a theorhetical ive been chewing over - as always insights are appreciated

    Code:
    use strict;
    
    my $test;
    my $module  = "LWP::Simple";
    
    if ($test){
    	eval "use $module";
    	}
    
    my $foo = get('http://www.google.com');
    
    print $foo;
    
    Undefined subroutine &main::get called at \PST.TMP line 11.
    now, If $test=1, then we get google's source code on screen...
    and if the if's code block was just 'use LWP::Simple' - then $foo would print out OK regardless of the value of $test.

    So...is perl compiling LWP::Simple anyways, then acting like it didnt - to fit with the if condition?

    This is what perldocs say about eval();

    eval '$x'; # CASE 3
    eval { $x }; # CASE 4

    (Case 4 is preferred for purely visual reasons, but it also has the advantage of compiling at compile-time instead of at run-time.)
    does this imply that case 3 (and my 'use $module') will not compile at compile-time, but wait to see if the conditions are correct first?

    ?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 1999
    Location
    Seattle
    Posts
    133
    Rep Power
    20
    use is always compile time, so it won't matter where you put it. use is the equivalent of:

    BEGIN { require module; import module list; }

    In the case of your eval, Perl doesn't know that this is actually a use statement, so it is executed only when the $test is true.

    Don
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Posts
    210
    Rep Power
    17
    sweet as

    anyone know of side effects in using this? It seems just too simple and perfect to be so undocumented.
    heres the kind of vibe i am considering - would appreciate opinions on this,

    Code:
    my $qmode 	= 'runThis';      # or $q->param('rm')
    my %runmodes 	= (
    		runThis		=> "myApp::someMod",
    		doAnother	=> "myApp::another",
    		sameModule	=> "myApp::someMod",
    		AUTOLOAD	=> "myApp::Common"
    		);
    
    
    #+------------------------------------------
    # blatent hack of CGI::Application, somewhat simplified
    my $rmOK;
    my $autoload;
    
    	if (exists($runmodes{$qmode})) {
    		$rmOK = $runmodes{$qmode};
    	} else {
    		$rmOK = $runmodes('AUTOLOAD');
    		$autoload = 1;
    	}
    
    my $rmGO 	= eval "use $rmOK";
    my $OUTPUT;
    
    unless ($autoload){
    		$OUTPUT 		= $qmode->($self);
    		# etc etc
    	}
    else {
    		$OUTPUT 		= AUTOLOAD->($self);
    
    	}
    
    
    # and so on...
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 1999
    Location
    Seattle
    Posts
    133
    Rep Power
    20
    What you have would work, but why try to reinvent the object oriented-ness built into Perl? Even using AUTOLOAD should be 'auto'matic. If you make your modules subclasses of each other, you can eliminate all of the if() tests to call the correct module, it's easier and less error prone to let Perl handle it.

    For more inspiration about Perl's OOP, check out Damian Conway's book "Object Oriented Perl". You can download sample chapters and source code from here (there is even a low-cost eBook version available):

    http://www.manning.com/Conway/

    Don
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Posts
    210
    Rep Power
    17
    donarb, thanks again

    I wasnt aware i was reinventing anything. im still pretty new at perl nevermind OOP, thats why i was asking about the above.

    Im really just looking for a flexible way of storing/managing packages of functions...without loading them all whenever the main module kicks off.

    will check out the ebook, chapter3 was pretty handy
    cheers!

IMN logo majestic logo threadwatch logo seochat tools logo