Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: sorting a hash that's in another file

by snax (Hermit)
on Jul 23, 2003 at 16:42 UTC ( [id://277331]=note: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


in reply to sorting a hash that's in another file

Well, if you're confident that you can read in just the hash definition -- say into an array -- and that you're sure that there's no way that this would end up being tainted maliciously, you can use join to get it all into one string and then eval to execute it.
  • Comment on Re: sorting a hash that's in another file

Replies are listed 'Best First'.
Re: Re: sorting a hash that's in another file
by alienhuman (Pilgrim) on Jul 23, 2003 at 16:54 UTC

    Hmm. I was a lot more confident until all those bold tags started showing up ;).

    Seriously, I'm working on my pattern match now, and it's non-trivial (at least for me), so if anyone has an alternative to doing something like:

    open SCRIPT, "script.pl" or die "you scurvy dog"; while (<SCRIPT>) { if ( grep(/\[... insert insane pattern match here ...]%some_hash[... + and here ...]/, $_) ) {

    I'm eager to hear. I wish that I could just require the script w the hash in it, but that will not work right.

    AH

      Well, off the top of my head, I think this would work:
      open SCRIPT, "script.pl" or die "you scurvy dog"; while (<SCRIPT>) { # Find the declaration next unless /\%some_hash/; $hash_declaration = $_; # Now pop out and do another while loop last; } while (<SCRIPT>) { # Keep appending until we reach a semi-colon $hash_declaration .= $_; last if /;/; } eval($hash_declaration);
      But this assumes that you won't have a semi colon in the hash declaration in script.pl. There's a zillion ways to do the reading-in part, too, beyond just a smarter test for when you're done.
        A very inefficient, but valid way of ensuring it works;
        use strict; use warnings; use Data::Dumper; open SCRIPT, "script.pl" or die "Can't open file: $!"; our $hash_declaration=''; our %some_hash; while (<SCRIPT>) { # Find the declaration next unless /\%some_hash/; $hash_declaration = $_; # Now pop out and do another while loop last; } while (<SCRIPT>) { # Keep appending until we reach a semi-colon $hash_declaration .= $_; eval $hash_declaration; last unless $@; } close(SCRIPT); print Data::Dumper->Dumper(\%some_hash);

        .02

        cLive ;-)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://277331]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.