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 scoping and source
51 modification 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 has a prototype or is part of the core, the call is deferred to
59 a corresponding wrapper generated in the "with" namespace. Some keywords
60 that couldn't possibly be replaced are also completely skipped. "no
61 with" undefines the hint and deletes the source filter, stopping any
62 subsequent 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" error is thrown.
73 A call will never be dispatched to a method whose name is one 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".
93 When there's a function in the caller namespace that has a core function
94 name, and when no method with the same name is present, the ambiguity is
95 resolved in favor of the caller namespace. That's different from the
96 usual perl semantics where "sub push; push @a, 1" gets resolved to
99 If a method has the same name as a prototyped function in the caller
100 namespace, and if a called is deferred to the method, it will have its
101 arguments passed by value.
106 Carp (core module since perl 5).
108 Filter::Util::Call, Scalar::Util and Text::Balanced (core since 5.7.3).
110 Sub::Prototype::Util 0.08.
113 Vincent Pit, "<perl at profvince.com>", <http://www.profvince.com>.
115 You can contact me by mail or on #perl @ FreeNode (vincent or
119 Please report any bugs or feature requests to "bug-with at rt.cpan.org",
120 or through the web interface at
121 <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=with>. I will be
122 notified, and then you'll automatically be notified of progress on your
123 bug as I make changes.
126 You can find documentation for this module with the perldoc command.
131 A fair part of this module is widely inspired from Filter::Simple
132 (especially "FILTER_ONLY"), but a complete integration was needed in
133 order to add hints support and more placeholder patterns.
136 Copyright 2008 Vincent Pit, all rights reserved.
138 This program is free software; you can redistribute it and/or modify it
139 under the same terms as Perl itself.