Hi Pearl12345,
For your more information on your error(not error at all, it is a warning) message, I am giving you the use of warnings pragma with the exampls, similar to your warning message.
Warnings
The most important tool for writing good Perl is the 'warnings' flag, the -w command line switch. You can turn on warnings by placing -w on the first line of your programs like so:
#!/usr/local/bin/perl -w
Or, if you're running a program from the command line, you can use -w there, as in perl -w myprogram.pl.
Turning on warnings will make Perl yelp and complain at a huge variety of things that are almost always sources of bugs in your programs. Perl normally takes a relaxed attitude toward things that may be problems; it assumes that you know what you're doing, even when you don't.
Here's an example of a program that Perl will be perfectly happy to run without blinking, even though it has an error on almost every line!
#!/usr/local/bin/perl
$filename = "./logfile.txt";
open (LOG, $fn);
print LOG "Test\n";
close LOGFILE;
Now, add the -w switch to the first line, and run it again. You should see something like this:
Name "main::filename" used only once: possible typo at ./a6-warn.pl line 3. Name "main::LOGFILE" used only once: possible typo at ./a6-warn.pl line 6. Name "main::fn" used only once: possible typo at ./a6-warn.pl line 4. Use of uninitialized value at ./a6-warn.pl line 4. print on closed filehandle main::LOG at ./a6-warn.pl line 5.
Here's what each of these errors means:
1. Name "main::filename" used only once: possible typo at ./a6-warn.pl line 3. and Name "main::fn" used only once: possible typo at ./a6-warn.pl line 4. Perl notices that $filename and $fn both only get used once, and guesses that you've misspelled or misnamed one or the other. This is because this almost always happens because of typos or bugs in your code, like using $filenmae instead of $filename, or using $filename throughout your program except for one place where you use $fn
2. Name "main::LOGFILE" used only once: possible typo at ./a6-warn.pl line 6. In the same way that we made our $filename typo, we mixed up the names of our filehandles: We use LOG for the filehandle while we're writing the log entry, but we try to close LOGFILE instead.
3. Use of uninitialized value at ./a6-warn.pl line 4. This is one of Perl's more cryptic complaints, but it's not difficult to fix. This means that you're trying to use a variable before you've assigned a value to it, and that is almost always an error. When we first mentioned $fn in our program, it hadn't been given a value yet. You can avoid this type of warning by always setting a default value for a variable before you first use it.
4. print on closed filehandle main::LOG at ./a6-warn.pl line 5. We didn't successfully open LOG, because $fn was empty. When Perl sees that we are trying to print something to the LOG filehandle, it would normally just ignore it and assume that we know what we're doing. But when -w is enabled, Perl warns us that it suspects there's something afoot.
So, how do we fix these warnings? The first step, obviously, is to fix these problems in our script. (And while we're at it, I deliberately violated our rule of always checking if open() succeeded! Let's fix that, too.) This turns it into:
#!/usr/local/bin/perl -w
$filename = "./logfile.txt";
open (LOG, $filename) or die "Couldn't open $filename: $!";
print LOG "Test\n";
close LOG;
Now, we run our corrected program, and get this back from it:
Filehandle main::LOG opened only for input at ./a6-warn2.pl line 5.
Where did this error come from? Look at our open(). Since we're not preceding the filename with > or >>, Perl opens the file for reading, but in the next line we're trying to write to it with a print. Perl will normally let this pass, but when warnings are in place, it alerts you to possible problems. Change line 4 to this instead and everything will be great:
open (LOG, ">>$filename") or die "Couldn't open $filename: $!";
The <-w> flag is your friend. Keep it on at all times. You may also want to read the <perldiag> man page, which contains a listing of all the various messages (including warnings) Perl will spit out when it encounters a problem. Each message is accompanied by a detailed description of what the message means and how to fix it.
Source from: Perl.com
|