in reply to Passing variables

Your subroutine is acting as programmed. You get a single array of arguments to work with in your subroutine.
&writefile("file.txt", @array);
arguments as writefile sees them:
file.txt, 1rst array element, second array element, third array element, etc...
So you are getting the file name as the first argument($_[0]), first array element as the second argument($_[1]), second array element as the third argument($_[2])

What you could do to be clearer and in similar style:
sub writefile { my ($filename, @array) = @_; open(FILE, ">$filename") || return ($!); print FILE @array; #nothing in between? close(FILE); return 0; }
Though I would pass a reference to the array since that would be quicker. Hint: \@array and @$arref