in reply to How to pass array literal through pass by reference

You are mixing paradigms here. The easiest way to do what you want would be to insert your list into an anonymous array, i.e.:

WriteToFile([qw(abc 23343)]);

See perlref and Quote-and-Quote-like-Operators for details.

Update: Incidentally, the qw list generator does not function like you may think - it uses whitespace as a delimiter, so commas and quotes are treated as literals, likely not what you want (I don't know for certain, obviously.) This is discussed in the link above. If you don't want to think about that, you could just as easily say:

WriteToFile( ["abc", "23343"] );

Replies are listed 'Best First'.
Re^2: How to pass array literal through pass by reference
by pravlm (Acolyte) on Mar 16, 2009 at 20:08 UTC
    Thanks for the reply, please make me understand, does using the "[" brackets will pass the parameter by ref?

      Please read the link I provided you (perlref) as it discusses anonymous arrays. A look through perlreftut would also likely do you some good.

      No matter what you do in Perl, arguments are always passed as an explicit list of values, just like in C. In passing by reference, you are really passing a pointer to an array. You can generate such a pointer with the \@array syntax that you used in your example code (also discussed in perlref). The syntax [1, 2, 3] returns a pointer to an array (1,2,3). That array has no symbolic representation within your namespace, and is thus "anonymous".

      So the short answer is that the syntax

      WriteToFile( ["abc", "23343"] );

      passes a reference to an array containing ("abc", "23343") to the subroutine WriteToFile.

      If you think of "pass by reference" in C terms, Perl subs always receive their arguments in a "pass by reference" fashion. Arguments come in the array, @_, and the elements of that array are what we call aliases to the original values. If you modify an element of that array, you are modifying the original value.

      my $so_called_constant = 'unmodified'; print "constant: $so_called_constant\n"; make_modified( $so_called_constant ); print "constant: $so_called_constant\n"; make_modified( 'literal' ); sub make_modified { $_ = 'modified' for @_ } __END__ constant: unmodified constant: modified Modification of a read-only value attempted

      Note that there are other places where these aliases appear naturally, and you can create them deliberately.

      What we call a "reference", a C programmer would probably call a "pointer". The brackets in "[ 1, 2, 3 ]" create a reference to an array that doesn't have a name. It's exactly like the reference you get from "\@triplet" except that there isn't a variable that holds the array whence came the reference.