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
$aand$bare special Perl variables for sorting—replace them with custom names like$usernameor$input_passto 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) toopencalls so you know why file operations fail (e.g., missing user directory). - Stored names/passwords will have newline characters saved to files—add
chompto 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
openseparates the mode (>for write,<for read) from the filename, eliminating ambiguity and security risks. - Added
chompto 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
相关产品推荐
相关产品推荐

