CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
eval "require $module"

 



srhadden
Novice

Jan 25, 2012, 2:45 PM

Post #1 of 14 (3611 views)
eval "require $module" Can't Post

I've seen this code before within a BEGIN block, which I understand BEGIN to be processed prior to run-time.

eval { require <somemodule> };

How is that diferent that just running require outside of a BEGIN block?


FishMonger
Veteran / Moderator

Jan 25, 2012, 6:39 PM

Post #2 of 14 (3603 views)
Re: [srhadden] eval "require $module" [In reply to] Can't Post

Loading a module via a use statement happens during compilation but require statements happen at runtime.

A BEGIN block is used when you want runtime code, such as a require statement, executed during the compilation phase.


srhadden
Novice

Jan 26, 2012, 9:59 AM

Post #3 of 14 (3597 views)
Re: [FishMonger] eval "require $module" [In reply to] Can't Post

Ah, ok I see.

Would you have any idea why when the eval { require ... } is run in the begin block, my module can't be found? Yet if I add the line:

require <module>;

It works fine?

I figured it was a library path setting, because I built and installed a cpan module in my home account, not as root, so I need to add my libpath. But I added lib 'mypath' inside the BEGIN block, yet the eval { require...} still acts as if it can't find the module.


FishMonger
Veteran / Moderator

Jan 26, 2012, 10:17 AM

Post #4 of 14 (3595 views)
Re: [srhadden] eval "require $module" [In reply to] Can't Post

Can you post the exact code of the working and non working sections of the scripts and the exact error message?


srhadden
Novice

Jan 26, 2012, 11:44 AM

Post #5 of 14 (3591 views)
Re: [FishMonger] eval "require $module" [In reply to] Can't Post

Sure!

#! /usr/bin/perl

use lib '/home/jason/lib64/perl5/site_perl/5.8.8/x86_64-linux-thread-multi';


BEGIN {
# See if we can load Term::ReadLine::Gnu
eval {require Term::ReadLine::Gnu};

if ($@) {
warn "Unable to load Term::ReadLine::Gnu\nCmdLine functionality will be limited!";
$_haveGnu = 0;
} else {
$_haveGnu = 1;
} # if
}

It says:

Unable to load Term::ReadLine::Gnu
CmdLine functionality will be limited! at ./readlinex.pl line 11.
Can't locate object method "using_history" via package "Term::ReadLine::Stub" at ./readlinex.pl line 22.


The error "using_history" is one of the extended routines to Term::Readline that the Gnu module offers. I think the ordering of the code matters, when I moved the lib statement under the BEGIN block, my module is loaded because I no longer get the using_history error. BUT, I still get "Unable to load Term::ReadLine::Gnu.

I don't really need this to work, but I just want to understand it to further my knowledge.

Thanks much for looking, and I appreciate your reply to my other post too.


(This post was edited by srhadden on Jan 26, 2012, 11:49 AM)


FishMonger
Veteran / Moderator

Jan 27, 2012, 8:44 AM

Post #6 of 14 (3582 views)
Re: [srhadden] eval "require $module" [In reply to] Can't Post

You need to print out the error message that the require statement generated, not simply your custom error message which doesn't say anything about why it failed.

You should always include the warnings and strict pragmas in every Perl script.

I'd start by writing a short test script which only loads the module and then work up from there.


Code
#!/usr/bin/perl 

use strict;
use warnings;

use lib '/home/jason/lib64/perl5/site_perl/5.8.8/x86_64-linux-thread-multi';
use Term::ReadLine::Gnu;



srhadden
Novice

Jan 27, 2012, 10:14 AM

Post #7 of 14 (3579 views)
Re: [FishMonger] eval "require $module" [In reply to] Can't Post

OK, very good to know in the future.

It didn't procduce any more warnings. So I print out the result from the eval statement, the result is stored in $@ after the eval statement.

CmdLine functionality will be limited! at ./readlinex.pl line 17.
Can't locate object method "using_history" via package "Term::ReadLine::Stub" at ./readlinex.pl line 30.


