Re: a simple include....
by dragonchild (Archbishop) on Apr 30, 2003 at 13:56 UTC
|
What's wrong with require? That _is_ the analog to #include ...
------ We are the carpenters and bricklayers of the Information Age. Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement. Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified. | [reply] |
|
I have tried to take part of my prov.pl (which worked perfectly) in a inc.pl and add a
require './inc.pl'
Then it generates me this message :
[Wed Apr 30 16:00:23 2003] [error] Global symbol "$SousMaintenance" re
+quires explicit package name at /var/www/tools/crm/prov.pl line 17.
So, it is absolutely not a "simple" include.
| [reply] [d/l] [select] |
|
'require' isn't quite exactly an '#include'. The included file has its own scope which is causing your error. Fortunately, that's not too hard to work around.
Perl has very good support for reusable code. All you have to do is take a little time to figure out how to properly format it. These (along with hmerrill's recommendations]) ought to get you started.
90% of every Perl application is already written. ⇒ | dragonchild |
| [reply] |
|
This error is due to the fact that you have strictures in use when including your file which unfortunately does not conply with strict. The 'simple' option is simple to turn off strictures when you're including the file
## effects of 'no strict' limited to the anonymous blocko
{
no strict;
require "./inc.pl";
}
It's a little fiddly I admit, but no more fiddly than a script that doesn't conform to strict (it really does make your life easier).
HTH
_________ broquaint | [reply] [d/l] |
|
|
|
|
|
#!/usr/bin/perl
# prov.pl
use strict;
use warnings;
use vars qw( $SousMaintenance );
require './inc.pl';
print $SousMaintenance,"\n";
with an inc.pl like:
$SousMaintenance = 'value one';
1;
the problem goes away. Another idea for you is to include a package declaration within inc.pl so that you create a namespace (seperate from main::) like so:
# inc.pl
package myInc;
$SousMaintenance = 'value one';
Then call it like this within prov.pl:
print $myInc::SousMaintenance,"\n";
cp
----
"Never be afraid to try something new. Remember, amateurs built the ark. Professionals built the Titanic." | [reply] [d/l] [select] |
Re: a simple include....
by hmerrill (Friar) on Apr 30, 2003 at 14:06 UTC
|
What's wrong with 'use'? I, and probably most of the perl community, develop common code routines all the time - I typically create a perl 'module' - put my common code in there - and then 'use' that module anytime I need access to that code.
If you're not yet familiar with Perl's 'perldocs', get familiar with them. I just did 'perldoc perl', and then searched(using the forward slash '/') for 'module', and here are a few things I found at the top:
perlmod Perl modules: how they work
perlmodlib Perl modules: how to write and use
perlmodstyle Perl modules: how to write modules with
+ style
perlmodinstall Perl modules: how to install from CPAN
perlnewmod Perl modules: preparing a new module fo
+r distribution
So, to read the perldocs for 'perlmod', I'd do 'perldoc perlmod'.
HTH. | [reply] [d/l] |
|
I am not exactly speaking of common code, but of common 'text'. This is not a subroutine, it is a common set of declarations and too of code. It is to generate a common look in some HTML pages.
| [reply] |
|
| [reply] |
Re: a simple include....
by dpuu (Chaplain) on Apr 30, 2003 at 14:30 UTC
|
use and require are the most common mechanism to import stuff into your code. But the thing that is closest to C's #include is do. Pass it a string: perl will treat it as a filename, and execute the file using the same scoping rules as eval. --Dave | [reply] |
|
Thank you, I have just tried that, but I get the same error
Global symbol "$SousMaintenance" requires explicit package name...
| [reply] |
|
A code snippet would help us to help you debug it :-)
| [reply] |