Welcome to the Monastery | |
PerlMonks |
comment on |
( [id://3333]=superdoc: print w/replies, xml ) | Need Help?? |
Declaring hash structures within subs that get repeatedly called is a common problem. Another solution that allows the hash to be declared on demand (if the sub may or may not be called this is good) but still only be called once is to use a closure like this:
Doing it this way makes %hash private to the sub as the my delaration localises it to the bare block where it is accessible to the sub but not main:: per se. You start the sub by checking if the hash is populated. On the first call to the sub it will not be so you set it. From then on it will populated so you don't need to set it again during subsequent calls to the sub. Perl retains a reference to it between calls to the sub so it never gets garbage collected. cheers tachyon s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print In reply to Re: Re: Re: Re: Re: Z80 Assembler -> Hex Opcode
by tachyon
|
|