我是 Perl 的新手,做一些需要我通过函数输入和输出变量的练习。我使用全局变量创建了相同的脚本,并且它有效,但是在我尝试使用我的并将所有内容设置为严格之后,我遇到了一堆问题。我的语法关闭了吗?还是更基本的东西?任何帮助,将不胜感激:
#!/usr/bin/perl -w
use warnings;
use strict;
use DBI;
use Getopt::Std;
getopt('s:');
my $opt_s;
my $search = $opt_s;
my @array = &function1();
my $arrayvalue = &function2();
&function1($search);
&function2(@array);
if (&function2(@array) != 0 {
print "no values found for '$search'","\n"};
sub function1 {
my search = $_[0];
our $dbh = DBI->connect("dbi:mysql:dbname=database, "user", "password")
or die $DBI::errstr;
my $sql = $dbh->selectall_arrayref("SELECT Player from Players_Sport where Sport like '$search'")
or die $DBI::errstr;
my @array = map { $_->[0] } @$sql;
$dbh->disconnect
or warn "Disconnection failed: $DBI::errstr\n";
return @array;
}
sub function2 {
my $arrayvalue=()
my @array = $_[0];
print join("\n", @array, "\n");
if(scalar (@array) == 0) {
$arrayvalue = -1;
}
else {$arrayvalue = 0;
};
return $arrayvalue;
}