Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^7: Making a variable in a sub retain its value between calls

by ihb (Deacon)
on Apr 19, 2005 at 00:44 UTC ( [id://449076]=note: print w/replies, xml ) Need Help??


in reply to Re^6: Making a variable in a sub retain its value between calls
in thread Making a variable in a sub retain its value between calls

I assure you, it certainly isn't a tomayto-tomahto difference of opinion. This isn't about opinions. This is about not putting extremely excessive limitations on your code without reason. I get the feeling that you don't realize how far the consequences propagate -- the consequences go far beyond your reach as a module author. (Yes, the world will probably end, too. ;-)) Any user that uses your module will be effected by this completely useless limitation.

See CHECK and INIT under mod_perl and call a modules INIT-section for examples of when INIT can cause trouble. (The second problem is completely unnecessary and wouldn't have been a problem if the author would've understood the purpose of INIT, as I point out in my reply.)

Reply to the update:
Right, if you use it only for scripts it's OK, but if I'll have an opinion then it's that it's far better to use a solution that, in general, always works over a solution that, in general, sometimes works.

I'm curious. When do you need to use INITs to delay execution after all BEGIN blocks in scripts?

ihb

See perltoc if you don't know which perldoc to read!

Replies are listed 'Best First'.
Re^8: Making a variable in a sub retain its value between calls
by tlm (Prior) on Apr 19, 2005 at 01:22 UTC

    I'm curious. When do you need to use INITs to delay execution after all BEGIN blocks in scripts?

    The point is that I don't have to even think about it. This is all very simple: I have a script, I have various bits of initialization code that need to be executed before run time scattered throughout; so I put them INIT blocks, without having to give a second's thought to what possible conflict any one of these INIT blocks may have with some BEGIN block later in the code.

    The point of using INIT blocks to solve the static variable initialization problem is to give the programmer the flexibility to arrange code as desired; otherwise it would have been sufficient to stick all subs with static variables at the top of the file. If one puts this initialization code in BEGIN blocks now one goes back to having to worry about the position of these initalization BEGIN blocks relative to other BEGIN blocks (including implicit ones in use statements) in the file, which makes no sense to me.

    the lowliest monk

      Are you aware that BEGIN also executes before run-time? Using BEGIN should require less thought than INIT, given that INIT is very similar but has extra limitations, yet you incorrectly claim that you "don't have to even think about it".

        I don't know how to reply to this other than to re-iterate what I wrote in nodes 449075 and 449081. But I'd like to remark that from your response and ihb's, you apparently consider INIT blocks to be at best useless, at worst harmful, and certainly misnamed (i.e. not suitable for initialization). Do you have any use for them?

        Update: OK, I looked up INIT blocks in the Camel, wondering if I would find some dire warnings against their use. On p. 223 (3d edition), in a discussion of precisely the problem the OP posted, the book states

        ...you'll need to make sure any run-time assignment to my is executed early enough, either by putting the whole block before your main program, or, alternatively, by placing a BEGIN or INIT block around it to make sure it gets executed before your program starts.
        So what I wrote is essentially the same thing that the Perl docs say. If INIT blocks are so evil, why is the Camel giving such advice?

        the lowliest monk

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-04-20 01:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found