use strict और use warnings
ये दो lines Perl को आपके typos और sloppy code catch कराती हैं।
In this page:
use strict और use warnings
use strict आपको my से variables declare करने के लिए force करता है और कई risky constructs रोकता है, इसलिए एक misspelled variable name एक compile-time error बन जाता है। use warnings suspicious चीज़ों के बारे में messages print करता है जैसे एक undefined value इस्तेमाल करना। लगभग हर serious Perl script दोनों lines से शुरू होता है।
Note:
Warnings STDERR में जाते हैं, इसलिए ये normal program output के साथ mix नहीं होते।
उदाहरण: use strict and use warnings
use strict;
use warnings;
my $total = 10;
my $result = eval q{ $totl + 1 }; # typo: $totl was never declared
if ($@) {
print "Caught a strict error:\n";
print $@ =~ /Global symbol "\$totl" requires explicit package name/ ? " undeclared variable \$totl\n" : " other error\n";
}
print "Total is $total\n";
# Output:
# Caught a strict error:
# undeclared variable $totl
# Total is 10
Login to try C/C++/Java/PHP code in the editor
आम गलतियां
- use strict छोड़ देना और typos से silently global variables बनाना
- warnings की वजह fix करने की बजाय इन्हें ignore करना
- my से variables declare करना भूल जाना
चैप्टर सारांश
- use strict declared variables require करता है
- use warnings suspicious code report करता है
- दोनों typos जल्दी catch करते हैं
- my से variables declare करें
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: