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: Intermediate:
Using BEGIN

 



perlfarmer
User

Nov 4, 2009, 9:14 AM

Post #1 of 6 (514 views)
Using BEGIN Can't Post

I have a BEGIN block in some code to set the path for a library:


Code
BEGIN { 

my $CEToolsPath = "/vbs/tools/cetools";
if (-d "/vbs/tools/cetools") {
$CEToolsPath = "/vbs/tools/cetools";
}
else {
$CEToolsPath = "/wsdb/tools.out/cetools";
}
}

use lib "$CEToolsPath/perllib";


However, I am getting the following error message:

"Global symbol "$CEToolsPath" requires explicit package name"

so clearly the variable $CEToolsPath is not being instantiated. Any advice is appreciated.


7stud
Enthusiast

Nov 4, 2009, 4:33 PM

Post #2 of 6 (510 views)
Re: [perlfarmer] Using BEGIN [In reply to] Can't Post

In your opinion, what is the purpose of "my" in a perl program?


(This post was edited by 7stud on Nov 4, 2009, 4:34 PM)


perlfarmer
User

Nov 5, 2009, 8:06 AM

Post #3 of 6 (484 views)
Re: [7stud] Using BEGIN [In reply to] Can't Post

I tried it with our. Same problem. They both appear to be scoped within the BEGIN block, which I wasn't expecting.


FishMonger
Veteran / Moderator

Nov 5, 2009, 8:11 AM

Post #4 of 6 (483 views)
Re: [perlfarmer] Using BEGIN [In reply to] Can't Post

Put the use lib statement inside the BEGIN block.


perlfarmer
User

Nov 5, 2009, 8:30 AM

Post #5 of 6 (481 views)
Re: [FishMonger] Using BEGIN [In reply to] Can't Post

[smacks forehead] I should have thought of that. Thanks.


perlfarmer
User

Nov 5, 2009, 10:02 AM

Post #6 of 6 (476 views)
Re: [perlfarmer] Using BEGIN [In reply to] Can't Post

Hmm. When I put the use lib statement inside the block a later require statement, that looks for something in that lib, fails. The only thing that seems to work is:


Code
my $CEToolsPath; 
BEGIN {

$CEToolsPath = "/vbs/tools/cetools";
if (-d "/vbs/tools/cetools") {
$CEToolsPath = "/vbs/tools/cetools";
}
else {
$CEToolsPath = "/wsdb/tools.out/cetools";
}
}
use lib "$CEToolsPath/perllib";


which is fine, but it doesn't fit with my understanding of how BEGIN works.

 
 


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

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