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

Guys, I'm trying to restart a Perl script from itself to set up a few environment variables without using a wrapper script.
The script runs correctly if I run it directly but if I run it from within itself, it dies saying:

Search pattern not terminated at lib/5.18.2/warnings.pm line 508. Compilation failed in require at LogReader.pl line 33. BEGIN failed--compilation aborted at LogReader.pl line 33.

I've tried to disable "warnings" and everything but no luck so far. I would appreciate some help!! There is the code:
#!/usr/bin/perl BEGIN { # Instead of using a messy wrapper to set the necessary variab +les, we use the script itself to set the variables and restart itself unless ($ENV{BEGIN_BLOCK} or $^C) { $ENV{LD_LIBRARY_PATH} = '/app/datapower/tools/LogReade +r/expat/lib/'; $ENV{BEGIN_BLOCK} = 1; print "BEGIN_BLOCK executed. ENV:". $ENV{BEGIN_BLOCK} +."\n"; exec 'env',$0,@ARGV; } print "BEGIN executed. \n"; unshift @INC, ("/app/caapm/epagent/epaplugins/lib/perl/", "/app/caapm/ +tools/lib/","/app/caapm/datapower/tools/LogReader/", "/app/caapm/datapower/tools/LogReader/expat","/app/caapm/dat +apower/tools/LogReader/XML/Parser/Expat/", "/app/caapm/datapower/tools/localperl/lib/site_perl/5.18.2/s +un4-solaris-thread-multi-64", "/app/caapm/datapower/tools/localperl/lib/site_perl/5.18.2", "/app/caapm/datapower/tools/localperl/lib/5.18.2/sun4-solari +s-thread-multi-64", "/app/caapm/datapower/tools/localperl/lib/5.18.2"); print "INC executed. ". @INC ."\n"; print "\n From within BEGIN: LD_LIBRARY_PATH = " . $ENV{LD_LIB +RARY_PATH} . "\n"; #delete($ENV{BEGIN_BLOCK}); #delete ($ENV{LD_LIBRARY_PATH}); print "============== ENV =======================\n"; print `env`; print "\n Command being executed: $0 \n"; } INIT { print "\n From within INIT: LD_LIBRARY_PATH = " . $ENV{LD_LIBR +ARY_PATH} . "\n"; } $^W =0; use strict; no warnings; use File::Tail; use XML::Simple; use XML::Parser; use threads; use threads::shared; use Config; use DateTime::Format::Strptime; use URI::URL; use strict; use warnings; use Data::Dump qw(dump); # Not in use as it doesnt allow you to use more the 3 levels for the m +etric names.. use Wily::PrintMetric; use Ecetera::Utils qw( logit Generate_conf Read_Config :const ); use File::Basename qw( dirname ); use File::Basename; use Cwd qw( abs_path ); print "Executed till the end - WOW!\n";
I'm using Perl v5.18.2 (from source) and v5.8.4 on Solaris.

Cheers,
Alex

Replies are listed 'Best First'.
Re: Search pattern not terminated at warnings.pm line 508.
by NetWallah (Canon) on Jun 25, 2014 at 04:50 UTC
    I got a "permission denied" from the shell, when executing your "exec 'env'..." line. Changing that line to:
    exec '/usr/bin/env',"MYCODEFILE=$0",@ARGV;
    allows the line to execute, and shows the environment variables.

            What is the sound of Perl? Is it not the sound of a wall that people have stopped banging their heads against?
                  -Larry Wall, 1992

Re: Search pattern not terminated at warnings.pm line 508. ( defined-or)
by Anonymous Monk on Jun 25, 2014 at 06:48 UTC
    You can't mix perls and warnings.pm -- warnings.pm for perl-5.18.2 won't work with perl-5.8.8 -- warnings.pm (and strict.pm...) are tightly connected to the perl they're distributed with

    perl 5.8.8 did not have the defined-or operator

Re: Search pattern not terminated at warnings.pm line 508.
by maat (Initiate) on Jun 26, 2014 at 01:17 UTC

    Great job guys!


    Based on your comments I figured out that at the "exec" I was triggering the script using the default Perl installation on the system (/usr/bin/perl) instead of mine customized one and mixing up a Perl executable v5.8.4 and the Libraries of the v5.18.2. Changing the first line from:
    #!/usr/bin/perl
    To
    #!/app/caapm/datapower/tools/localperl/bin/perl
    All working now!
    Thanks!!

    Cheers,
    Alexandre
      shebang is great, but perl is $^X :}
Re: Search pattern not terminated at warnings.pm line 508.
by Anonymous Monk on Jun 25, 2014 at 06:41 UTC
    Which of the modules you are using are source filters?
Re: Search pattern not terminated at warnings.pm line 508.
by maat (Initiate) on Jun 26, 2014 at 01:25 UTC
    I've also include:
    require 5.18.2;
    To make it easier to debug in case someone else try to change the default Perl Executable on the script.

    Cheers,
    Alexandre