The code for bar pulling will depend on your machine. The shortest way to program it that I know is to use sub programs with nested calls. On some machines, Haas is one, you can have all the subs in one long program but on other machines they have to be completely separate programs such as in this description.
First program starts machine with the bar ready for first piece, and uses M98 with an L count to call second program.
Second program does first piece and parts it off then calls third program using M98 but no count.
Third program pulls the bar and the machine returns to the second program with M99. The second program also has M99 immediately after the call for the bar pull program so it returns to the first program and the sequence repeats until the L counts down to zero, then the first program stops the machine.
An open mind is a virtue...so long as all the common sense has not leaked out.