Read Variables from include file

S

sulata

Hello

I am new to perl but have lot of experience in php. I have a question.

In one file I want to define a variable like;

#FILE NAME: variable.pl
$name = "Google";

And in the second file, I want to include this file and read the
variable like;

require('variable.php');
print $name;

This is what we do in PHP but does not seem to work in PERL. Can anuone
help?
 
S

sulata

Ok thanks. I have another question if you can answer. Is there a way to
make one connection file in perl and use in all my pages by including
it. If yes, how please.

Thanks
 
G

Gunnar Hjalmarsson

In one file I want to define a variable like;

#FILE NAME: variable.pl
$name = "Google";

And in the second file, I want to include this file and read the
variable like;

require('variable.php');
print $name;

This is what we do in PHP but does not seem to work in PERL. Can anuone
help?

In Perl you might have:

use variable;

or

require 'variable.pl';

Suggested reading: http://perldoc.perl.org/perlmod.html
 
S

sulata

Thanks all solved. I multi posted it because I needed a solution. Sorry
for the inconvenience caused to anyone.
 
T

Tad McClellan

I multi posted it because I needed a solution.


I dumped trash in the park because I needed a place to put it.

That makes it OK somehow?

Sorry
for the inconvenience caused to anyone.


Sorry for the inconvenience caused to the kids who play in the park.
 
K

krakle

Thanks all solved. I multi posted it because I needed a solution. Sorry
for the inconvenience caused to anyone.

And if everyone did that Usenet would be Useless.

You actually still didn't get your question answered. If you want to
use (or import) variables from a different Perl script and 'require'
them just like PHP that will work the same way:

require 'some.pl';
print $name;

__BUT__ it's considered SLOPPY CODING and will _not_ work complying
with proper code guidlines.

use strict; # Mandatory for the intelligent
require 'some.pl';
print $name;

You receive this error:
Global symbol "$name" requires explicit package name

The best way to 'import' variables from another file is to create a
proper module (.pm). Again, like mentioned before if you only want to
share variables for configuration purposes there are many other easier
and more practical solutions. Check out CPAN.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,202
Messages
2,571,055
Members
47,659
Latest member
salragu

Latest Threads

Top