You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Perl数据库程序报错:open函数第一个参数后缺少逗号

Fixing Perl Open Function Errors & Script Improvements

Alright, let's tackle those "Missing comma after first argument to open function" errors you're seeing—they're straightforward fixes, and we'll clean up a few other potential issues in your script too.

Direct Cause of the Errors

Perl's two-argument open syntax requires a comma separating the filehandle and the mode/filename string. You omitted this comma in all your open calls. For example:

  • Wrong: open PASSWORD ">$name.pswrd";
  • Correct (two-argument): open PASSWORD, ">$name.pswrd";

That said, three-argument open with lexical filehandles is better practice—it's safer (prevents filename injection) and avoids global variable clutter. We'll use that in the fixed script.

Other Script Issues to Fix

  • $a and $b are special Perl variables for sorting—replace them with custom names like $username or $input_pass to avoid bugs.
  • The login block has two useless lines (PASSWORD; and "..";)—delete them.
  • You forgot to close the password filehandle in the login block.
  • Add error checking (or die) to open calls so you know why file operations fail (e.g., missing user directory).
  • Stored names/passwords will have newline characters saved to files—add chomp to fix mismatches between input and stored values.

Fixed Script Code

sub change { 
    my($dir) = @_; 
    chdir $dir or die "ERROR: Could not change to directory $dir: $!"; 
}

print "Do you want to register or log-in? ";
chomp(my $action = <STDIN>);

if($action =~ /register/i) {
    print "What is your name: ";
    chomp(my $name = <STDIN>);
    print "What is your password: ";
    chomp(my $password = <STDIN>);

    change "name";
    # Use three-argument open with lexical filehandle
    open my $name_fh, '>', "$name.nme" or die "Can't create name file: $!";
    print $name_fh "$name";
    close $name_fh;
    change "..";

    change "password";
    open my $pass_fh, '>', "$name.pswrd" or die "Can't create password file: $!";
    print $pass_fh "$password";
    close $pass_fh;
    change "..";

    print "Complete.\n";
} else {
    print "Name: ";
    chomp(my $username = <STDIN>);
    print "Password: ";
    chomp(my $input_pass = <STDIN>);

    change "name";
    open my $name_fh, '<', "$username.nme" or die "Can't open name file: $!";
    my $stored_name = <$name_fh>;
    chomp $stored_name; # Remove newline from file
    close $name_fh;
    change "..";

    change "password";
    open my $pass_fh, '<', "$username.pswrd" or die "Can't open password file: $!";
    my $stored_pass = <$pass_fh>;
    chomp $stored_pass; # Remove newline from file
    close $pass_fh;
    change "..";

    if($username eq $stored_name and $input_pass eq $stored_pass) {
        print "Log-in successful!\n";
        sleep(2);
        print "Hello, $username!\n";
    } else {
        print "Invalid username or password.\n";
    }
}

Key Improvements Explained

  • Lexical filehandles (my $name_fh) instead of global ones are scoped to the block, so they clean up automatically if you forget to close them.
  • Three-argument open separates the mode (> for write, < for read) from the filename, eliminating ambiguity and security risks.
  • Added chomp to stored values to fix newline-related mismatches between user input and saved data.
  • Added an explicit failure message for login attempts to give clear feedback to users.

内容的提问来源于stack exchange,提问作者Xedrian

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 06:27:41