Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: How Are Attributes Useful?

by talexb (Chancellor)
on Jan 18, 2005 at 21:55 UTC ( [id://423195]=note: print w/replies, xml ) Need Help??


in reply to How Are Attributes Useful?

I'm not an expert, but I can bring some of my C experience to bear.

The C language is bound closely to assembler, and as such has certain keywords that act like the attributes that you're talking about. One that comes to mind is volatile, a variable declaration modifier that signals the compiler that whenever that variable was used in an expression, its value had to be read from the memory location assigned to it -- because its value was volatile.

Another example is the lock attribute discussed in the Camel (pp. 457-458); it gives the compiler a hint that only the thread that has locked a function is allowed to call it. This is an attempt to prevent a race condition.

Finally, there's the oxymoron of the constant variable -- that's just a convenience that allows a constant value to be stored in a read-only variable. Any attempt to update this variable's value will be ignored.

So to answer your question -- attributes are used to give hints to the compiler and the run-time about special treatment to be given to certain variables and functions. And sometimes that behaviour isn't fully defined -- it's to leave future perl developers (note the lower case 'p') room to expand, as necessary.

Alex / talexb / Toronto

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

Replies are listed 'Best First'.
Re^2: How Are Attributes Useful?
by beauregard (Monk) on Jan 18, 2005 at 22:55 UTC
    "shared" variables in threads::shared is a good real-world example of attribute use.

    And the now deprecated "locked" attribute works (worked) almost identically to how Java handles the synchronized keyword on methods...

    c.

Re^2: How Are Attributes Useful?
by Limbic~Region (Chancellor) on Jan 19, 2005 at 00:40 UTC
    talexb,
    I don't mean offense, but this really doesn't help at all. The only example of attributes that I have seen that doing anything useful are the built-in ones that I mentioned in my question. You have described them without providing an example which is the same problem I had with the documentation. Can you give me an example of using attributes to make a read-only variable/sub? The question was looking for something tangible and to me this is just window dressing.

    Cheers - L~R

      Look at Attribute::Protected and Attribute::Property. More specifically, look at their source to see what they're doing. Essentially, both are inserting some extra code that runs (fairly transparently) in advance of the code block you provided with the subroutine definition.

      There are legitimate issues around when attributes get run (i.e. during the CHECK phase by default) and whether this is mod_perl safe, but this can be coded around as some others have mentioned. Personally, I think they're an interesting concept that hasn't matured properly yet. Essentially, it's an attempt at specifying behavior as a bunch of building blocks -- another way of templating functionality and reducing redundant code.

      -xdg

      Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2024-04-24 23:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found