How can I store entire contents of an array to a scalar variable. eg:
my $code = do { local $/; <FILE HANDLE>; };
This works fine for file handles but I need this for an array.
How can I store entire contents of an array to a scalar variable. eg:
my $code = do { local $/; <FILE HANDLE>; };
This works fine for file handles but I need this for an array.
You can actually use a scalar variable as a filehandle:
my $bigbuffer;
my $f;
open $f, ">", \$bigbuffer; # opens $f for writing into the variable $bigbuffer
# do whatever prints fwrites etc you want here
You can also take a reference to the array:
my @array = 'a'..'z';
my $scalar = \@array;
foo( $scalar );
sub foo {
my $array_ref = shift;
for my $f ( @$array_ref ) {
do_something( $f );
}
}
Which approach you take really depends on what you are trying to accomplish.