开发者

How can I store entire contents of a Perl array to a scalar variable?

开发者 https://www.devze.com 2022-12-17 05:40 出处:网络
How can I store en开发者_开发技巧tire contents of an array to a scalar variable. eg: my $code = do { local $/; <FILE HANDLE>; };

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
0

精彩评论

暂无评论...
验证码 换一张
取 消