Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Anyone using Attribute::Default from CPAN?

by rovf (Priest)
on Jun 05, 2008 at 14:12 UTC ( #690437=perlquestion: print w/replies, xml ) Need Help??

rovf has asked for the wisdom of the Perl Monks concerning the following question:

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>

Replies are listed 'Best First'.
Re: Anyone using Attribute::Default from CPAN?
by moritz (Cardinal) on Jun 05, 2008 at 14:28 UTC
    I don't know what went wrong, but you have another alternative. You can use Exporter without inheriting from it:
    package Co; use Exporter qw(import); our @EXPORT_OK = qw(f); ...
      Thank you for this suggestion, but this doesn't work either: Though I don't get any error message, the default argument is not supplied to the function. :-(
      -- 
      Ronald Fischer <ynnor@mm.st>
Re: Anyone using Attribute::Default from CPAN?
by lodin (Hermit) on Jun 05, 2008 at 19:04 UTC

      Thanks a lot. The articles you wrote gave me some idea where the problem could be. I now tend to agree with you that I would have been better off without attributes, but at least I could get it working!. I have placed a HowTo on my public scratchpad.

      Thanks to all for helping!

      -- 
      Ronald Fischer <ynnor@mm.st>

        Perhaps the comments on your scratchpad would be a good addition to the documentation of the module. You could annotate the POD via AnnoCPAN and e-mail the author of Attribute::Default telling him about your problem and your solution.

        lodin

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://690437]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2022-05-17 21:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (68 votes). Check out past polls.

    Notices?