Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Re: Re: Re: Re: a simple include....

by pcouderc (Monk)
on Apr 30, 2003 at 14:38 UTC ( #254313=note: print w/replies, xml ) Need Help??

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

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 and It would oblige me to declare $main::Sousprogramme for all the variables of my 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/ line 16.
Wed Apr 30 15:59:34 2003 error Can't call method "prepare" on an undefined value at /var/www/tools/crm/ line 44.
This becomes complicated when I was looking for a "simple" include....

Replies are listed 'Best First'.
Re: Re: Re: Re: Re: a simple include....
by broquaint (Abbot) on Apr 30, 2003 at 15:07 UTC
    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 then it will be undefined in (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 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.


      Ok, thank you, I understand "package main" is not necessary.
      I am surprised by your insistence in saying that "require" is an equivalent of an include. I suppose it is a question of word. What I call a "simple include", is like a macro, a textual equivalence, like macros or include processed by a C preprocessor : no syntax analysis is done. This is opposite to "require" which "load in external functions from a library at runtime" : I never said that I wanted to include fonctions nor make a library, I want a "simple include"...
      I would have accepted the answer that what I want to do is not possible, that it is not in the spirit of perl, coming from you a so good famous "saint"...I wonder if there is not something else that I miss.
        include processed by a C preprocessor : no syntax analysis is done
        Aha, I see where you're coming from now. Unfortunately perl really isn't geared towards source manipulation in that way - all its including functions must evaluate the code in the given file before including it into the parent source, such is the nature of perl. This behaviour is due to the fact that perl is a semi-interpreted dynamic language (it's compiled down to internal bytecode and then executed within perl) so when you require a file it is performed at runtime and therefore must be syntactically sound and pass any strictures that might be in place. As for use that's just a require done at compile-time and also calls the import method of the given package.

        So, finally, to answer your question (as I now understand it ;), no, perl has no 'simple 'include'.
        But here's one I made earlier (as it were)

        package include; use IO::File; use Regexp::Common 'quoted'; use Filter::Simple; =head1 SYNPOSIS use include; include Foo::Bar; include ""; =cut FILTER_ONLY code => sub { local $/; s< ^ \s* include \s+ (.*?) ; $> ( my $f = $1; if($f =~ /^['"]) { (undef, undef, $f) = $f =~ $RE{quoted}{-keep}; } else { ($f = "$") =~ s<::>(/)g; } my $fh = IO::File->new($f) or die("include: $!"); $fh->getlines )xg; } ;
        Roughly, the code should act like #include does in C by just dropping the source of the given package or file in place. It's untested at the moment, but hopefully it should DTR. There's also Text::MacroScript but I'm not sure if that meets your needs of a 'simple include' (although my gruesome filter is hardly simple ;).


Re: Re: Re: Re: Re: a simple include....
by pfaut (Priest) on Apr 30, 2003 at 14:45 UTC

    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.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2021-04-19 15:16 GMT
Find Nodes?
    Voting Booth?

    No recent polls found