in reply to Re^7: replicating the command 'unset LIBPATH' in perl
in thread replicating the command 'unset LIBPATH' in perl

Its not a secret :) __FILE__ site:perldoc.perl.org The special literals __FILE__, __LINE__, and __PACKAGE__ represent the current filename, line number, and package name at that point in your program.
$ perl -e"die __FILE__" -e at -e line 1. $ echo die __FILE__ > thefileis $ perl -e"die __FILE__" -e at -e line 1. $ echo die __FILE__ > thefileis $ perl thefileis thefileis at thefileis line 1. $ perl D:\thefileis D:\thefileis at D:\thefileis line 1. $

Replies are listed 'Best First'.
Re^9: replicating the command 'unset LIBPATH' in perl
by viffer (Beadle) on May 19, 2010 at 06:33 UTC
    OK - so __FILE__ is the name of my script, but I will admit to not understanding what you're trying to do with it.

    When I run your version of the BEGIN code, I get

    Use of uninitialized value in string eq at /apps/bin/blah/reconcile_wi +thdotsh.pl line 68. main::BEGIN() called at /apps/bin/blah/reconcile_withdotsh.pl +line 73 eval {...} called at /apps/bin/blah/reconcile_withdotsh.pl lin +e 73
    where lines 68 and 73 are:
    67 BEGIN { 68 unless( $ENV{ +__FILE__ } eq __FILE__ ){ 69 delete $ENV{LIBPATH}; 70 $ENV{ +__FILE__ } = __FILE__; 71 exec $^X, __FILE__, @ARGV; # relaunch without LIBPATH 72 } 73}
    It isn't __FILE__ that is the uninitialized value (as I put a print command in), so presumably its the "$ENV{ +__FILE__ }" but I can't manage to print that to check - and being in a BEGIN statement I can't step through it in debug.

    Also, in both versions the script just ends (as opposed to terminating with an error).

    Maybe I should be a heretic and go back to using COBOL on a mainframe! Do any still exist?

    Cheers

    Kev

      When I run your version of the BEGIN code

      Where'd you see that?

      I get Use of uninitialized value in string eq

      That's what happens when you use a variable to which you never assigned a value.

      Also, in both versions the script just ends (as opposed to terminating with an error).

      How did you determine that (as oppose to running successfully with no output)? If it's true, maybe whatever is messing with your Perl to cause it to give a stack trace for warnings — where's that eval come from? — is also messing with the process replacing itself.

      The whole point of placing the code in BEGIN is to get it to execute as early as possible, yet the code is at line 67 of the script. Move that to line 1! There's no point in doing work before this check since it restarts the process from scratch.

        I was referring to Anonymous Monk and JavaFan's BEGIN code.

        I have now moved the BEGIN to line 1. I thought it didn't matter where you put the BEGIN statement since it executed first at compile time? Obviously not!

        I presumed the code had just 'stopped' since using perl -d it didn't allow me to step through the script - but when I checked the log files it had run.

        The weird thing is when I include the line

        "unless( $ENV{+ __FILE__ } eq __FILE__ ){"
        from AnonymousMonks code (below:)
        BEGIN { unless( $ENV{ +__FILE__ } eq __FILE__ ){ delete $ENV{LIBPATH}; $ENV{ +__FILE__ } = __FILE__; exec $^X, __FILE__, @ARGV; } }
        I get the uninitialised error message, I can't step through the code, but it connects and works, however
        if I exclude the line, (as per JavaFans example here:),
        BEGIN { if (exists $ENV{LIBPATH}) { delete $ENV{LIBPATH}; exec $^X, __FILE__, @ARGV; } }
        I don't get the uninitialsed error, I can step through the script, but I can't connect!

        I really don't understand what the line:

        "unless( $ENV{+ __FILE__ } eq __FILE__ ){"
        is trying to do, but without it the solution doesn't work. They both delete the LIBPATH and both do the same exec, but only one works.

        I could live without being able to step through the code in debug mode, although again, I don't understand why the inclusion of Anonymous Monks BEGIN code seems to disable debug mode.

        I really do appreciate everyones patience.