in reply to Re: elegant way of handling hash?
in thread elegant way of handling hash?

hi doward, i will try to be more clear .
my %replacehash; $replacehash{TEMP1} = $flow->getExeName(); $replacehash{TEMP2} = $flow->getTempName(); $replacehash{TEMP3} = $flow->getData()->getNameType(); ....
All the API's are different .

Replies are listed 'Best First'.
Re^3: elegant way of handling hash?
by BrowserUk (Patriarch) on Jan 19, 2006 at 10:19 UTC

    You can do the first two quite easily

    @replacehash{ map{ "TEMP$_" } 1 .. 2 } = map{ $flow->$_ } qw[getExeName getTempName];

    Doing the third one, with the double level of indirection, would probably require a string eval.

    Whether you should do this is another matter entirely. It would have to be a pretty long list to warrent it, and even then you would probably be better having the $flow class provide an API that returned the hash. That way the construction need only be done once and the user of the $flow object can just do

    my %replacehash = $flow->getAll();

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      It wouldn't need a string eval, but it does start to become convoluted. Something like this should work:
      @replacehash{ map{ "TEMP$_" } 1 .. 2 } = map{ use List::Util 'reduce'; reduce {$a->$b} ($flow, split /->/, $_); } qw[getExeName getTempName getData->getNameType];

      Caution: Contents may have been coded under pressure.
Re^3: elegant way of handling hash?
by ambrus (Abbot) on Jan 19, 2006 at 16:47 UTC

    You could try

    my %replacehash = ( TEMP1 => $flow->getExeName(), TEMP2 => $flow->getTempName(), TEMP3 => $flow->getData()->getNameType(), )
    (If you call a method with no arguments, like here, you could also omit the empty pair of parentheses, but that's not the point here.)

    Also

    my %replacehash; @replacehash{map { "TEMP$_" } 1 .. 3} = ( $flow->getExeName(), $flow->getTempName(), $flow->getData()->getNameType(), )