Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Including files

by ambrus (Abbot)
on Sep 24, 2004 at 11:44 UTC ( [id://393464]=note: print w/replies, xml ) Need Help??


in reply to Including files

Couldn't one write ACME::Include based on source filters instead of the DB way you gave above?

Source filters are a bad idea in general, but note that this one does not attempt to parse perl code, so it can not be fooled by wierd-looking perl code like some source filters can be. I still don't say that soing such things would be a good idea, but it might be cleaner that the DB hack.

Here's my example, which just a quick draft, does not handle line numbers etc.

Filter/Include.pm is

package Filter::Include; use warnings; use strict; use Filter::Util::Call; sub import { @_==2 or die qq{usage: use Filter::Include "filename"}; open my $f, "<", $_[1] or die qq{cannot open include file "$_[ +1]": $!}; read $f, my $d, -s $f or die qq{cannot read file "$_[1]": $!}; filter_add(sub { $_ = $d; filter_del(); 1; }); } 1; __END__

first is:

#!perl -w use warnings; use strict; my(@a, @b); @a = ( 1, 2, do{ use Filter::Include "./second"; }, 7, 8); print "a(@a) b(@b)\n"; __END__

and second which has to be in the current dir when first is run:

3, 4); @b = (5, 6,

Then, perl first prints a(1 2 3 4) b(5 6 7 8)

Replies are listed 'Best First'.
Re^2: Including files
by Juerd (Abbot) on Sep 24, 2004 at 11:51 UTC

    IMO, the DB hack is a cleaner solution, because it is faster, uses a documented feature and has clean syntax for using it. To achieve such clean syntax with a source filter, you need to write a complex regex.

    Besides that, source filters don't work everywhere (like in eval) and I really think the included file should by itself be syntactically correct. Including code is bad, but including partitial expressions is, IMHO, even worse.

    On the other hand, the source filter really literally includes, while the DB hack only loads and runs during runtime.

    Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }

      IMO, the DB hack is a cleaner solution, because it is faster, uses a documented feature ...

      I don't think any of them would be much faster than the other. The source filter is documented too.

      and has clean syntax for using it. To achieve such clean syntax with a source filter, you need to write a complex regex.

      It's not that simple. The difference is that the code I gave above does the include in compile time, and its syntax is use Filter::Include "file" (the do is not needed when including complete statements). The DB way includes the code at run-time, that's why it's possible to use a simple subroutine include "file" is possible. While it is indeed not possible to make my solution work with such a simple syntax, without actually interpreting the code (incidentally that's what the actual Filter::Include cpan module does); if you wanted to modify the DB solution so that it includes the code in compile time (which can be a difference in semantics, depending on what the include file contains), you'll have to use a use or BEGIN syntax too, or try to parse the code.

      Besides that, source filters don't work everywhere (like in eval)...
      That's true. More generally, source filters can be used only at compile-time, not runtime. Also, source filters can not be used from command line (-e) it seems.
      and I really think the included file should by itself be syntactically correct. Including code is bad, but including partitial expressions is, IMHO, even worse.

      True. I just wanted to show that this is really including the file.

      Finally let me note that some include facility is already built in perl: the -P switch. If the file third contains

      #!perl -w use warnings; use strict; my(@a, @b); @a = ( 1, 2, #include "./second" 7, 8); print "a(@a) b(@b)\n"; __END__
      and you run it with perl -P third, you get the same results. Of course, perlrun warns you that there are lots of problems with the -P switch.

        Finally let me note that some include facility is already built in perl: the -P switch.

        The -P switch is built in, but its functionality is all but. It depends on an external utility and execs sh, then perl and then cc. It's not only a very dangerous construct, but also a very inefficient one :)

        Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-03-29 11:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found