From the question it sounds like you are struggling with the difference between the scope of a variable, and the persistence of data pointed to by a reference. The data ["elemnt1","elmnt2"] is assigned to a variable ($array_ref) that goes out of scope at the end of do_something. However, because it is returned, there is a reference to the data, and it persists even when $array_ref goes out of scope.
Last time I checked, perl used reference-counting for garbage collection, so that array will not be garbage-collected until the last reference to it goes out of scope, e.g., if you unset $ref at the end of your example.