For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > December 2007 > export variables









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author export variables
jwaixs@gmail.com

2007-12-30, 4:02 am

Hello,

Could someone tell me if this is possible and if it is how I do it. I
have the following two file;

file1.pl
---
print "$testvar\n";
---

file2.pl
---
my $testvar = 37;
use "file1.pl";
---

If I run file2.pl (perl file2.pl) I will of course only see a newline.
But is it possible to export the testvar to file1.pl?

Thank you,
Noud

J. Peng

2007-12-30, 4:02 am

On Dec 29, 2007 8:32 PM, <jwaixs@gmail.com> wrote:
> Hello,
>
> Could someone tell me if this is possible and if it is how I do it. I
> have the following two file;
>
> file1.pl
> ---
> print "$testvar\n";
> ---
>
> file2.pl
> ---
> my $testvar = 37;
> use "file1.pl";
> ---
>
> If I run file2.pl (perl file2.pl) I will of course only see a newline.
> But is it possible to export the testvar to file1.pl?
>


Hi,

You can do what you wanted by declaring the variable $testvar with 'our':

$ cat t1.pl

print $testvar;

$ cat t2.pl
use strict;
our $testvar = 123;
require 't1.pl';

$ perl t2.pl
123


But wait, this way above is not good practice in perl programming.
You'd better write something like below:

$ cat t1.pl

sub myprint {
print $testvar;
}
1;

$ cat t2.pl
use strict;
require 't1.pl';
our $testvar = 123;

myprint();

$ perl t2.pl
123

Surely this is one way I showed. There are more ways to do it,like
using the OO,etc.
Patmarbidon

2007-12-30, 4:02 am

Hello if you want to share variables content you might to use 'our'
instead of 'my'.

But I don't understand your example with 'use "file1.pl"'.

I always use 'use module_name' and never 'use program_name'

Can you tell us more

jwaixs@gmail.com a écrit :
> Hello,
>
> Could someone tell me if this is possible and if it is how I do it. I
> have the following two file;
>
> file1.pl
> ---
> print "$testvar\n";
> ---
>
> file2.pl
> ---
> my $testvar = 37;
> use "file1.pl";
> ---
>
> If I run file2.pl (perl file2.pl) I will of course only see a newline.
> But is it possible to export the testvar to file1.pl?
>
> Thank you,
> Noud
>
>


Jwaixs

2007-12-30, 7:01 pm

On Dec 30, 6:45 am, patmarbi...@free.fr (Patmarbidon) wrote:
> Hello if you want to share variables content you might to use 'our'
> instead of 'my'.


Yes, that does it! Thank you!

>
> But I don't understand your example with 'use "file1.pl"'.
>
> I always use 'use module_name' and never 'use program_name'
>
> Can you tell us more


I just want to split up a big file into smaller parts. But I don't
know which functioncall I should use for it. Peng is using "require",
is this the standard way for importing a file?

Noud

Jeff Pang

2007-12-30, 7:01 pm

On Dec 30, 2007 7:29 PM, jwaixs <jwaixs@gmail.com> wrote:

>
> I just want to split up a big file into smaller parts. But I don't
> know which functioncall I should use for it. Peng is using "require",
> is this the standard way for importing a file?
>


Generally we use 'require' to read a config file.But this is not
absolute.You can use 'require','use' or 'do' for this destination.

from perldoc -q "What's the difference between require and use":

Perl offers several different ways to include code from one
file into another. Here are the deltas between the various
inclusion constructs:

....
Chas. Owens

2007-12-30, 7:01 pm

On Dec 30, 2007 6:29 AM, jwaixs <jwaixs@gmail.com> wrote:
snip
> I just want to split up a big file into smaller parts. But I don't
> know which functioncall I should use for it. Peng is using "require",
> is this the standard way for importing a file?

snip

The require function is one of the standard ways of including code
from another file. The use function (which uses require, but does a
few other things) is another. You can also get similar functionality
from the do function, but that is generally not a good idea.

Just breaking up a large file into many small ones is a bad idea,
especially if you are sharing variables between the files. If your
program is too large then try to break some of the functionality out
into modules instead. Keep each module independant of the others and
the main program. This will make it easier to make changes later and
you may even find that you can reuse some of the modules in other
projects. You can read more about modules in perldoc perlmod or
http://perldoc.perl.org/perlmod.html.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com