Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: How Are Attributes Useful?

by dimar (Curate)
on Jan 18, 2005 at 23:41 UTC ( [id://423216]=note: print w/replies, xml ) Need Help??


in reply to How Are Attributes Useful?

One thing that seems to be missing from the picture is why would the 'non-behavior-changing' aspect of attributes detract from their utility? (assuming this aspect is not in dispute).

It seems pretty straightforward to envision a circumstance where someone might want to redefine metadata about a subroutine or method, without necessarily changing its name.

If you will, please consider a simple illustration:

If you are familiar with Windows, you notice how a filename allows essentially a 'basename' plus an 'extension' ... there have been numerous times when I wished I could sort or view files based on something besides just that. In other words, don't treat all these files exactly the same just because they all have the same extension 'txt' ... I want to segment them even further.

The problem is the only workaround is to rename the files ... or append additional info to the filenames.

### before vacation_2003.txt vacation_2004.txt ### after vacation_2003.txt vacation_2004_hotels.ca.northern.txt vacation_2004_hotels.ca.southern.txt

This is unacceptable if you have external dependencies that specifically rely on the previous filenames.

The punchline is that naming files (and by analogy subroutines) is in large part an arbitrary choice, and entirely up to you. The only real constraint is that they be unique within the relevant scope. Therefore, talking about specific names people have used for their files in the past is never really going to illustrate the issue of *whether* a particular naming convention is useful for *you*.

Thus, the (at least one) point of attributes is the ability to obviate the need for name changes as the exclusive method of distinguishing certain methods and properties into arbitrary subgroups (without breaking your code).

No, this is not much more than adding little 'sticky notes' but still, that does not diminish their potential usefulness does it? This answers "how" they can be useful, but not "whether" they are useful in your specific project or circumstance, or according to your specific coding style.

Replies are listed 'Best First'.
Re^2: How Are Attributes Useful?
by Limbic~Region (Chancellor) on Jan 19, 2005 at 00:49 UTC
    scooterm,
    to envision a circumstance where someone might want to redefine metadata about a subroutine or method, without necessarily changing its name

    It is my understanding that attributes are handled in the compiling phase (or rather non-runtime phases) so I fail to see how this would allow you to redefine the subroutine metadata. Could you provide an example of how to do this so that I might better understand?

    Cheers - L~R

      It is my understanding that attributes are handled in the compiling phase (or rather non-runtime phases)

      That is consistent with my understanding as well. I personally do not have sample code to honor your specific request, but it is a good question. Absent specific code samples, discusson on this topic tends to diminishing returns.

        It is my understanding that attributes are handled in the compiling phase (or rather non-runtime phases) ..

      Well, I disagree, but without either the knowledge of how perl works or the time to delve into the perl code, I can't back up my feeling about this. I think both compile-time *and* run-time behvaiour is affected by attributes.

      I wonder if TimToady or DanS could help us out here.

      Alex / talexb / Toronto

      "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

        talexb,
        After discovering Attribute::Handlers::Prospective and playing around with it for a while, I did find that you can trigger a piece of code to be run at any phase, to include runtime, as a result of using an attribute. What is still unclear to me is:
        • How this can be accomplished without this module. The documentation on attributes is lacking in my opinion and this module was written by TheDamian using source filters afterall.
        • How triggering a piece of code can make a variable read-only, as you suggested previously, or have any other effect on its behavior. The code only gets triggered once, not each time the variable/sub is accessed
        So far - it still seems to me to be nothing more than a label that you can attach providing the possibility of other code to react to that label.

        ...but without either the knowledge of how perl works or the time to delve into the perl code
        That is the point of the original question. It seems that a lot of people are in the dark. It also seems that there are a lot of assumptions about what is possible without any code backing it up. It seems to me that it shouldn't be very hard for someone that really understands attributes to post a small snippet demonstrating how to use them.

        Cheers - L~R

        Update: Added last paragraph moments after hitting submit.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2024-03-19 02:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found