|
OOorNO - Handles "@_" for your own class methods. |
OOorNO - Handles ``@_'' for your own class methods.
OOorNO exists for the sole purpose of helping another module handle the input for its internal routines, and is not intended for direct use in your Perl programs, though it can.
For heftier, more robust support for parsing input to your programs, take a look at Getopt::Long.pm for parsing commandline arguments and CGI.pm for parsing STDIN in programs that operate over the common gateway interface.
0.00_7
None by default.
All available methods. (see section ``METHODS'' below)
:all (exports all of @EXPORT_OK)
coerce_array(@array/(list))This method retrieves input sent to your class methods when called with
name-value pairs and returns an anonymous hash reference whose keys and values
correspond to the input argument names and their respective values. If nothing
is passed to it, an empty hash reference will be returned, eg- { }
package Your::Class; use OOorNO qw( coerce_array );
sub bar {
my($args) = coerce_array(@_);
...
-OR-
package Your::Class; use OOorNO; our($onobj) = OOorNO->new();
sub foo {
my($self) = shift(@_);
my($args) = $onobj->coerce_array(@_);
...
-OR-
package Your::Class; use OOorNO; use vars qw( @ISA );
@ISA = qw( OOorNO );
sub foo {
my($self) = shift(@_);
my($args) = $self->coerce_array(@_);
...
It's common practice for Perl modules to accept name-value pairs for their methods, and because @_ is an array it is easy to encounter warnings and errors when this isn't handled correctly. An example of what this kind of call would look like is shown below in the imaginary subroutine ``Your::Class::method()''
Your::Class->method
(
-name => 'Joe',
-rank => 'Private, First-Class',
-SN => '87D91-35-713FOO',
);
Quite often a class method will use code such as this to handle name-value paired input:
sub foo {
my($class) = shift;
my(%args) = @_; ...
-and/or-
sub bar {
my($args) = { @_ }; ...
While this practice is not evil, it can be error-prone in situations where:
# object-oriented style
use CGI;
my($cgi_object) = CGI->new();
my($visitor) = $cgi_object->param('visitor name');
-OR-
# procedural style
use CGI qw( param );
my($visitor) = param('visitor name');
When these situations occur, class methods sorting out name-value paired input using the common problematic technique (demonstrated above in ``Pitfalls)'' encounter problems such as undesired program behavior, general errors, and warnings -both fatal and non-fatal. Problems include:
Warnings (see perldiag) resulting from the above mentioned situations could include any the following (Some of these don't apply unless you run your program under the warnings pragma) like you should.
Can't coerce array into hashOdd number of elements in hash assignmentNot a %s reference%s is probably ``HASH'', though it could be complaining about a
non-reference to any data type that your routine may be attempting to treat
as a reference. This is often the result of a class method being called in
procedural style rather than in the object-oriented style using the arrow
-\> syntax. The class method expects the first argument to be an object
reference, when it is clearly not. (This warning is fatal as well.)
Can't call method %s on unblessed reference
myargs(@_)This method retrieves the input sent to your class methods and returns it untouched, with the exception that if a blessed object reference from the same namespace as the caller is found in $_[0], it will be not be included with the rest of the arguments when they are returned. This simply allows the methods in your class to get their argment list quickly without having to check if they were called procedurally or with object-oriented notation. This has its own pros and cons, and you should use this tool with care.
If you are expecting a blessed object reference from your package to be in @_ regardless of the way your method was called -don't use this method because that reference will obviously be excluded from your argument list.
package Your::Class; use OOorNO qw( myargs );
sub bar {
my(@args) = myargs(@_);
...
-OR-
package Your::Class; use OOorNO; our($onobj) = OOorNO->new();
sub foo {
my(@args) = $onobj->myargs(@_);
...
myself(@_)Undocumented.
OOorNO(@_)Undocumented.
shave_opts(\@_)Undocumented.
None.
This documentation isn't done yet, as you can see. This is being rectified as quickly as possible. Please excercise caution if you choose to use this code before it can be further documented for you. It is present on CPAN at this time despite its unfinished condition in order to providing support for the File::Util module which lists OOorNO among its prerequisites. Please excuse the inconvenience.
Tommy Butler <cpan@atrixnet.com>
Copyright(c) 2001-2003, Tommy Butler. All rights reserved.
This library is free software, you may redistribute and/or modify it under the same terms as Perl itself.
|
OOorNO - Handles "@_" for your own class methods. |