Lord Olleus said:
Hanibals army was made up mostly of mercenaries and barbarians, and very few of those thought in a hellenistic formation. I also don't think you can say that pyrhus really beat the romans, it was more of a draw.
Also, the advantage the legions had was there ability to attack flanks quickly. It is that advantage which makes them supperior, as during a battle the probability that one of the huge phalanxes will show its flank is very high. It just shows that movement > strength.
It wasnt so simple. The opposition phalanx-easy to outflank vs manoevroable legion is a kind of school's truth. Simplification made to quickly explain problem.
First, there were at least two kinds of phalanx - original greek from V-IV century, armed with 3-3,6m spear, armour, large shield and sword, but more universal and flexible, and macedonian sarissaphoroi (or pedzetairoi), armed with 5-6m spear, light armour, small shield and dagger alike sword.
Sarissaphoroi were very specialized unit, excellent to stop enemy and engage him in fight, but depending on other units which should in the meantime protect their flanks and conduct flanking attack. They could be effective only in well coordinated and trained army, that successfully implemented combined arms doctrine.
Second, good commanders like Philip, Alexander, Hannibal or Pyrrhus could use (and used!) them quite flexible and covered weak points with lighter troops with ease. You say, Hannibal's army consisted of barbarians - but for Romans every nation but theirs was barbarian. Iberians, Libyans, Celts, latin allies were all as civilized as Romans that time. Moreover, his army was organized in Hellenistic pattern, and great victory at the Cannaes was achieved much in the way of hellenistic combined arms.
Third, Pyrrhus - we shouldnt follow roman historians' lies about him, as they wanted to diminish his greatness. He won at Ausculum and Heraclea, and most probably at Beneventum - though Romans said it was their victory, it could be such only in strategical sense as Pyrrhus couldnt continue his campaign. The fact is, that Romans were so scared of him they didnt even try to take Tarentum until his death. Why he lose? Well, he had to fight for power since his childhood, and having initially nothing, he became one of the most important Greek leaders. Because of continuous wars and very little resource base, he had been short on money and men for almost all the time. To get what he needed, he fought successfully against Romans, Carthaginians and Antigonos in Greece. He could still win, but had just bad luck and got incidentally killed at Argos.
Fourth - Romans' army wasnt that far from phalanx as many people thinks. Initially it fought in typical combined arms order with phalanx formation. They changed it however after many fights with Iberians, who used guerilla tactics against them. It wasnt opposition to greek phalanx and combined arms though, I would even say it was more logical evolution of original idea. They simple put more attention to the heavy infantry, armed with swords and well protected with armour and large shield as much more capable in close combat and less vulnerable to ambush. They didnt resign of spearman though - armed with long hastae triarii still played vital role in Roman warfare. Similarily to hellenistic armies, success of the whole army heavily depended on collaboration between units. The main advantages of Romans were morale, manpower and cheap yet well trained citizen army. Even if they lose several times - as was in Hannibal's and Pyrrhus' cases - they could replace each lost soldier with two new ones, while their enemies not. I would also add, that Hannibal and Pyrrhus were great personalities and functional scheme of their armies were an effect of individual military genius. In Rome, army organization was a matter of state, so great leaders died, but army organization stayed for centuries.