Since the other argument wasn't convincing enough, here's an argument that hopefully is.

There's absolutely no reason, in general, for the initialization of the typical static variable to happen at compile time.

Yes there is. Now, a very real-world example:

# Foo.pm package Foo; use base 'Exporter'; our @EXPORT = qw/ foo_init foo_begin /; { my $thing; INIT { $thing = 'init' }; sub foo_init { print "init: $thing"; } } { my $thing; BEGIN { $thing = 'begin' }; sub foo_begin { print "begin: $thing"; } } 1; # Bar.pm package Bar; use Foo; # This can be put in &import if you # think that's more real-world. foo_init(); foo_begin(); 1; # foo.pl #!/usr/bin/perl -wl use Bar;
The output of this will be
Use of uninitialized value in concatenation (.) or string at Foo.pm li +ne 9. init: begin: begin

Again, if you put unnecessary INITs in a module you render that module close to useless for many applications where it could've been used.

ihb

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


In reply to Re^5: Making a variable in a sub retain its value between calls by ihb
in thread Making a variable in a sub retain its value between calls by crashtest

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.