in reply to Re^2: Pass an "extra' variable to Find::File subroutine
in thread Pass an "extra' variable to Find::File subroutine

You'd design the C library differently.
  • Comment on Re^3: Pass an "extra' variable to Find::File subroutine

Replies are listed 'Best First'.
Re^4: Pass an "extra' variable to Find::File subroutine
by moritz (Cardinal) on Jun 22, 2010 at 16:03 UTC
    Would I?

    It's a common pattern in C libraries to pass a void pointer to callbacks, and allow the caller of the library to hand in a pointer which is then passed on to the callback.

    And that's a rather sane design in a language without closures, because otherwise the the callback can't obtain additional information about the caller (except for globals, which makes rekursive calls dangerous to impossible).

    Other popular design decision include a hidden, global state variable (same problem with reentrancy), or omitting callbacks altogether (might work for a directory traversal library like File::Find, but would be exceptionally painful for a GUI toolkit).

      Would I?

      You ask this, then proceed to explain exactly what you'd change: The addition of a parameter that takes a pointer to pass to the callback. You answered your own question.