Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

a simple include....

by pcouderc (Monk)
on Apr 30, 2003 at 13:55 UTC ( [id://254283]=perlquestion: print w/replies, xml ) Need Help??

pcouderc has asked for the wisdom of the Perl Monks concerning the following question:

I am new to perl and I am sorry for a so basic question (but it indicates that I have not found in the doc). I have some perl scripts (under modperl Apache) that share common source, more exactly they use the same lines of code. In C++, I should use an #include.
Is there a perl equivalent that I have not found?
I have tried "use" or "require" but this is absolutely not the equivalent.

Thank you in advance
Pierre Couderc

Replies are listed 'Best First'.
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.

      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.

        '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
        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

        If you declare $SousMaintenance within prov.pl, like so:

        #!/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."
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.
      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.
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
      Thank you, I have just tried that, but I get the same error
      Global symbol "$SousMaintenance" requires explicit package name...
        A code snippet would help us to help you debug it :-)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://254283]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2024-04-20 04:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found