http://www.perlmonks.org?node_id=1005341


in reply to Replacing If Elsif Else with Hash

It worked fine in the script as it was, but when I placed that script into a external subroutine, it broke things...

I endorse your desire to use a dispatch table of some kind rather than the original approach, but I am curious to know how things 'broke' when you copied the  if ... elsif ... else structure to other code. Can you elaborate?

Replies are listed 'Best First'.
Re^2: Replacing If Elsif Else with Hash
by johnfl68 (Scribe) on Nov 24, 2012 at 08:51 UTC

    To be honest, I am not exactly sure. The portion of code involves some simple Image::Magick and would run, but would not write the final output image from the external subroutine. I spent most of the day trying to figure out why the image wasn't being created.

    I ended up slowly rebuilding the subroutine with small portions, and running with each small addition until the point when the image wasn't being written again, and it turned out to be the if elsif else portion. In the end, I had a subroutine with exactly the same code, with everything except that section.

    I use strict and warnings, nothing ever showed up, so I can only assume there was some sort of collision with Image::Magick but not sure what. I even tried changing both $severity and $background to different string names, at first thinking maybe one of them was colliding with something, but still, no image would be written.

    With both the hash version I did, and the one suggested by frozenwithjoy, they both work fine in the external sub.

    I am still curious as to why it did not work that way, but in the end I am happy that I was at least able to track down what part of the code was causing the problem, and find a solution.

      johnfl68:

      If everything worked find (i.e. no error messages, etc.) and if failed only because of the addition of the if/else block, then I think I'd suspect one or more of your braces was a line too high or too low, causing the code to be bypassed or some such. Pure speculation, though.

      ...roboticus

      When your only tool is a hammer, all problems look like your thumb.