/usr/share/perl5/Web/Machine/Util.pm is in libweb-machine-perl 0.17-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 | package Web::Machine::Util;
# ABSTRACT: General Utility module
use strict;
use warnings;
our $VERSION = '0.17';
use Carp qw[ confess ];
use Scalar::Util qw[ blessed ];
use List::Util qw[ first ];
use HTTP::Headers::ActionPack 0.07;
use Sub::Exporter -setup => {
exports => [qw[
first
pair_key
pair_value
bind_path
create_date
create_header
inflate_headers
]]
};
sub pair_key { ( keys %{ $_[0] } )[0] }
sub pair_value { ( values %{ $_[0] } )[0] }
{
my $ACTION_PACK = HTTP::Headers::ActionPack->new;
sub create_header { $ACTION_PACK->create( @_ ) }
sub create_date { $ACTION_PACK->create( 'DateHeader' => shift ) }
sub inflate_headers { $ACTION_PACK->inflate( @_ ) }
sub get_action_pack { $ACTION_PACK }
}
sub bind_path {
my ($spec, $path) = @_;
my @parts = grep { defined $_ && $_ ne q{} } split /\// => $path;
my @spec = grep { defined $_ && $_ ne q{} } split /\// => $spec;
my @results;
foreach my $i ( 0 .. $#spec ) {
if ( $spec[ $i ] =~ /^\*$/ ) {
push @results => @parts;
@parts = ();
last;
}
elsif ( $spec[ $i ] =~ /^\:/ ) {
return unless defined $parts[ 0 ];
push @results => shift @parts;
}
elsif ( $spec[ $i ] =~ /^\?\:/ ) {
push @results => shift @parts
if defined $parts[ 0 ];
}
else {
return unless defined $parts[ 0 ];
return unless $spec[ $i ] eq $parts[ 0 ];
shift @parts;
}
}
return if @parts;
wantarray
? @results
: (scalar @results == 1)
? $results[0]
: @results;
}
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
Web::Machine::Util - General Utility module
=head1 VERSION
version 0.17
=head1 SYNOPSIS
use Web::Machine::Util;
=head1 DESCRIPTION
This is just a basic utility module used internally by
L<Web::Machine>. There are no real user serviceable parts
in here.
=for Pod::Coverage get_action_pack
=head1 FUNCTIONS
=over 4
=item C<first>
This is imported from L<List::Util> and passed on here
for export.
=item C<pair_key>
=item C<pair_value>
These two functions are used for fetching the key
and value out of a pair in the L<Web::Machine> internals.
We represent a pair simply as a HASH ref with one key.
=item C<inflate_headers( $request )>
This will call C<inflate> on an instance of L<HTTP::Headers::ActionPack>.
=item C<create_header( @args )>
This will call C<create> on an instance of L<HTTP::Headers::ActionPack>.
=item C<create_date( $date_string | $time_peice )>
Given either a C<$date_string> or an instance of L<Time::Piece>,
this will inflate it into a L<HTTP::Headers::ActionPack::DateHeader>
object, suitable for use in the FSM.
=item C<bind_path( $path_spec, $path )>
Given a C<$path_spec> (described below) and a C<$path>, this will
either bind the path to the spec and return and array of bound
values, or it will return nothing. Returning nothing indicates
that no match was found. Additionally, if this function is called
in scalar context, and there is only one match, it will return
that item. Otherwise it will return the array as normal. This all
makes it easy to use the following idiom:
if ( my $id = bind_path( '/:id', $request->path_info ) ) {
# handle the case with an ID here
}
else {
# handle other cases here
}
The C<$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 (C<*>) 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 C<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 C<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.
B<NOTE:> Some care should be taken when using path specs in
which the only things are either optional parameters
(prefixed with C<?:>) or the "splat" operator (C<*>)
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 C<undef> whereas a match success (in
which nothing was matched) will return C<0> (indicating
an array with zero size).
=back
=head1 SUPPORT
bugs may be submitted through L<https://github.com/houseabsolute/webmachine-perl/issues>.
=head1 AUTHORS
=over 4
=item *
Stevan Little <stevan@cpan.org>
=item *
Dave Rolsky <autarch@urth.org>
=back
=head1 COPYRIGHT AND LICENCE
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.
=cut
|