10

Consider the following expression:

Block[{Print = CopyToClipboard}, doSomething[]]

Now all calls to Print from doSomething[] (and all other functions it may invoke) will copy values to clipboard rather than printing them. Suppose I want to modify this expression such that all calls to Print both copy values to clipboard and print them.

Block[{Print = (CopyToClipboard[#]; Print[#]) &}, doSomething[]]

I need something similar to the above, but which doesn't go into an infinite recursion and where the occurrence of Print within the pure function retains its global meaning that it had before the execution entered the Block. Is it possible?

I understand the difference between Block and Module, and I need to use Block to create a dynamic (rather than lexical) scoping.

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
Vladimir Reshetnikov
  • 7,213
  • 27
  • 75

1 Answers1

9

What you are looking for is the Villegas - Gayley technique:

Internal`InheritedBlock[{Print},
    Unprotect[Print];
    Module[{inPrint},
       Print[arg_]/;!TrueQ[inPrint]:=
          Block[{inPrint = True},
              (CopyToClipboard[#]; Print[#])&[arg]
          ];
    ];
    your-code
]

Here, the Internal`InheritedBlock is used to make sure that the redefinition of Block remains local to your execution stack - it is also a dynamic scoping construct, but it preserves the old definitions, unlike Block.

Leonid Shifrin
  • 114,335
  • 15
  • 329
  • 420