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

Is there some way to declare a custom attribute inline?

For example: sub mysub($) : modifies { }.

My goal is to be able to tell myself that a sub I didn't necessaraly write wants a reference, without attempting to use the prototype for more than detecting the number of arguments. In this case, I would use prototype() and attributes::get() to determine that it needs one argument, and it needs to be able to modify the original, hence, needs a reference. If anyone can suggest a better way, please do so.

Whenever I attempt to follow the example posted above, perl rejects the attribute as invalid... which it is... but it is only invalid because it isn't a built-in, the syntax is acceptable...



My code doesn't have bugs, it just develops random features.

Flame ~ Lead Programmer: GMS (DOWN) | GMS (DOWN)

Replies are listed 'Best First'.
Re: custom attribute?
by chromatic (Archbishop) on Jan 28, 2003 at 06:26 UTC

    Are you looking for Attribute::Handlers? I'm not sure I understand your question, but that's by far the easiest way to declare your own attributes.

Re: custom attribute?
by diotalevi (Canon) on Jan 28, 2003 at 13:00 UTC

    I'm interested but I'm not sure what you're asking for. Can you elaborate? Also, I see you playing with prototypes alot these days. Have you read Tom Christiansen's FMTEYEWK Prototypes in Perl? In general it works out that prototypes just don't work for parameter validation. If you want that then you ought to check out Params::Validate and solve the problem the right way.


    Seeking Green geeks in Minnesota

      In this case I'm not really using the prototype. It's behaving more like a counter than anything else. I use the prototype function to read it and determine how many arguments to send... no more. The attribute I'm trying to set is to tell me weather or not the sub being delcared needs references instead of just data.



      My code doesn't have bugs, it just develops random features.

      Flame ~ Lead Programmer: GMS (DOWN) | GMS (DOWN)

Re: custom attribute? (wrong channel)
by tye (Sage) on Jan 28, 2003 at 18:09 UTC

    You are using subroutine prototypes to convey a count. Since you are looking up the prototype you must be building the argument list dynamically which means you are also bypassing the prototype (or using eval which is probably worse). So it seems you are misusing the prototype.

    Now you want to put an attribute on the subroutine just so you can check for it. I think these both sound like you are fixated on using a specific channel for conveying information and you should consider alternatives.

    You could convey the number of arguments by how many letters in the subroutine name and use uppercase if you want a reference and lower case if not. (just to give you an exagerated idea of how your current plan sounds to me)

    Another alternative would be to have the subroutine return the meta information when you call it with no arguments:

    sub mysub { return -4 unless @_; my( $svA, $svB, $svC, $svD )= @_; ... }
    meaning it wants 4 arguments that are references.

    Good luck with your attributes problem. I did a quick look in perlsub which pointed to the documentation for attributes.pm which mentions doing custom attributes, so perhaps you should just go read that.

                    - tye
      Thanks for the advice tye. The problem is, I don't actually call the sub by name, I work with references, many of which are anonymous to begin with (declared inline). This is suppost to be a part of the custom tests option mentioned here: Data Validation Tests



      My code doesn't have bugs, it just develops random features.

      Flame ~ Lead Programmer: GMS (DOWN) | GMS (DOWN)

        The "suggestion" on using subroutine name was facetious, meant to illustrate another example of an inappropriate channel.

                        - tye