-
Notifications
You must be signed in to change notification settings - Fork 581
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Combine monkey_patch and class_to_path in new Mojo::BaseUtil
- Loading branch information
Showing
10 changed files
with
108 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package Mojo::BaseUtil; | ||
# only using pure Perl as the only purpose of this module is to break a | ||
# circular dependency involving Mojo::Base | ||
use strict; | ||
use warnings; | ||
|
||
use Exporter qw(import); | ||
use Sub::Util qw(set_subname); | ||
|
||
our @EXPORT_OK = (qw(class_to_path monkey_patch)); | ||
|
||
|
||
sub class_to_path { join '.', join('/', split(/::|'/, shift)), 'pm' } | ||
|
||
sub monkey_patch { | ||
my ($class, %patch) = @_; | ||
no strict 'refs'; | ||
no warnings 'redefine'; | ||
*{"${class}::$_"} = set_subname("${class}::$_", $patch{$_}) for keys %patch; | ||
} | ||
|
||
1; | ||
|
||
=encoding utf8 | ||
=head1 NAME | ||
Mojo::BaseUtil - Common utility functions used in Mojo::Base, re-exported in Mojo::Util. | ||
=head1 SYNOPSIS | ||
use Mojo::BaseUtil qw(class_to_patch monkey_path); | ||
my $path = class_to_path 'Foo::Bar'; | ||
monkey_patch 'MyApp', foo => sub { say 'Foo!' }; | ||
=head1 DESCRIPTION | ||
L<Mojo::BaseUtil> provides a C<class_to_path> and the C<monkey_patch> function | ||
for L<Mojo>. The main purpose is to provide functions to both C<Mojo::Base> | ||
and C<Mojo::Util> so that C<Mojo::Base> does not have to load the rest of | ||
C<Mojo::Util>. | ||
=head1 FUNCTIONS | ||
L<Mojo::BaseUtil> only implements a limited number of functions. For forward | ||
compatibility it should still be imported individually. | ||
=head2 class_to_path | ||
my $path = class_to_path 'Foo::Bar'; | ||
Convert class name to path, as used by C<%INC>. | ||
# "Foo/Bar.pm" | ||
class_to_path 'Foo::Bar'; | ||
# "FooBar.pm" | ||
class_to_path 'FooBar'; | ||
=head2 monkey_patch | ||
monkey_patch $package, foo => sub {...}; | ||
monkey_patch $package, foo => sub {...}, bar => sub {...}; | ||
Monkey patch functions into package. | ||
monkey_patch 'MyApp', | ||
one => sub { say 'One!' }, | ||
two => sub { say 'Two!' }, | ||
three => sub { say 'Three!' }; | ||
=head1 SEE ALSO | ||
L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>. | ||
=cut |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters