force recursive source filtering

A

Andrew Schulman

I want to apply a source filter, via say Filter::Simple, not just to a
particular source file, but to all of the source files that it may do, require,
or use. E.g., if I've built a source filter in mySourceFilter, then in

use mySourceFilter;
use custommodule;
print ...

the code that resides in the above source file will be filtered, but the code
that it reads in from custommodule.pm will not. This is what
http://perldoc.perl.org/perlfilter.html says, and my observation confirms it.

What I want is to modify the do, require, and use statements to automatically
apply mySourceFilter to every file they load. But I don't know how.

Anyone done this? Suggestions?
Thanks,
Andrew.
 
U

Uri Guttman

AS> I want to apply a source filter, via say Filter::Simple, not just
AS> to a particular source file, but to all of the source files that
AS> it may do, require, or use. E.g., if I've built a source filter
AS> in mySourceFilter, then in

AS> use mySourceFilter;
AS> use custommodule;
AS> print ...

AS> the code that resides in the above source file will be filtered,
AS> but the code that it reads in from custommodule.pm will not. This
AS> is what http://perldoc.perl.org/perlfilter.html says, and my
AS> observation confirms it.

AS> What I want is to modify the do, require, and use statements to
AS> automatically apply mySourceFilter to every file they load. But I
AS> don't know how.

did you know that source filtering is considered very evil in the perl
community? the main reason is that it can break code easily since it
doesn't properly (and nothing but perl can parse perl) parse the
code. perl syntax can change when you use certain modules (many examples
of this exist) and filtering won't know that and can wreak havoc.

so the big question is why do you want to do any source filtering and
even worse, do it for all loaded modules? there may be tricky ways to
filter all modules but i won't even touch that yet. i sense you have
found this neat little tool and think it will solve all your coding
problems. instead it will likely cause you much pain.

uri
 
S

sln

I want to apply a source filter, via say Filter::Simple, not just to a
particular source file, but to all of the source files that it may do, require,
or use. E.g., if I've built a source filter in mySourceFilter, then in

use mySourceFilter;
use custommodule;
print ...

the code that resides in the above source file will be filtered, but the code
that it reads in from custommodule.pm will not. This is what
http://perldoc.perl.org/perlfilter.html says, and my observation confirms it.

What I want is to modify the do, require, and use statements to automatically
apply mySourceFilter to every file they load. But I don't know how.

Anyone done this? Suggestions?
Thanks,
Andrew.

Hey, wave a magic wand, say presto chango, get magically transformed,
polymorphic code.

sln
 
A

Andrew Schulman

Hi, thanks for your answer.
AS> I want to apply a source filter, via say Filter::Simple, not just
AS> to a particular source file, but to all of the source files that
AS> it may do, require, or use.

did you know that source filtering is considered very evil in the perl
community?

No, I didn't.
the main reason is that it can break code easily since it
doesn't properly (and nothing but perl can parse perl) parse the
code. perl syntax can change when you use certain modules (many examples
of this exist) and filtering won't know that and can wreak havoc.

Hm... that's confusing to me, since none of the description that I read said
anything about parsing at all. It is a simple text filter, is it not? And
that's exactly what I want-- a textual filter that will make a small number of
well-defined, parameterized transformations on my code-- roughly speaking,

s/string1/$ENV{A}/g

No syntactical parsing is implied or desired.

Clearly I can modify my code to do this directly, but there are disadvantages to
that approach too, which are particular to my situation and which I won't go
into here. I'd like to understand the feasibility and drawbacks (aside from
performance, which is acceptable in a development environment) of a
comprehensive source code filtering approach.

Andrew.
 
T

Tad J McClellan

No, I didn't.


There is the primary drawback.

Source filtering is very likely to be the cause of bugs.

You will likely have enough bugs even without providing them
with an additional way to insert themselves... :)

Hm... that's confusing to me, since none of the description that I read said
anything about parsing at all.


Do you want to apply the changes to code AND to data?

If not, then you need to parse to know whether it appears in code or in data.

It is a simple text filter, is it not? And
that's exactly what I want-- a textual filter that will make a small number of
well-defined, parameterized transformations on my code-- roughly speaking,

s/string1/$ENV{A}/g

No syntactical parsing is implied or desired.


Perhaps you have not thought this through in enough detail...

Let's say that $ENV{A} = 'foobar';

You want all of these transformations then?

process_string1($arg) ==> process_foobar($arg)

process_string100($arg) ==> process_foobar00($arg)

my $arg = 'string100' ==> my $arg = 'foobar00'


You might say "but none of my code will have those situations".

However, you want to apply this to _all_ code.

Can you say that none of those situations occur in the other people's
code that you are use/require-ing?

Clearly I can modify my code to do this directly, but there are disadvantages to
that approach too, which are particular to my situation and which I won't go
into here.


Then we will be unable to suggest how to work around them without
resorting to the evil that is source code filtering.

I'd like to understand the feasibility and drawbacks (aside from
performance, which is acceptable in a development environment) of a
comprehensive source code filtering approach.


The drawback is in maintainability.
 
A

Andrew Schulman

Do you want to apply the changes to code AND to data?

Yes, that is fine because the changes are quite specific.
If not, then you need to parse to know whether it appears in code or in data.

Agreed that I don't want to parse. I wouldn't even know how to start.
Perhaps you have not thought this through in enough detail...

Let's say that $ENV{A} = 'foobar';

You want all of these transformations then?

process_string1($arg) ==> process_foobar($arg)

process_string100($arg) ==> process_foobar00($arg)

my $arg = 'string100' ==> my $arg = 'foobar00'

I'm aware that it can be hard to get these sorts of transformations right. I
accept responsibility for any resulting bugs, and promise not to sue, berate, or
even blame anyone except myself, nor to post any questions here resulting from
any code that's using source filtering.
Then we will be unable to suggest how to work around them without
resorting to the evil that is source code filtering.

That is okay. I know the alternatives and can weigh them here in the context of
our environment. But in order to do that I want to fully understand the source
filtering option, which is why I posted.

Thanks for your help so far. I do appreciate the stern warnings and will take
them to heart.

At this point I think it will be most useful for me to focus my question more
narrowly and try again in a new thread.

Thanks,
Andrew.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top