Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re^9: replicating the command 'unset LIBPATH' in perl

by viffer (Beadle)
on May 19, 2010 at 06:33 UTC ( #840621=note: print w/replies, xml ) Need Help??


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

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

Replies are listed 'Best First'.
Re^10: replicating the command 'unset LIBPATH' in perl
by ikegami (Patriarch) on May 19, 2010 at 06:55 UTC

    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.

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

        But the code starts with first two lines

        #!/usr/bin/perl -- BEGIN {

        I thought it didn't matter where you put the BEGIN statement since it executed first at compile time? Obviously not!

        Who told you that? Which line do you think gets executed first?

        $ cat junk12.pl warn "hello"; BEGIN { warn "hello "; } warn "hello"; BEGIN { warn "hello "; } warn "hello";

        See also Execution order of END/CHECK vs BEGIN/INIT

        I get the uninitialised error message,

        Those are called warnings (see perllexwarn), and in order to get warnings, you have to specifically turn them on, which the code I posted did not do (you should do that in your code, ...).

        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.

        Wait, what? perldoc -f exec

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (2)
As of 2022-05-21 00:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (76 votes). Check out past polls.

    Notices?