An issue with "Require"

B

Bill H

In many of my programs I use require as a sort of conditional include
to use code only when needed. I have noticed that if a "required"
program also has a "require" in it that the 2nd one does not have
access to the variables from the first, but does have access to the
variables from the main code. Is this normal or am I doing something
wrong? Here is a rough example on how I am using it:

Main program

require "global.pm"; # These are global routines used by everything

if ($var == 1)
{
require "subprog.pm";
}

Subprog.pm

$page = "index.htm";
&SendPage($page); # this is a routine in global.pm to load and display
the file named in $page

The above will work fine, but if I do this:

Subprog.pm

$page = "index.htm"
if ($page eq "index.htm")
{
require "subprog2.pm";
}

Subprog2.pm

&SendPage($page);

Subprog2.pm does not get the value for $page passed to it by
Subprog,pm.

I know this is rough but I hope it illustrates the idea. There is
probably something simple I am not doing but I have not been able to
figure out how to get a required program to allow its variables to be
accessed by another one. Am i right in assuming that code that is
included using "require" just gets "run" in the place where the require
is at (similar to using #include in C), or is it run as a form of
subroutine?

Bill H www.ts1000.us
 
A

anno4000

Bill H said:
In many of my programs I use require as a sort of conditional include
to use code only when needed. I have noticed that if a "required"
program also has a "require" in it that the 2nd one does not have
access to the variables from the first, but does have access to the
variables from the main code. Is this normal or am I doing something
wrong? Here is a rough example on how I am using it:

Main program

require "global.pm"; # These are global routines used by everything

if ($var == 1)
{
require "subprog.pm";
}

Subprog.pm

This doesn't call a program in Perl. Under strictures it's a syntax
error, with "no strict 'subs'" it is a concatenation of two barewords.
$page = "index.htm";
&SendPage($page); # this is a routine in global.pm to load and display
the file named in $page

The above will work fine, but if I do this:

Define "work". It doesn't look like it does what it is meant to do.

[more similar code snipped]

Anno
 
C

Chris Davies

Bill H said:
In many of my programs I use require as a sort of conditional include
to use code only when needed. I have noticed that if a "required"
program also has a "require" in it that the 2nd one does not have
access to the variables from the first, but does have access to the
variables from the main code. Is this normal or am I doing something
wrong? Here is a rough example on how I am using it:

It seems to work fine here using perl 5.8.7 on GNU/Linux. Incidentally,
why are you using &SendPage() instead of SendPage()?

main.pl:
#!/usr/bin/perl
#
use strict;
use warnings;

warn "requiring global.pm\n";
require "global.pm";

my $page;
my $var = 0+ ((shift @ARGV) || 0);
warn "var=$var\n";

if ($var == 1) {
warn "requiring subprog.pm\n";
require "subprog.pm";
}

global.pm:
warn "running global.pm\n";

sub SendPage
{
my $page = shift;
warn ">> SendPage($page)\n";
}

subprog.pm:
warn "running subprog.pm\n";

$page = "index.htm";

warn 'calling &SendPage()', "\n";
&SendPage($page);

warn 'calling SendPage()', "\n";
SendPage($page);

if ($page eq 'index.htm') {
warn "requiring subprog2.pm\n";
require "subprog2.pm";
}

subprog2.pm:
warn "running subprog2.pm\n";

warn 'calling &SendPage()', "\n";
&SendPage($page);

warn 'calling SendPage()', "\n";
SendPage($page);


This lot produces this output, which seems to be what you're wanting:
$ perl main.pl 1
requiring global.pm
running global.pm
var=1
requiring subprog.pm
running subprog.pm
calling &SendPage()requiring subprog2.pm
running subprog2.pm
calling &SendPage()
Chris
 
M

Mumia W.

Bill said:
[...] Am i right in assuming that code that is
included using "require" just gets "run" in the place where the require
is at (similar to using #include in C), or is it run as a form of
subroutine?

No. "Required" files are loaded and run *once*. After that, they are
never loaded again--no matter how many times you "require" them.

If you say this:

require "myprogram.pm";
require "myprogram.pm";

"Myprogram.pm" will only be loaded and run once. The second "require"
will essentially be ignored. "Require" is a way of telling perl you need
the functionality of a script, and the script you require should do
little beyond initialize itself because perl wants to be efficient by
loading the script only once.

If you want perl to do something with a script each time it's called,
use the "do" command:

do "myprogram.pm";
do "myprogram.pm";

Now "myprogram.pm" will be executed twice.


perldoc -f require
perldoc -f do
 
D

Dave Weaver

if ($var == 1)
{
require "subprog.pm";
}

I get the impression that you expect this code to be the equivalent
of inserting them contents of subprog.pm inside the body of the if();
it's not. (i.e. require isn't the equivalent of C's #include)
See
perldoc -f require
to find out what require really does.

Perhaps
do "subprog.pm";
is the sort of thing you're thinking of, but it's still a horrible
approach.

What are you trying to achieve by having these conditional requires?
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top