Hello Monks,
I would like to use Attribute::Default from CPAN in order to define default arguments for subs in a easily readable way. Here is a simple example which works:
use strict;
use warnings;
use base 'Attribute::Default';
sub f : Default(undef,"xyz") {
print("@_\nEND\n");
}
f('abc'); # 2nd argument defaults to xyz
This prints
abc xyz, followed by a line saying
END. Things stop to work, however, when I use Attribute::Default in a package which itself uses the Exporter; or maybe I just may some silly mistake. Here is the first thing I tried:
# This is file Co.pm
package Co;
use strict;
use warnings;
use base qw(Exporter Attribute::Default);
our @EXPORT_OK=qw(f);
sub f : Default(undef,"xyz") {
print("@_\nEND\n");
}
1;
This will be called in the following way:
# This is file cotest.pl
use warnings;
use strict;
use Co qw(f);
f('abc');
Executing this will output just
abc followed by
END; i.e. the default argument is not used.
It gets even more bizarre when I handle @ISA by myself instead of letting use base doint it:
# This is file Co1.pm
package Co1;
use strict;
use warnings;
use Exporter;
use Attribute::Default;
our @ISA=qw(Exporter Attribute::Default);
our @EXPORT_OK=qw(f);
sub f : Default(undef,"xyz") {
print("@_\nEND\n");
}
1;
I call this as usual,
# This is file cotest1.pl
use warnings;
use strict;
use Co qw(f);
f('abc');
but now I get the error message:
Invalid CODE attribute: Default(undef,"xyz") at Co1.pm
Well, I guess there is a reason why the perldoc to
Attribute::Default says that you need do
use base '...' to access it. Anyway, as a last resort I tried to combine the two approaches:
# This is Co2.pm
package Co2;
use strict;
use warnings;
use Exporter;
our @ISA=qw(Exporter);
use base qw(Attribute::Default);
our @EXPORT_OK=qw(f);
sub f : Default(undef,"xyz") {
print("@_\nEND\n");
}
1;
The program for calling it is, not surprisingly:
use warnings;
use strict;
use Co2 qw(f);
f('abc');
Now I get the error message:
Can't locate object method "_ATTR_CODE_Default" via package "Co2"
What is going wrong here?
--
Ronald Fischer <ynnor@mm.st>
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.