NAME

Data::Fetch - Prime method calls for background execution using threads

VERSION

Version 0.07

SYNOPSIS

This module allows you to prepare time-consuming method calls in advance. It runs those methods in background threads so that when you later need the result, you don't need to wait for it to compute.

use CalculatePi;
use Data::Fetch;

my $fetcher = Data::Fetch->new();
my $pi = CalculatePi->new(places => 1_000_000);

# Prime the method call in the background
$fetcher->prime(
    object  => $pi,
    message => 'as_string',
    arg     => undef  # Optional
);

# Do other work here...

# Retrieve the result later (waits only if it hasn't completed yet)
my $value = $fetcher->get(
    object  => $pi,
    message => 'as_string',
    arg     => undef
);

print $value, "\n";

DESCRIPTION

Some method calls are expensive, such as generating a large dataset or performing heavy computations. If you know in advance that you'll need the result later, Data::Fetch lets you prime that method call so it begins running in a background thread.

When you later call get, the value is returned immediately if ready - or you'll wait for it to finish.

SUBROUTINES/METHODS

new

my $fetcher = Data::Fetch->new();

Constructs and returns a new Data::Fetch object.

prime

$fetcher->prime(
    object  => $obj,
    message => 'method_name',
    arg     => $args_ref    # Optional
);

Starts a background thread that will call the given method on the object.

Takes the following parameters:

  • object

    The object on which the method will be invoked.

  • message

    The name of the method to call.

  • arg

    (Optional) The arguments to pass to the method. This must be:

    - A scalar - An arrayref of positional arguments - A hashref of named arguments

    The arguments are passed to the method using Perl's standard @_ behavior:

    $obj->$method(@$args)      # if arg is an arrayref
    $obj->$method(%$args)      # if arg is a hashref
    $obj->$method($arg)        # otherwise

If called in list context, the method result will be stored and returned in list context when retrieved via get().

get

my $value = $fetcher->get(
    object  => $obj,
    message => 'method_name',
    arg     => $arg         # Optional
);

Retrieves the result of a previously primed method. If the background thread is still running, it will wait for it to finish. If the method wasn't primed, it will call the method directly and cache the result.

get can be called in list or scalar context, depending on what the method returns. If the method returns a list, use:

my @list = $fetcher->get(...);

AUTHOR

Nigel Horne, <njh at nigelhorne.com>

BUGS

Can't pass more than one argument to the message.

I would not advise using this to call messages that change values in the object.

Changing a value between prime and get will not necessarily get you the data you want. That's the way it works and isn't going to change.

If you change a value between two calls of get(), the earlier value is always used. This is definitely a feature not a bug.

Please report any bugs or feature requests to bug-data-fetch at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Data-Fetch. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

See http://www.cpantesters.org/cpan/report/116390147. This code could produce the "attempt to prime twice" if you're unlucky and Perl assigns the same address to the new object as the old object.

my $fetch = Data::Fetch->new();
my $data = Class::Simple->new();
$fetch->prime(object => $data, message => 'get');
$fetch->get(object => $data, message => 'get');
$data = Class::Simple->new();	# Possibly the address of $data isn't changed
$fetch->prime(object => $data, message => 'get');	# <<<< This could produce the error

SEE ALSO

SUPPORT

This module is provided as-is without any warranty.

You can find documentation for this module with the perldoc command.

perldoc Data::Fetch

You can also look for information at:

LICENSE AND COPYRIGHT

Copyright 2010-2025 Nigel Horne.

This program is released under the following licence: GPL2