Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^4: How to simulate a preprocessor macro without one?

by AriSoft (Sexton)
on Feb 09, 2010 at 15:57 UTC ( [id://822217]=note: print w/replies, xml ) Need Help??


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

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.

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

Replies are listed 'Best First'.
Re^5: How to simulate a preprocessor macro without one?
by LanX (Saint) on Feb 09, 2010 at 17:23 UTC
    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...(!?!)

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

      That doesn't overload qq. It's already done its job by then

      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.

        ...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???

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (5)
As of 2024-04-23 22:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found