2 with - Lexically call methods with a default object.
10 sub new { my $class = shift; bless { id = > shift }, $class }
12 sub hlagh { my $self = shift; print "Deuce::hlagh $self->{id}\n" }
16 sub hlagh { print "Pants::hlagh\n" }
20 my $deuce = new Deuce 1;
26 hlagh; # Deuce::hlagh 1
27 Pants::hlagh; # Pants::hlagh
30 use with \Deuce->new(2);
31 hlagh; # Deuce::hlagh 2
34 hlagh; # Deuce::hlagh 1
43 This pragma lets you define a default object against with methods will
44 be called in the current scope when possible. It is enabled by the "use
45 with \$obj" idiom (note that you must pass a reference to the object).
46 If you "use with" several times in the current scope, the default object
47 will be the last specified one.
50 The main problem to address is that lexical scope and source
51 modifications can only occur at compile time, while object creation and
52 method resolution happen at run-time.
54 The "use with \$obj" statement stores an address to the variable $obj in
55 the "with" field of the hints hash "%^H". It also starts a source filter
56 that replaces function calls with calls to "with::defer", passing the
57 name of the original function as the first argument. When the replaced
58 function is part of Perl core, the call is deferred to a corresponding
59 wrapper generated in the "with" namespace. Some keywords that couldn't
60 possibly be replaced are also completely skipped. "no with" undefines
61 the hint and deletes the source filter, stopping any subsequent
62 modification in the current scope.
64 When the script is executed, deferred calls first fetch the default
65 object back from the address stored into the hint. If the object "->can"
66 the original function name, a method call is issued. If not, the calling
67 namespace is inspected for a subroutine with the proper name, and if
68 it's present the program "goto"s into it. If that fails too, the core
69 function with the same name is recalled if possible, or an "Undefined
70 subroutine" warning is thrown.
73 A call will never dispatch to methods whose name is part of :
75 my our local sub do eval goto return
76 if else elsif unless given when or and
77 while until for foreach next redo last continue
79 map grep system exec sort print say
84 No function or constant is exported by this pragma.
87 Most likely slow. Almost surely non thread-safe. Contains source
88 filters, hence brittle. Messes with the dreadful prototypes. Crazy. Will
91 Don't put anything on the same line of "use with \$obj" or "no with".
96 Carp (core module since perl 5).
98 Filter::Util::Call, Scalar::Util and Text::Balanced (core since 5.7.3).
100 Sub::Prototype::Util 0.08.
103 Vincent Pit, "<perl at profvince.com>", <http://www.profvince.com>.
105 You can contact me by mail or on #perl @ FreeNode (vincent or
109 Please report any bugs or feature requests to "bug-with at rt.cpan.org",
110 or through the web interface at
111 <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=with>. I will be
112 notified, and then you'll automatically be notified of progress on your
113 bug as I make changes.
116 You can find documentation for this module with the perldoc command.
121 A fair part of this module is widely inspired from Filter::Simple
122 (especially "FILTER_ONLY"), but a complete integration was needed in
123 order to add hints support and more placeholder patterns.
126 Copyright 2008 Vincent Pit, all rights reserved.
128 This program is free software; you can redistribute it and/or modify it
129 under the same terms as Perl itself.