Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^4: goto &sub and local question

by TGI (Vicar)
on Nov 25, 2008 at 02:26 UTC ( #725754=note: print w/ replies, xml ) Need Help??


in reply to Re^3: goto &sub and local question
in thread goto &sub and local question

If you plan on initializing %familySecrets, you need to use a BEGIN block if you are in main. In a module, this isn't necessary. Technically, you can use any one of the other special named blocks that run before your code: BEGIN, UNITCHECK, CHECK, or INIT. See perlmod for more details on these blocks.

{ my %familySecrets; BEGIN { %familySecrets = ( closet => 'skeleton', herbs_and_spices => 11, coke_recipe => 'sugar, water, shoe polish', ); } sub s1 { # Read family secrets } sub s2 { #write family secret #call s1 } }


TGI says moo


Comment on Re^4: goto &sub and local question
Select or Download Code
Re^5: goto &sub and local question
by ikegami (Pope) on Nov 25, 2008 at 03:18 UTC

    No need to introduce a new block. You can just add BEGIN in front of the existing curlies.

    BEGIN { my %familySecrets = ( ... ); sub s1 { ... } sub s2 { ... } }

    By the way, BEGIN is probably not needed in the main script either, but there's no harm in using it.

      I like the idea of using one block, it's much cleaner. Thank you.

      Whether you need to use a BEGIN around initializations in your main package really depends on how you write your code. If your initialization occurs in your script before you call any of the subs that rely on it, no BEGIN is needed. If you aren't initializing anything, no BEGIN is needed.

      When you require or use a module, the code it contains is executed. So, any initializations will occur when the code is required, no matter where they are in the module. If the module is calls its own routines while being required, then the same possibility for problems with uninitialized block scoped variables exists.

      # subs first - BEGIN not needed { my $var = 'blah'; sub foo { ... } sub bar { ... } } sub bax { ... } do_stuff_here();
      # subs last - BEGIN needed iff initializing values. do_stuff_here(); # $var has been created but not initialized. { my $var = 'blah'; sub foo { ... } sub bar { ... } } sub bax { ... }

      When you say the BEGIN is probably not needed, I interpret your statement to mean that the BEGIN may not be necessary because the OP probably will not have to initialize his block scoped variables.

      If my understanding is incorrect, is there some other mechanism that you suggest for initializing the variables?


      TGI says moo

        When you say the BEGIN is probably not needed, I interpret your statement to mean that the BEGIN may not be necessary because the OP probably will not have to initialize his block scoped variables.

        Most code I've seen is organized such that initialization statements come before others. I meant BEGIN is probably not needed because the code is probably already executed in the right order without it.

        If my understanding is incorrect, is there some other mechanism that you suggest for initializing the variables?

        No, BEGIN is just fine. Like I said, even if it's not needed, there's no harm. For example, when I have an inlined module, I do

        BEGIN { package ...; ... }

        The BEGIN is rarely needed there, but it has avoided problem in the past. There's no cost to adding the word BEGIN, especially since I already had curlies in place to limit the scope of package and any my and our vars.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (13)
As of 2014-07-28 15:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (204 votes), past polls