Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I saw a code snippet like
use IO::Socket::SSL; BEGIN { $ENV{PERL_NET_HTTPS_SSL_SOCKET_CLASS} = "IO::Socket::SSL"; $ENV{PERL_LWP_SSL_VERIFY_HOSTNAME} = 0; $ENV{PERL_LWP_ENV_PROXY}='http://127.0.0.1'; $ENV{HTTPS_DEBUG} = 1; }
why the use of the BEGIN block ? why not just assign the $ENV values at runtime?

Replies are listed 'Best First'.
Re: BEGIN block
by Corion (Patriarch) on Oct 23, 2015 at 07:32 UTC

    Most likely, right after the BEGIN block, there is another use statement. The assignments to %ENV are for the benefit of that next use statement (which implicitly also happens at BEGIN time).

    In the case of IO::Socket::SSL, this is to tell LWP::UserAgent the options to use with IO::Socket::SSL.

Re: BEGIN block
by Discipulus (Canon) on Oct 23, 2015 at 07:13 UTC
    Probably to be sure the value are set before the module is loaded?
    Infact an use statement is exactly equivalent to:
    BEGIN { require Module; Module->import( LIST ); }
    The only way to do something before this is use a BEGIN block just before the use statement
    L*
    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
      Yes, that's what a BEGIN block is generally about, but, in this case, it appears that this BEGIN block with these four assignments occurs after the use IO::Socket::SSL; statement, so that these assignments are executed afterwards.
        oops, it seems i've not seen the use statement!

        L*
        There are no rules, there are no thumbs..
        Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: BEGIN block
by AnomalousMonk (Archbishop) on Oct 23, 2015 at 07:18 UTC

    See "BEGIN, UNITCHECK, CHECK, INIT and END" section in perlmod.


    Give a man a fish:  <%-{-{-{-<

Re: BEGIN block
by Anonymous Monk on Oct 23, 2015 at 07:12 UTC

    why the use of the BEGIN block ? why not just assign the $ENV values at runtime?

    So that it has effect at BEGIN time ( at use time)

    Maybe those specific %ENV values have no effect at runtime, maybe they only affect IO::Socket::SSL at BEGIN time (the docs ought to clarify this, check the docs)