[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Source filters



At 5:14 PM -0700 5/21/03, Michael Vanier wrote:
>Much as I hate to compliment perl ;-) ;-), that's pretty effing amazing.  I
>wonder what the original motivation was for this feature.  I can't imagine
>there was that much call for latin (or pig-latin, or esperanto...).  In
>some ways this takes the macro concept to its logical extreme (although I
>doubt it's as easy to manipulate code as it is in lisp/scheme macros).

The original point of this was to allow for things like compressed 
source files and tarred or zipped libraries. (The one downside to 
CPAN is a great temptation to use a module from it, which leads to a 
few more, and they use more, and then you find distributing programs 
somewhat... interesting)

As you can see, things didn't quite turn out that way. The latin 
module was done to demonstrate the power of source filters. (The 
Klingon module was done for fun)

Whether it's as powerful as macros is up for grabs. On the one hand, 
the input is generally significantly more syntactically complex than 
Lisp is, so you have to do a lot more work. On the other hand, 
there's sufficient control that neither the input nor the output has 
to be anything resembling perl. (The Inline modules combine source 
filters and some other tricks to allow embedded languages--C, Python, 
Ruby, Parrot assemlber, Befunge--in perl code

>Do any other languages have this feature?

I think Python and Ruby might have source filters, but I'm not sure. 
It's sort of akin to C's preprocessor, only it fails to suck. :)

>Mike
>
>>  From: Jasvir Nagra <jas@cs.arizona.edu>
>>  Date: Wed, 21 May 2003 09:42:59 -0700
>>
>>  I am lowly perl minion and this is a question better addressed by the
>>  gods, however:
>>
>>  Perl has source filters.  This is a special kind of filter that
>>  modifies the source after it is loaded into memory and before it
>>  reaches the parser.  One can do whatever translation one wishes
>>  provided the translation results in correct perl code.
>>
>>  So modules such as the following exist:
>>
>>  use Filter::cpp;
>>  #define FOO 1
>>  #ifdef FOO
>>  print "fooing";
>>  #else
>>  print "baring";
>>  #endif
>>
>>  or
>>
>>  use Rot13;
>>  cevag "uryyb serq\a" ;
>>
>>  A source filter that provides a scheme like syntax for perl would be
>>  straightforward if one does not in fact exist already.
>>
>>  /jas
>>
>>  Michael Vanier <mvanier@cs.caltech.edu> writes:
>>
>>  > I'm glad you brought this up.  For those of us who aren't perl gurus, can
>>  > you please explain to us in as little space as possible how the hell perl
>>  > does this kind of thing?  Is the perl lexer/parser dynamically hackable or
>>  > something?  Would it be possible to write a scheme-like syntax for perl if
>>  > one was so inclined?  If not, where are the lines drawn?
>>  >
>>  > I'm wondering if perl has the same kind of power as, say, ocamlp4 gives to
>>  > ocaml, or if it's doing something completely different.
>>  >
>>  > Really curious,
>>  >
>>  > Mike
>>  >
>>  >> From: Jasvir Nagra <jas@cs.arizona.edu>
>>  >> Date: Wed, 21 May 2003 08:43:22 -0700
>>  >>
>>  >> Dan Sugalski <dan@sidhe.org> writes:
>>  >>
>>  >> > 2) If the parser saw 1 + three in the code stream it wouldn't do the
>>  >> > math, so I'd generally assume that 1 + "three" wouldn't either.
>>  >> >
>>  >> > Both statements are language-local--I wouldn't be surprised to find a
>>  >> > language that allowed "three" either as a string constant or bareword
>>  >> > to translate to the number three. I'd expect it wouldn't be as useful
>>  >>
>>  >> #!/usr/bin/perl -l
>>  >>
>>  >> use Lingua::Romana::Perligata;
>>  >>
>>  >> addementum tres tum 1 egresso scribe.
>>  >> addementum tres tum unum egresso scribe.
>>  >> addementum 3 tum 1 egresso scribe.
>>  >>
>>  >> --
>>  >> Jasvir Nagra
>>  >> http://www.cs.auckland.ac.nz/~jas
>>  >>
>>  >
>>
>>  --
>>  Jasvir Nagra
>>  http://www.cs.auckland.ac.nz/~jas
>>


-- 
                                         Dan

--------------------------------------"it's like this"-------------------
Dan Sugalski                          even samurai
dan@sidhe.org                         have teddy bears and even
                                       teddy bears get drunk