Callback(3)

πŸ“¦
version 1.07 Β· 2 versions
, in fedora 43

πŸ’– Callback β€” the manual page, but make it slay πŸ’–

okay so bestie, welcome to Callback(3), a page from the User Contributed Perl Documentation section. yes it's a real manpage, yes it's about function callbacks, and yes i'm about to make it make sense. buckle up πŸŽ€


✨ NAME ✨

Callback β€” an object interface for function callbacks 🫢


🌸 SYNOPSIS 🌸

        use Callback;

        my $callback = new Callback (\&myfunc, @myargs);
        my $callback = new Callback ($myobj, $mymethod, @myargs);
        my $callback = new Callback ($old_callback, @myargs);

        $callback->call(@some_more_args);

that's the whole vibe right there, no cap ✨


πŸ¦‹ DESCRIPTION πŸ¦‹

so like, Callback gives you one standard interface for registering callbacks. and those callbacks can be one of two flavors:

  • purely functional β€” literally just a function call with arguments
  • object-oriented β€” a method call on an object

when you construct a callback, you can hand it a base set of arguments. those function arguments will come before any arguments you add at the moment you actually make the call. so the OG args go first, the new ones follow. periodt πŸ’…

now, there are two forms of the callback constructor, and which one you get depends on whether the call is a pure functional call or a method call. the rule is simple, bestie:

if the first argument is an object, then the second argument is a method name to be called on that object. 🫢

method resolution happens at the time the Callback object is built. and if it can't find that method? an error will be raised. 😭 it's not gonna wait around and hope for the best, it's gonna tell you immediately.

Callback objects built for object-oriented calls also have the property of being serializable via Storable. ✨ but purely functional callbacks? those cannot be serialized, because CODE references are not supported by Storable. πŸ’€ so don't even try it, that's just how it is.

oh and also β€” Callback objects can be created from existing Callback objects πŸ‘‘. any arguments you pass will be appended onto the original list of arguments.


πŸ”₯ TRACING πŸ”₯

        use Callback qw(@callbackTrace);

if you're out here writing a debugging routine that provides a stack-dump β€” like for example Carp::confess β€” it is genuinely useful to know where a callback was registered. iykyk 🧚

        my $ct = 0;
        while (($package, $file, $line, $subname, $hasargs, $wantarray) = caller($i++)) {
            ...

            if ($subname eq 'Callback::call') {
                print "callback registered $Callback::callbackTrace[$ct]\n";
                $ct++;
            }
        }

and without code like that? it becomes very hard to know what's going on. 😭 like, main character energy lost in the sauce. so use the trace, bestie.


Copyright (C) 1994, 2000, 2002 David Muir Sharnoff. All rights reserved. This module may be licensed on the same terms as Perl itself.


πŸ‘‘ AUTHORS πŸ‘‘

David Muir Sharnoff <muir@idiom.com> and Raphael Manfredi <Raphael_Manfredi@pobox.com>


🌈 SEE ALSO 🌈

  • Storable(3). β€” go read that one next, fr fr πŸ’–

and that's the whole page, bestie!! πŸŽ€ this was Callback(3), rendered by perl v5.42.0 and dated 2025-07-25. stay slay, go call your callbacks responsibly πŸ’…βœ¨πŸ¦‹

πŸ“¦ where this page comes from