Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Have you tried swapping out the <code>BEGIN{}</code> block for an <code>INIT{}</code> block? That's the standard approach for things like modperl which use the "compile-once, run-many" model, as you need to initialize things anew on each separate run, not just once during the compile.</p> <p>But I have to ask why it's all in special block anyway. Why don't you just make some sort of <code>prepare_db_connection()</code> function, and then call it as you need to when the program starts up?</p> <p>Something that won't work in a <code>BEGIN{}</code> will also have the same problem if it's main-line code in a module file that gets <code>use</code>d. That's another possible reason to use an <code>INIT{}</code> block.</p> <p>I've also seen deadly-embrace problems of mutual recursion that have to be unravelled using something like an <code>require</code> instead of <code>use</code>, or an <code>INIT{}</code> instead of a <code>BEGIN{}</code>. But that's pretty rare.</p> <p>Consider this program:</p> <pre><code>% cat sto-INIT-eg #!/usr/bin/perl -l print " PRINT: main running"; die " DIE: main dying\n"; die "DIE XXX /* NOTREACHED */"; END { print "1st END: done running" } CHECK { print "1st CHECK: done compiling" } INIT { print "1st INIT: started running" } END { print "2nd END: done running" } BEGIN { print "1st BEGIN: still compiling" } INIT { print "2nd INIT: started running" } BEGIN { print "2nd BEGIN: still compiling" } CHECK { print "2nd CHECK: done compiling" } END { print "3rd END: done running" } </code></pre> <p>When compiled only, it produces:</p> <pre><code>% perl -c sto-INIT-eg 1st BEGIN: still compiling 2nd BEGIN: still compiling 2nd CHECK: done compiling 1st CHECK: done compiling sto-INIT-eg syntax OK </code></pre> <p>While when compiled <em>and</em> executed, it produces this:</p> <pre><code>% perl sto-INIT-eg 1st BEGIN: still compiling 2nd BEGIN: still compiling 2nd CHECK: done compiling 1st CHECK: done compiling 1st INIT: started running 2nd INIT: started running PRINT: main running DIE: main dying 3rd END: done running 2nd END: done running 1st END: done running </code></pre> <p>And the shell reports an exit of 255, per the <code>die</code>.</p> <p>You should be able to arrange to have the connection happen when you need it to, even if a <code>BEGIN{}</code> proves too early. </p> <p>Hm, just remembered. There's no chance you're doing something with <code>DATA</code> in a <code>BEGIN{}</code>, is there? That's not set up till the interpreter runs; it's not open to the compiler.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload