On Fri, May 06, 2011 at 08:16:14AM +0100, Rob Dixon wrote:
> On 06/05/2011 01:21, Mike McClain wrote:
> >
> > Here's a simple example that illustrates the problem I've run into:
<snip>
> As Uri says, initialising variables at the point of declaration is done
> at run time. This is from 'Programming Perl', in the section 4.8.2 -
> 'Lexically Scoped Variables: my'
>
> > A statement sequence may contain declarations of lexically scoped
> > variables.> > Such declarations tend to be placed at the front of the
> > statement sequence,
but
> > this is not a requirement. In addition to declaring variable names at
> > compile
> > time, the declarations act like ordinary run-time statements: each of them
> > is
> > elaborated within the sequence of statements as if it were an ordinary
> > statement
> > without the modifier.
Thanks, I've the second edition which is not quite so explicit
and INIT is not in the index nor where BEGIN and END are discussed
but the above clearly explains why I wasn't getting what I expected.
> This could be solved with a BEGIN block, but an INIT block is more
> appropriate as it ensures that compilation is complete and all 'use'
> statements have been executed. Like this:
<snip>
> INIT {
This is the solution I needed.
> But I am unsure what you intend by the statement "$fibs[$#fibs+1] = 2;"
> and you need to be aware that this also will be executed at the start of
> run time.
The code in my post was just to explore very simply what I was seeing
in a much larger program and the statement you mention had no purpose
other than to help me see where the problem lay.
Sorry if it was confusing.
> Alternatively, you could simply write code to do the initialisation on
> the first call of the subroutine:
<snip>
Since the problem that brought this up is a procedure for generating
Fibbonacci numbers that may be called thousands of times an extra test
would be less than optimal but I'll keep it in mind since I may have a
use for it another day.
> I hope this helps.
>
> Rob
Absolutely a big help.
Not only did you supply an explanation but a solution.
Best answer possible.
Thanks again,
Mike
--
Satisfied user of Linux since 1997.
O< ascii ribbon campaign - stop html mail - www.asciiribbon.org
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/