Web::Machine::Util(3pm) | User Contributed Perl Documentation | Web::Machine::Util(3pm) |
Web::Machine::Util - General Utility module
version 0.17
use Web::Machine::Util;
This is just a basic utility module used internally by Web::Machine. There are no real user serviceable parts in here.
if ( my $id = bind_path( '/:id', $request->path_info ) ) { # handle the case with an ID here } else { # handle other cases here }
The $path_spec follows a pretty standard convention. Literal path parts must match corresponding literal. Variable path parts are prefixed by a colon and are captured for returning later, if a question mark (?) prefixes the colon, that element will be considered optional. And lastly the "splat" operator ("*") is supported and causes all the rest of the path segments to be returned. Below are a few examples of this:
spec path result ------------------------------------------------------------ /test/:foo/:bar /test/1/2 ( 1, 2 ) /test/:foo/:bar /test/1/ undef #failure-case /test/* /test/1/2/3 ( 1, 2, 3 ) /user/:id/:action /user/1/edit ( 1, 'edit' ) /?:id /201 ( 201 ) /?:id / ( )
This function is kept deliberately simple and it is expected that the user will use "my" in the array form to assign multiple variables, like this:
my ( $foo, $bar ) = bind_path( '/test/:foo/:bar', $path );
In the future we might add a "bind_path_hash" function which captures the variable names as well, but to be honest, if you feel you need that, you likely want one of the many excellent path dispatching modules available on CPAN.
NOTE: Some care should be taken when using path specs in which the only things are either optional parameters (prefixed with "?:") or the "splat" operator ("*") as they can return empty arrays, which in certain contexts can look like match failure. In these cases you can test the match in scalar context to verify, a match failure will be "undef" whereas a match success (in which nothing was matched) will return 0 (indicating an array with zero size).
bugs may be submitted through <https://github.com/houseabsolute/webmachine-perl/issues>.
This software is copyright (c) 2016 by Infinity Interactive, Inc.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
2017-01-26 | perl v5.24.1 |