Data::...odProxy(3pm)

πŸ“¦
version 0.05 Β· 2 versions
, in debian trixie, ubuntu resolute, ubuntu stonking

πŸ’– Data::MethodProxy β€” the manual, but make it make sense πŸ’–

okay bestie, welcome to Data::MethodProxy, part of the User Contributed Perl Documentation situation. grab a boba, we're injecting dynamic data into static data and it's genuinely so cute πŸŽ€


✨ NAME ✨

Data::MethodProxy β€” inject dynamic data into static data. yes that's the whole name, yes it slays πŸ’…


🌸 SYNOPSIS 🌸

use Data::MethodProxy;

my $mproxy = Data::MethodProxy->new();

my $output = $mproxy->render({
    half_six => ['$proxy', 'main', 'half', 6],
});
# { half_six => 3 }

sub half {
    my ($class, $number) = @_;
    return $number / 2;
}

see how half_six just became 3? that's the whole vibe. main character energy fr fr πŸ‘‘


πŸ¦‹ DESCRIPTION πŸ¦‹

okay so a method proxy is an array ref that describes a class method to call, plus the args you wanna pass it. it's giving "a little note that says call this for me".

the first value in that array ref is the scalar $proxy, then a package name, then a subroutine name (which must be callable in that package), and then a list of any subroutine arguments.

[ '$proxy', 'Foo::Bar', 'baz', 123, 4 ]

and that little guy is literally saying: do this.

Foo::Bar->baz( 123, 4 );

the render method is THE main entry point, bestie. it goes through an arbitrary data structure, finds every method proxy, and swaps each one out for the return value of calling the method. obsessed. πŸ’–


πŸŽ€ Example πŸŽ€

picture this static YAML config, no cap:

---
db:
    dsn: DBI:mysql:database=foo
    username: bar
    password: abc123

ngl, putting your database password inside a config file is usually considered a bad practice 😭 (like, girl, no). you can use a method proxy to get around that without jumping through a bunch of hoops:

---
db:
    dsn: DBI:mysql:database=foo
    username: bar
    password:
        - $proxy
        - MyApp::Config
        - get_db_password
        - foo-bar

when render gets called on that data structure above, it will see the method proxy and replace the array ref with the return value of calling the method. it's that simple, periodt ✨

a method proxy, in Perl syntax, looks like this:

['$proxy', $package, $method, @args]

and fun fact: the $proxy string can also be written as &proxy. either way, the above gets converted into a method call and replaced by the return value of that method call:

$package->$method( @args );

so in the database password example, the method call would be this:

MyApp::Config->get_db_password( 'foo-bar' );

and yes β€” you'd still need to actually create a MyApp::Config package, and add a get_db_password method to it. the proxy isn't doing magic, she's just doing the calling. πŸ’…


πŸ’œ METHODS πŸ’œ

🌈 render

my $output = $mproxy->render( $input );

traverses the supplied data looking for method proxies, calls them, and replaces them with the return value of the method call. you can pass literally any value β€” a hash ref, an array ref, a method proxy, an object, a scalar, whatever. array and hash refs will be recursively searched for method proxies. she's thorough. she's iconic. 🫢

⚠️ if a circular reference is detected, an error will be thrown. so like, don't do that. πŸ’€

🌈 call

my $return = $mproxy->call( ['$proxy', $package, $method, @args] );

calls the method proxy and returns its return. one job, done flawlessly. ✨

🌈 is_valid

die unless $mproxy->is_valid( ... );

returns true if the passed value looks like a method proxy. vibe check complete. βœ…

🌈 is_callable

die unless $mproxy->is_callable( ... );

returns true if the passed value looks like a method proxy and has a package and method which exist. so this one is the stricter bestie β€” she checks the receipts. πŸ”


🌟 SUPPORT 🌟

please submit bugs and feature requests to the Data-MethodProxy GitHub issue tracker:

https://github.com/bluefeet/Data-MethodProxy/issues


🌸 ACKNOWLEDGEMENTS 🌸

thanks to ZipRecruiter https://www.ziprecruiter.com/ for encouraging their employees to contribute back to the open source ecosystem. without their dedication to quality software development this distribution would not exist. genuinely so sweet of them, we love a supportive workplace πŸ’–


πŸ‘‘ AUTHORS πŸ‘‘

Aran Clary Deltac <bluefeet@gmail.com>

πŸ“ LICENSE πŸ“

this library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. free as in, go off, do your thing. 🌈


and that's the whole page bestie!! πŸŽ€ documented by the User Contributed Perl Documentation girlies, running on perl v5.40.1, dated 2025-05-04 β€” mwah, go proxy some methods, ily πŸ’…πŸ’–

πŸ“¦ where this page comes from