Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: setting PERL_PERTURB_KEYS & PERL_HASH_SEED in a perl file

by FreeBeerReekingMonk (Deacon)
on Jul 14, 2016 at 17:56 UTC ( #1167794=note: print w/replies, xml ) Need Help??


in reply to setting PERL_PERTURB_KEYS & PERL_HASH_SEED in a perl file

I am not sure this is the best way, but you can run yourself again:

BEGIN{ unless (defined $ENV{APPLEJACK}){ my $cmd = "env APPLEJACK=TRUEBLUE $^X $0"; print "Running exec $cmd\n"; exec($cmd, @ARGV); exit 0; }else{ print "APPLEJACK already defined\n"; } } print "STARTING " . $ENV{APPLEJACK} . "\n";

Replies are listed 'Best First'.
Re^2: setting PERL_PERTURB_KEYS & PERL_HASH_SEED in a perl file (simpler)
by tye (Sage) on Jul 14, 2016 at 18:22 UTC
    if( ! $ENV{PERL_PERTURB_KEYS} ) { $ENV{PERL_PERTURB_KEYS} = 1; $ENV{PERL_HASH_SEED} = ...; exec( $^X, $0, @ARGV ); die "Failed to exec self: $!\n($^X $0 @ARGV)\n"; } ... rest of Perl code ...

    You can add the BEGIN block if you want to optimize away some wasted initialization work.

    Note that this does not take care of options being passed to Perl by the user, for example, if the script is being invoked like "perl -Imylib -i.old script *.ds". In a more likely scenario, you wouldn't be able to debug the script by just doing "perl -d script ...".

    - tye        

Re^2: setting PERL_PERTURB_KEYS & PERL_HASH_SEED in a perl file
by Corion (Patriarch) on Jul 14, 2016 at 18:23 UTC

    Also, for true relaunching, see Devel::PL_origargv. Your approach will generally work but will lose invocations such as

    perl -MSome::Module -Ilib -w script.pl

    Also, I think your approach would lose the -Taint flag, which is a bit more serious than the program just not working.

Re^2: setting PERL_PERTURB_KEYS & PERL_HASH_SEED in a perl file (bug)
by tye (Sage) on Jul 14, 2016 at 18:27 UTC

    Oh, now I notice that you've mixed your coding metaphors a bit.

    my $cmd = "env APPLEJACK=TRUEBLUE $^X $0"; ... exec($cmd, @ARGV);

    By using the 'list' form of exec, you've told Perl to interpret $cmd as only a path to an executable, not as some string to be interpreted by a shell as 4 different words.

    - tye        

Re^2: setting PERL_PERTURB_KEYS & PERL_HASH_SEED in a perl file
by FreeBeerReekingMonk (Deacon) on Jul 15, 2016 at 14:03 UTC
    Ah, thank you fellow monks for the insight. (actually, I tried tye's way, but it did not work then) Thus:

    BEGIN{ unless (defined $ENV{APPLEJACK}){ $ENV{APPLEJACK}= "TRUEBLUE"; exec( $^X, $0, @ARGV ); exit 0; # we actually never get here } } print "STARTING " . $ENV{APPLEJACK} . "\n";

    prints:

    STARTING TRUEBLUE

      Thx Everyone.

      The solution you gave indeed work.

      exec( $^X, $0, @ARGV );

      I was sure that whatever written inside a BEGIN statement is hapannig before everything else, so I'm not sure why my first attempt did't wok

      Isn't there a way to solve that without recalling the script again?

      By the way, I don't rely on hash to be sorted, but in this case I did my sort base on the values and not the keys.

      Since the values might be equal I got inconsistent printing while in earlier perl version the printing was consistent.

      Thx

      Guy

        Hi gravid,

        Could you show a short example of the kind of sort you're doing (some sample input data, brief code and expected output)? I'm almost certain there's a better solution than messing with Perl's internal hash settings and re-starting the interpreter.

        Regards,
        -- Hauke D

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (7)
As of 2022-12-08 00:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?