in reply to a simple include....

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.

Replies are listed 'Best First'.
Re: Re: a simple include....
by pcouderc (Monk) on Apr 30, 2003 at 14:04 UTC
    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

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

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