DEBUG: Can't locate Term/ReadLine/Gnu.pm in @INC (@INC contains: /usr/lib64/perl5/site_perl/5.8.8/x86_64-linux-thread-multi /usr/lib/perl5/site_perl/5.8.8 /usr/lib/perl5/site_perl /usr/lib64/perl5/vendor_perl/5.8.8/x86_64-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.8 /usr/lib/perl5/vendor_perl /usr/lib64/perl5/5.8.8/x86_64-linux-thread-multi /usr/lib/perl5/5.8.8 .) at ./readlinex.pl line 10.

SOO, it does not have my libary path. Where should I be adding the location of my library path? At the top before the BEGIN call?

When I do that, then the error is different.

DEBUG: It is invalid to load Term::ReadLine::Gnu directly. Please consult
the Term::ReadLine documentation for more information.
at /home/bfleming/lib64/perl5/site_perl/5.8.8/x86_64-linux-thread-multi/Term/ReadLine/Gnu.pm line 69


I pulled this code from a scirpt that I know works, so just not sure why it isn't working for me.


FishMonger
Veteran / Moderator

Jan 27, 2012, 12:22 PM

Post #8 of 14 (3577 views)
Re: [srhadden] eval "require $module" [In reply to] Can't Post

I have not use Term::ReadLine::Gnu and would need to run some tests to be sure, but I think (based on the module's synopsis) that when it's installed, it replaces the default Term::ReadLine module with one that loads this module extension.


srhadden
Novice

Jan 27, 2012, 12:24 PM

Post #9 of 14 (3575 views)
Re: [FishMonger] eval "require $module" [In reply to] Can't Post

Yeah it is a little different because it replaces/extemds Term::Readline, which was new to me since I have gotten by mostly with the core modules.

I would say let this one rest, you helped so much I can't ask for more on this subject because it isn't really blocking me, more of a curiosity.

Thank you, sir.


srhadden
Novice

Feb 16, 2012, 11:22 AM

Post #10 of 14 (3421 views)
Re: [srhadden] eval "require $module" [In reply to] Can't Post

Sorry, I'm using firefox with Mac OS Lion (if that matters) and by god, I can't even post a new message to the forums.

I feel like an idiot here, I don't get it. Anyone have any idea what I'm talking about?? I try to make a new post, type it up, nothing happens, but apparently I can reply.


(This post was edited by srhadden on Feb 16, 2012, 11:25 AM)


BillKSmith
Veteran

Feb 16, 2012, 4:22 PM

Post #11 of 14 (3400 views)
Re: [srhadden] eval "require $module" [In reply to] Can't Post

There have been very few (if any) new threads in the past three weeks. You seem to have identified the reason. I also am unable to start a new thread. I suspect that there are new anti-spam measures which are to strict. We can only hope that someone with the necessary authority is reading this.

Good job noticing the difference between new thread and reply!
Good Luck,
Bill


srhadden
Novice

Feb 16, 2012, 4:55 PM

Post #12 of 14 (3397 views)
We Can't Post New Threads admins!! [In reply to] Can't Post

Yeah, I hear ya bud. The first time I noticed the problem was I couldn't login from my ipad, and I just cursed apple's browser, but it probably is site related.

Glad I'm not going any more nuts that I already am.

Cheers!


FishMonger
Veteran / Moderator

Feb 17, 2012, 6:22 AM

Post #13 of 14 (3384 views)
Re: [srhadden] We Can't Post New Threads admins!! [In reply to] Can't Post

If you want, you could send an email to the administrative contact found when doing an whois lookup. That is what I did to get them to work on cleaning up the spam problem.


FishMonger
Veteran / Moderator

Feb 19, 2012, 9:37 AM

Post #14 of 14 (3320 views)
Re: [FishMonger] We Can't Post New Threads admins!! [In reply to] Can't Post

FYI, I just sent an email to the site admin, Alex Krohn, regarding the problem with not being able to create new threads.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives