Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Re: a simple include....

by pcouderc (Monk)
on Apr 30, 2003 at 14:04 UTC ( [id://254292]=note: print w/replies, xml ) Need Help??


in reply to Re: a simple include....
in thread a simple include....

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.

Replies are listed 'Best First'.
Re: Re: Re: a simple include....
by pfaut (Priest) on Apr 30, 2003 at 14:13 UTC

    '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
Re: Re: Re: a simple include....
by broquaint (Abbot) on Apr 30, 2003 at 14:17 UTC
    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

      1- I understand from your answer that there is no equivalent of a "simple" include
      2- I have tried do declare
      package main;
      in both main.pl and inc.pl. It would oblige me to declare $main::Sousprogramme for all the variables of my main.pl. I have done that but I get more errors in a
      $main::cursor = ($main::dbh)->prepare("Select...");

      Database handle destroyed without explicit disconnect at /var/www/tools/crm/main.pl line 16.
      Wed Apr 30 15:59:34 2003 error Can't call method "prepare" on an undefined value at /var/www/tools/crm/main.pl line 44.
      This becomes complicated when I was looking for a "simple" include....
        I understand from your answer that there is no equivalent of a "simple" include
        There is, and it's require. Your requirements are more complex however, precluding one from any simple answer.

        Firstly package main is only necessary to explicitly declare that you are in main's namespace, which all code is by default. So unless you have other package declarations it will be unncessary in your code.

        Secondly, $main::dbh has not beeen assigned anywhere (or was defined somewhere, and undefined at a later point) which is why you get that second error message. Note that lexical variables are not the same as package variables, so if you have declared my $dbh in inc.pl then it will be undefined in main.pl (and does not exist as $main::dbh which is a package variable).

        As for the first error message I imagine it is due to the fact you have declared $dbh as a lexical variable in inc.pl and it is falling out of scope which will call it's DESTROY method (destructor) and thereby trigger said error message as there has been no explicit disconnect.
        HTH

        _________
        broquaint

        The perl way to do this is to isolate the common code in a module that exports symbols into the user's namespace. Look into Exporter and its various clones for packages that will assist you in setting this up.

        90% of every Perl application is already written.
        dragonchild
Re: Re: Re: a simple include....
by crouchingpenguin (Priest) on Apr 30, 2003 at 16:27 UTC

    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."

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2024-04-24 21:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found