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

Re: Macros, LFSPs and LFMs

by BrowserUk (Pope)
on Jun 11, 2003 at 19:23 UTC ( #265146=note: print w/ replies, xml ) Need Help??


in reply to Macros, LFSPs and LFMs

The "should we, shouldn't we" debate seems to be yet another sterile academic debate from my perspective, but maybe I'm missing something. Whilst I remember making a few rods for my own back with the C macro facility, mostly related to writing macros that had side-effects, I have also seen them put to very good use.

Does anyone with a strong opinion against adding macro facilities care to give a few reasons?

On the other hand, I'd also like to see a few examples of what people would use a macro facility for in Perl. Assuming that p5 had a C-like

#define name(arg[,arg]) {\ some code here\ }

I think that's roughly the syntax

What would you use it for?


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller



Comment on Re: Macros, LFSPs and LFMs
Download Code
Re: Re: Macros, LFSPs and LFMs
by mirod (Canon) on Jun 11, 2003 at 19:30 UTC

      Damrn it! You mean now I learnt perl I gotta learn french as well? :)


      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller


        Hey, don't laugh, a whole bunch of French Comp-Sci students learnt first a language named LSE (link in French, of course!) and its ancestor LSD (! For Langage Sans Difficulté - Painless Language) which was a cross between Pascal and Basic, using French keywords!

        Damrn it! You mean now I learnt perl I gotta learn french as well? :)

        Completely OT...

        A few years ago I had to write the set-top box code for an interactive TV programme. In French. An example:

        debut lecture reaction nulle @10:00:46:03 fin lecture; si BOUTON=2 alors CANAL=3

        There was no paper documentation for the box available to me. I was in the UK and the people who knew the system were few timezones away in (French speaking parts of) Canada. We could only syntax check the code locally, not having the hardware to run it on site.

        Of course being a product of the English educational system I can't speak French. In fact I managed to fail Graded Test French (the lowest format of exam available at the time) getting less on the multiple-choice questions than I would have if I had guessed at random.

        Gosh that was a fun project (actually - it went surprisingly well :-)

Re: Re: Macros, LFSPs and LFMs
by adrianh (Chancellor) on Jun 11, 2003 at 22:20 UTC

    On the other hand, I'd also like to see a few examples of what people would use a macro facility for in Perl. Assuming that p5 had a C-like

    #define name(arg[,arg]) {\ some code here\ }

    I wouldn't use it for C-like purposes. C-like macros are not that powerful - you can only do basic textual substitutions. To quote Synopsis 6:

    Macros (keyword: macro) are routines whose calls execute as soon as they are parsed (i.e. at compile-time). Macros may return another source code string or a parse-tree.

    Returning a parse-tree is where it gets interesting. What we get here are LISPish macros. We'll be able to write code that's run at compile time to create our own syntax.

    Want a switch statement in Perl6? Just write one.

    Want to add AOP or Design by Contract support to Perl6? Just write some new syntax.

    Want to create a domain-specific language so you can write code that more directly reflects the problem domain? Yes - you guessed it - just write some new syntax!

    There's a discussion of macros on perl.perl6.language that you might find interesting. Also read the relevant bit of Apocalypse 6.

    Macros - can't wait ;-)

    Update: Just come across A Macro System for Perl?, which has some relevant material for those who are interested.

      Thanks muchly for that. Never having done anything with Lispish languages, much less it's macro facilities, I've never really encountered macros in this form.

      The perl.perl6.language thread was particularly useful. I'd read that bit on macros in Apo.6, but hidden amongst so much else, I think I must have closed my eyes and held my breath as it washed over me. Time to go back and read itagain. I think that LW summed up the pro's and cons of macros in this form with two statements--that probably shouldn't be taken out of context, but will serve me as anchors in my grep for further info and an opinion.

      This is the most dangerous kind of return value, and the least likely to produce coherent error messages with decent line numbers for the end user. But it's also very powerful. Hee, hee.

      A lot of Perl is fun, and macros are fun, but in general, you should never use a macro just for the fun of it. It's far too easy to poke someone's eye out with a macro.

      And as far as Macros - can't wait ;-) goes--I probably agree, but I'll reserve judgement until I form an opinion. This is one of those things that I find very difficult to grasp the significance of at a purely theoretical level, a couple of practical uses will probably convince me.

      I tend to be pretty impirical by nature. That's probably why I have such a hard time with pure math once you get beyond stuff you can at least model with a few empty loo-roll holders and some sticky-back plastic. I have vague memories of something about a blind French mathematican that proved that you can turn a sphere inside out (topologicaly speaking) without breaking the surface. I concluded that you would have to be blind to even concieve of such a notion:)


      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller


        Well, you know all these attempts to add syntax to Perl5 using sub(&;)-ish prototypes? (Like try { ... } catch { ... }; which look like keywords but are really function calls.) That device is very limited and carries risks of leaking accidentally created closures which makes it dangerous to use in any persistent environment. In Perl6, even if the language didn't already have these, it would be easy to write macros that transform this into code behaving exactly as if they were real keywords.

        Or consider the Acme::Don't module and esp Simon Cozen's take at it; this would be trivial to implement with a Perl6 macro.

        Then we have cases like the Test modules. It would be nice if there was a way for ok() to actually "understandd" the code of the test being evaluated, so we could just write ok($foo eq $bar, 'calculation [...]') - currently, in order to get the actual test described in the failure message you must use a specialized derivative function such as is($foo, $bar, 'calculation results should equal safety check'). Instead of having to learn all the testing modules and the tests they provide (and hunt around for the module that implements the kind of test you want or eventually rolling your own), you just let a macro figure out how to DWIM with Perl code, whose syntax you presumably already know well.

        Or PDL. It basically adds a whole new sublanguage to Perl for mass numbercrunching. In many cases it has to try to fit a square peg into a round hole in order to somehow map a mathematically oriented notation into Perl syntax in a semi intuitive fashion. With true macros, it could actually add new definitions of mathematically oriented syntax to the language.

        And these are only the most obvious examples because they already attempt to recast Perl5 syntax for a specific subject. Probably any very deep but narrowly defined programming field can likely benefit from casting its most basic premises into actual, additional supporting syntax for the language.

        Makeshifts last the longest.

        Another application that just popped into my mind - albeit not one that necessarily needs the full power of LISPish macros: real assert()s. And while they don't need it, they too can exploit the full power of macros, by being able to grok the assertion code similar to the way I outlined for the ok() function in tests.

        Makeshifts last the longest.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (9)
As of 2014-07-23 06:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (133 votes), past polls