First, a few comments on your code: delete completely deletes the element, including its descendants. So you should do this after moving the children. Then you cannot paste an element that's already part of a tree. You need to cut it, then paste it. Or in short, to move it. move, like paste, needs a position and a referent as arguments: $c->move( before => $p) would work.
Also, I am not sure of the test on $p->parent('li'): it will try to find an li ancestor to $p, which may, possibly, find false positives (a p buried in a table within a li), so you probably want $p->parent->tag eq 'li', or $p->parent->is( 'li')).
That said, try $p->erase if( $p->parent->is( 'li')), that might just do what you want.