in reply to Passing variables

The problem is that a list (ie @foo) inside another list (ie @_) gets expanded -- the list items are all there, but no longer enclosed in a distinct list. If @foo = (a, b, c), then your subroutine sees $_[1] as 'a', $_[2] as 'b', etc.

To fix this, you could just print all of the remaining items in @_ inside writeFile, or else you could preserve @file's identity as a distinct list by passing it in as a reference:

&writeFile("file.txt", \@file);
Now, as far as writeFile is concerned, $_[1] contains a reference that points to all of @file, not just the first line. To use this correctly, you must also modify the print statement slightly:
print FILE @$_[1];
'@$_[1]' indicates that the program should access the array or '@' that '$_[1]' is a reference to. It can also be written as @{$_[1]}, if that helps.

See perldoc perlreftut for a references tutorial, not to mention the various O'Reilly books.

-- Frag.

Replies are listed 'Best First'.
Re: Re: Passing variables
by Stamp_Guy (Monk) on Jun 15, 2001 at 19:08 UTC
    Thanks Frag. That makes sense. I appreciate the explanation. Thanks for spelling it out for me.

    Stamp_Guy