in reply to Re: static storage in a local subroutine?
in thread static storage in a local subroutine?

You probably want that to be "BEGIN {" not "{", to ensure that the initialization is performed once and only once.

-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.

  • Comment on Re^2: static storage in a local subroutine?

Replies are listed 'Best First'.
Re^3: static storage in a local subroutine?
by Limbic~Region (Chancellor) on Nov 23, 2005 at 14:56 UTC
    merlyn,
    You are of course correct. It is better to be safe then sorry. Unfortunately, I am a bit confused as to the behavior of the following:
    #!/usr/bin/perl use strict; use warnings; INITIALIZE: { my $cnt = 1; sub get_count { return $cnt++; } } print get_count(), "\n" for 1..10; goto INITIALIZE if get_count() < 15; __END__ 1..10, 12..21
    Care to elaborate?

    Update: Explanation from tye in the CB
    The re-initialization of $cnt doesn't impact the get_count() sub because it is only defined once at compile time. It however does get re-initialized which may impact other things within the same scope but not in the sub itself. To avoid wasting resources and make the intent clear, using the BEGIN block is a good idea (which I never doubted in the first place).

    Cheers - L~R