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

I have studied this issue a little more. I found that practically I do not need an ordinary (source manipulating) macro function at this case. In my case I need an access to the lexical scope of the callers environment to eval string type closures as they would exist inside the callers code.

Is there any practical method to change the lexical scope temporarily? caller() will give you some access to the callers properties but I think I need more.

sub foo { my $name="FOO"; bar("$name"); } sub bar { my $name="BAR"; print "My name is $name"; #My name is BAR function_to_do_something_using_callers_lexical_view { print "Your name is $name"; #Your name is FOO } } foo();

If there is no such function, as I fear, would it be "easy" or even possible to make a plugin which could install this kind of new core function to the interpreter? Or should I make my own Perl release ;-)

Replies are listed 'Best First'.
Re^3: How to simulate a preprocessor macro without one?
by LanX (Saint) on Feb 09, 2010 at 12:34 UTC
    > In my case I need an access to the lexical scope of the callers environment to eval string type closures as they would exist inside the callers code.

    PadWalker

    > but I think I need more.

    I think you need to rethink your questions... ;)

    Cheers Rolf

      Ok, here is my question rethinked: How could I overload the qq operator?

      With overloaded qq operator I could fully control the interpolation. For example, I could interpolate -> operators which are not available in the standard interpolation.

        How could I overload the qq operator?

        By using overload and overloading q for the constant string parts and the "."-operator for the string concatenations in between.

        Cheers Rolf

        UPDATE: If this whole thread is just about interpolating method-calls in strings/templates, you should consider the De/Ref-trick with @{[...]} :

        perl -e ' {package test; sub test {return "test"}}; $a=[]; bless $a, test; print "@{[$a->test]}" '

        please note: here $a->test is called in list context, for scalar context use ${\$a->test} !

        UPDATE2: After rereading overload I have the impression that overloading "" might be the approach to overload qq ... but this might also affect all other possible stringifications...(!?!)