From the Super Mario Wiki
Coin Snakes are types of moving Coins in Super Mario World and Super Mario World: Super Mario Advance 2. They are usually released from certain ? Blocks, and they move to the direction the player presses on the D-Pad (directional buttons) on the controller or Game Boy Advance. They are found in stages such as Donut Ghost House, Donut Secret 2, Valley Ghost House and Star World 5. If Mario or Luigi hits a P-Switch, the Coin Snake will become solid blocks, similar to Snake Blocks, which Mario or Luigi can ride and control the blocks at the same time. However, if the Coin Snake or Coin Snake blocks touch a wall or obstacle, they will stop moving and can no longer be controlled. Coin Snakes also appear in the World-e level Doors o' Plenty from Super Mario Advance 4: Super Mario Bros. 3 where they serve the same purpose.