37,719
社区成员
发帖
与我相关
我的任务
分享
my $str="foo";
my @foo=(1,2,3);
如果通过$str得到$foo[0]的值并打印出来呢?
use strict;
use warnings;
{
my @foo = (1,2,3);
my $str = "foo";
print eval("\${$str}[0]"), "\n";
}
{
no strict 'refs';
our @foo = (1,2,3); # must be a package variale
my $str = "foo";
print ${$str}[0], "\n";
}
E:\reading\perl\ex>D:\Progs\Perl\bin\perl.exe -v
This is perl, v5.10.0 built for MSWin32-x86-multi-thread
(with 5 registered patches, see perl -V for more detail)
Copyright 1987-2007, Larry Wall
Binary build 1004 [287188] provided by ActiveState http://www.ActiveState.com
Built Sep 3 2008 13:16:37
Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.
Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl". If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.
E:\reading\perl\ex>test.pl
1
$perl -v
This is perl, v5.8.8 built for x86_64-linux-gnu-thread-multi
#!/usr/bin/perl
#
#use strict;
#
$str="foo";
#@$str=(1,2,3);
@foo=(1,2,3);
print "@$str[0]\n";