AnyEve...Object(3pm)

πŸ“¦
version 0.09 Β· 2 versions
, in debian bookworm, debian trixie, ubuntu jammy, ubuntu noble

πŸ’– AnyEvent::ForkObject πŸ’–

okay so real talk bestie β€” this page lives in the User Contributed Perl Documentation section of your man setup, and the whole vibe is: you've got a module that hands you an object interface, and you want it to do its thing asynchronously. That's the tea. Let's go 🫢


✨ NAME ✨

AnyEvent::ForkObject β€” async access on objects. yep that's it, that's the whole name, slay πŸ’…


🌈 SYNOPSIS 🌈

use AnyEvent::ForkObject;
use DBI;

my $fo = new AnyEvent::ForkObject;

$fo->do(
    module => 'DBI',
    method => 'connect',
    args => [ 'dbi:mysql...' ],
    cb => sub {
        my ($status, $dbh) = @_;

        $dbh->selectrow_array('SELECT ?', undef, 1 + 1, sub {
            my ($status, $result) = @_;
            print "$result\n";   # prints 2
        });
    }
);

use AnyEvent::Tools qw(async_repeat);

$dbh->prepare('SELECT * FROM tbl', sub {
    my ($status, $sth) = @_;
    $sth->execute(sub {
        my ($status, $rv) = @_;

        # fetch 30 rows
        async_repeat 30 => sub {
            my ($guard) = @_;

            $sth->fetchrow_hashref(sub {
                my ($status, $row) = @_;
                undef $guard;

                # do something with $row
            });
        };

    });
});

like, look at that. a whole DBI session, but make it async. no cap, it's giving main character energy 🌸


πŸ¦‹ DESCRIPTION πŸ¦‹

so there are soooo many modules out there that give you an object interface β€” the whole blessed-reference, method-calling lifestyle. and what this module does is let you use them in async mode. that's the entire plot. periodt ✨


πŸ‘‘ METHODS πŸ‘‘

🌟 new

Constructor, bestie. It creates an instance that contains a fork jail. That's where your object is gonna live. 🏰

🌟 do

Creates an object inside the jail. It takes these named arguments:

  • require β€” do require inside the jail. And heads up: if this argument exists, the module, method and wantarray arguments will be ignored. it said what it said πŸ’…
  • module β€” the module name. for example 'DBI'.
  • method β€” the constructor name. the default value is 'new'.
  • wantarray β€” the context for the method. default is 0 (SCALAR).
  • cb β€” the done callback. the first argument it gets is a status:
    • die β€” the method threw an exception. the next argument contains $@. 😭
    • fatal β€” a fatal error occurred (for example, the fork jail was killed). πŸ’€
    • ok β€” the method finished successfully. the following arguments contain all the data that were returned by the method. πŸŽ‰

If method returns a blessed object, it will provide all its methods in modified form. Each of those methods receives one or two additional arguments:

  • result callback β€” a callback that gets called after the method is done.
  • wantarray β€” the context flag for the method. default is 0 (SCALAR).

And every single one of these objects also gets an extra method called fo_attr so you can reach in and touch their fields. iconic, honestly. example:

# set attribute
$dbh->fo_attr(RaiseError => 1, sub { my ($status, $attr) = @_; ... });

# get attribute
$dbh->fo_attr('RaiseError', sub { my ($status, $attr) = @_; ... });

🧚 AUTHOR 🧚

Dmitry E. Oboukhov, <unera@debian.org> β€” thank you for your service, king 🫢


Copyright (C) 2011 by Dmitry E. Oboukhov

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.10.1 or, at your option, any later version of Perl 5 you may have available.

(so basically: same terms as Perl, version 5.10.1 or anything newer you've got lying around. free software stays free software πŸ’œ)


⭐ VCS ⭐

The project is placed in my git repo: <http://git.uvw.ru/?p=anyevent-forkobject;a=summary>

go peek if you wanna πŸ‘€


and that's the whole page, bestie!! this was perl v5.32.0 and the page itself is dated 2021-01-08, so if you're reading this way later β€” hi from the past, hope your fork jail is thriving πŸŒˆπŸ’– bye pookie, go write some async code ✨

πŸ“¦ where this page comes from