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

Can anyone out there possibly whip together the regex required to read the blow code (from $code) and add the hash elements passed to new to the hash ref $info? (Note: $code was generated by opening the actual file which holds the code.)

And of course - new can be called in many different ways:

my $fun = new CX::Lique( name => 'Test Addon', 'author'=> 'nutshell', email => 'x@x.com', descrip => 'That', version => '1.1', build => 2 );
And a second example:
my $q = CX::Lique->new( name => 'Test', 'author'=> 'yeehaw', 'email' => 'yeehaw@yahoo.com', descrip => 'Fun', version => '1.11', build => 2 );

So basically once the regex is done reading/processing $code - $info will look something like (if it processed the first example I showed above):

$info = { 'name' => 'Test Addon', 'author'=> 'nutshell', 'email' => 'x@x.com', 'descrip' => 'That', 'version' => '1.1', 'build' => 2 }
Good luck and thanks in advance! --nutshell

Replies are listed 'Best First'.
Re: Processing (from a variable) the 'new' methods hash and storing it to a hash ref
by BrowserUk (Patriarch) on Oct 03, 2002 at 03:20 UTC

    You could try this.

    #! perl -sw use strict; use Data::Dumper; my $source = do {local $/; <DATA> }; # Slurp the source # build an array of chunks that look like parms to Lique() my (@parms) = $source =~ m/=\s+(?:new\s+)?CX::Lique(?:->new\s*?)?\((.* +?)\);/gcs; # Processs the array and build a hash from the chunks. for my $parm (@parms) { my %info; $parm =~ s/'?(\w+)'?\s+=>\s+'?([^,')]+)'?,?\n/$info{$1} = $2;/seg; print Dumper(\%info); } __DATA__ my $fun = new CX::Lique( name => 'Test Addon', 'author'=> 'nutshell', email => 'x@x.com', descrip => 'That', version => '1.1', build => 2 ); my $q = CX::Lique->new( name => 'Test', 'author'=> 'yeehaw', 'email' => 'yeehaw@yahoo.com', descrip => 'Fun', version => '1.11', build => 2 );

    Which gives this. No guarentees it will work for all the possible ways this could be structured in the source though.

    C:\test>202407 $VAR1 = { 'descrip' => 'That', 'build' => '2', 'version' => '1.1', 'email' => 'x@x.com', 'name' => 'Test Addon' }; $VAR1 = { 'descrip' => 'Fun', 'build' => '2', 'version' => '1.11', 'email' => 'yeehaw@yahoo.com', 'name' => 'Test' }; C:\test>

    Cor! Like yer ring! ... HALO dammit! ... 'Ave it yer way! Hal-lo, Mister la-de-da. ... Like yer ring!
      Thanks SOO much++

      --nutshell

Re: Processing (from a variable) the 'new' methods hash and storing it to a hash ref
by BrowserUk (Patriarch) on Oct 03, 2002 at 01:09 UTC

    No regex needed. Just an extra set of curly braces takes care of the whole thing. Putting Lique() in a package left AAEFTR.

    #! perl -sw use strict; use Data::Dumper; my $debug = 1; sub Lique { my $inforef = shift; print Dumper( $inforef ) if $debug; # do stuff return $inforef; } my $fun = Lique( { # << Note! name => 'Test Addon', 'author'=> 'nutshell', email => 'x@x.com', descrip => 'That', version => '1.1', build => 2 } # << Note! ); my $q = Lique( { # << Note! name => 'Test', 'author'=> 'yeehaw', 'email' => 'yeehaw@yahoo.com', descrip => 'Fun', version => '1.11', build => 2 } # << Note! ); print Dumper( $fun, $q ); __DATA__ C:\test>202407 $VAR1 = { 'author' => 'nutshell', 'descrip' => 'That', 'build' => 2, 'version' => '1.1', 'email' => 'x@x.com', 'name' => 'Test Addon' }; $VAR1 = { 'author' => 'yeehaw', 'descrip' => 'Fun', 'build' => 2, 'version' => '1.11', 'email' => 'yeehaw@yahoo.com', 'name' => 'Test' }; $VAR1 = { 'author' => 'nutshell', 'descrip' => 'That', 'build' => 2, 'version' => '1.1', 'email' => 'x@x.com', 'name' => 'Test Addon' }; $VAR2 = { 'author' => 'yeehaw', 'descrip' => 'Fun', 'build' => 2, 'version' => '1.11', 'email' => 'yeehaw@yahoo.com', 'name' => 'Test' }; C:\test>

    Cor! Like yer ring! ... HALO dammit! ... 'Ave it yer way! Hal-lo, Mister la-de-da. ... Like yer ring!
Re: Processing (from a variable) the 'new' methods hash and storing it to a hash ref
by chromatic (Archbishop) on Oct 03, 2002 at 00:56 UTC

    What's the origin of $code? Does this do what you want?

    my $info = \%$fun; # or my $info = {}; @$info{ keys %$fun } = values %$fun;
Re: Processing (from a variable) the 'new' methods hash and storing it to a hash ref
by nutshell (Beadle) on Oct 03, 2002 at 01:34 UTC
    You've got exactly the right idea, BrowserUk, but if only it was that easy :(

    You see - I cannot let it physically execute the code; the only way is messy regex I'm thinking :\

    --nutshell

      In that case, you going to have to explain your problem a lot more clearly.

      What is "blow code"?

      You mention a var, $code twice, but it doesn't appear anywhere in your examples.

      What you appear to be saying is that you have a program source or sources, that makes calls to a function Lique() in a package CX, and that you want to parse these sources, locate each call to new CX::Lique() or CX::Lique->new() and the extract the parameters passes on these calls into a hashref %info using a regex?

      Am I closer?


      Cor! Like yer ring! ... HALO dammit! ... 'Ave it yer way! Hal-lo, Mister la-de-da. ... Like yer ring!
        Yes!

        That's exactly what I need to do :)

        --nutshell