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:
About require and global variable

 



marcel
New User

Nov 25, 2009, 8:50 AM

Post #1 of 3 (553 views)
About require and global variable Can't Post

Hello all,

I would like to *include* a file with variable definition on my perl script. This file is shared with a php application which includes it also. That are my settings (very simplified):

Config:
$name = "foo";
$email = "bar";

main.pl:
require "Config";
require "function.pl";

print $name;

main.php:
<?php include("Config"); echo $name; ?>

This is working as wanted but I have one more file level (function.pl) in my perl script and it s not working anymore

function.pl:

package Func;

sub myFunc
{
print $name;
}

What am I doing wrong ?

Thanks a lot.


shawnhcorey
Enthusiast


Nov 25, 2009, 1:37 PM

Post #2 of 3 (550 views)
Re: [marcel] About require and global variable [In reply to] Can't Post

When you write `package Func;` you are switching namespace. All the names you use belong to it and not package main. To access the main namespace, use fully qualified names.

Code
package Func; 

sub myFunc
{
print $::name;
}

1;


$::name is a shorten version of $main::name.

See `perldoc -f package` or http://perldoc.perl.org/functions/package.html

__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


marcel
New User

Nov 26, 2009, 1:52 AM

Post #3 of 3 (545 views)
Re: [shawnhcorey] About require and global variable [In reply to] Can't Post

Solved, thanks a lot.

 
 


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

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