How can I store en开发者_开发技巧tire 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.
Use join
.
my $str = join '', @array;
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.
@arr = ("1","2","3") ;
my $arr = "@arr" ;
print "$arr";
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
精彩评论