in reply to Sed in perl

It's hard to see spaces, so I'll replace spaces with underscores for clarity.

In your first example, it looks to me like you're doing:

   $mod = "ABC_";
   $hier = `tac temp.xx | sed -n '/$mod_/,/YYY/ p'| grep XXX`
so with the substitution, your second line is actually:
   $hier = `tac temp.xx | sed -n '/ABC__/,/YYY/ p'| grep XXX`
If I'm misreading the spaces, I apologize, but it appears to me that you're looking for ABC followed by 2 spaces. In the shell command, you're only looking for 1 space.