in reply to Re^5: How to simulate a preprocessor macro without one?
in thread How to simulate a preprocessor macro without one?

Your example printed the return value of the test() but I didn't see any lexical changes to happen.

The function I am now looking for, should be able to access the callers lexicals same way as qq operator does, to be able to create other new (better) ways to interpolate strings. Or, as in my original case, I would use callers local variables with the template string from the same caller. You should think the qq as a small template processor function to see the problem in my viewpoint.

If my goal is possible achieve it also means that you can write new functions which gives allmost same effect as macro proprocessors functions does in C.

  • Comment on Re^6: How to simulate a preprocessor macro without one?

Replies are listed 'Best First'.
Re^7: How to simulate a preprocessor macro without one?
by LanX (Saint) on Feb 09, 2010 at 22:06 UTC
    ...to see the problem in my viewpoint.

    sorry I have no real idea what you want...

    Cheers Rolf

    UPDATE: I'm speculating you don't want to "overload qq" but simulate operators working on variable names.

    So please explain, what's the problem with PadWalker (as already suggested) for lexical vars and (if necessary) inspecting the stash (symbol table hash) for package vars???

      Hello again Monks, Thank you for your advices. I made my program with padwalker and it worked but there was some problems. The solution was not optimal in some aspects like speed and memory consumption. Also the solution was ugly.

      Now I see that my question directed the issue in the wrong direction. Actually I needed two separate tools. One tool for accessing lexical variables from the caller and an another tool to simplify the resulting code after the first problem is solved.

      This is the idea how the template program peeks inside the callers variables.

      sub template { my $uplevel = shift; $uplevel->('print $x;'); $uplevel->('print $y;'); } my $x = 'X'; for my $y (1 .. 3) { template( sub{eval(shift)} ); };
      Now - only the second problem needs to be solved. How to write  template( sub{eval(shift)} ); somehow shorter way?