3 File::Find::Rule::Extending - the mini-guide to extending File::Find::Rule
7 package File::Find::Rule::Random;
10 # take useful things from File::Find::Rule
11 use base 'File::Find::Rule';
13 # and force our crack into the main namespace
14 sub File::Find::Rule::random () {
15 my $self = shift()->_force_object;
16 $self->exec( sub { rand > 0.5 } );
23 File::Find::Rule went down so well with the buying public that
24 everyone wanted to add extra features. With the 0.07 release this
25 became a possibility, using the following conventions.
27 =head2 Declare your package
29 package File::Find::Rule::Random;
32 =head2 Inherit methods from File::Find::Rule
34 # take useful things from File::Find::Rule
35 use base 'File::Find::Rule';
37 =head3 Force your madness into the main package
39 # and force our crack into the main namespace
40 sub File::Find::Rule::random () {
41 my $self = shift()->_force_object;
42 $self->exec( sub { rand > 0.5 } );
46 Yes, we're being very cavalier here and defining things into the main
47 File::Find::Rule namespace. This is due to lack of imaginiation on my
48 part - I simply can't find a way for the functional and oo interface
49 to work without doing this or some kind of inheritance, and
50 inheritance stops you using two File::Find::Rule::Foo modules
53 For this reason try and pick distinct names for your extensions. If
54 this becomes a problem then I may institute a semi-official registry
57 =head2 Taking no arguments.
59 Note the null prototype on random. This is a cheat for the procedural
60 interface to know that your sub takes no arguments, and so allows this
63 find( random => in => '.' );
65 If you hadn't declared C<random> with a null prototype it would have
66 consumed C<in> as a parameter to it, then got all confused as it
67 doesn't know about a C<'.'> rule.
71 Richard Clamp <richardc@unixbeard.net>
75 Copyright (C) 2002 Richard Clamp. All Rights Reserved.
77 This module is free software; you can redistribute it and/or modify it
78 under the same terms as Perl itself.
84 L<File::Find::Rule::MMagic> was the first extension module, so maybe