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

Hello all,
I've got to migrate a set of embedded (in excel workbooks) function calls from one arg list signature to another.

ie: funca(a,b,c,d) -> func(a,b,XXX,YYY,d) or funcb(a,b,c,d) -> func(a,b,,,d)
there are ~10 functions that need translating into ~6 new targets.

I've been building a pair of hashes, one for the func name translations and another for the arglists. But this is getting very c-ish and clunky. Can someone suggest a different, perhaps more general, approach? rather than hard wiring a set of hashes...

thanks,
-Tom

20030203 - Edit by Corion : Added formatting

Replies are listed 'Best First'.
Re: translate arglist
by Hofmator (Curate) on Feb 03, 2003 at 13:33 UTC
    Why not just use one hash, the key is the function name and the argument is an array consisting of search regex and replacement. Something like the following:
    use constant SEARCH => 0; use constant REPLACE => 1; my %translate = ( funca => [ qr/funca\( ([^,]+) , ([^,]+) ,([^,]+) ,([^)]+) \)/x, '"func($1,$2,XXX,YYY,$4)"' ], funcb => [ qr/funcb\( ([^,]+) , ([^,]+) ,([^,]+) ,([^)]+) \)/x, '"func($1,$2,,,$4)"' ], ); while (<DATA>) { for my $key (keys %translate) { if (/$key/) { s/$translate{$key}->[SEARCH]/$translate{$key}->[REPLACE]/e +e; print; } } } __DATA__ funca(10,20,30,40) funcb(10,20,30,40)
    Of course you might have to adjust the regexes a bit, taking care of whitespace, ... but I think you get the idea.

    -- Hofmator

      How would I construct a regex that would also allow a parameter to the func (could be any parameter!) to also be a func with args... ugh. I found several cases where the arglist is not simple. i.e. funca(1,2,func35(A,B),3) in this case I need a regex that'll set $1->1, $2->2, $3->func35(A,B), $4->3.
Re: translate arglist
by denap (Initiate) on Feb 03, 2003 at 15:41 UTC
    brilliant! thanks very much for the structure. I was having a real hard time getting my hands around the issue; bungling... It took me a few moments to grok what you were doing, but it looks like it'll work great for me. thanks again! -T