← Back to Perl Course | Chapter 1: Introduction | Lesson 5 of 7

use strict और use warnings

ये दो lines Perl को आपके typos और sloppy code catch कराती हैं।

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

perl
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
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. use strict छोड़ देना और typos से silently global variables बनाना
  2. warnings की वजह fix करने की बजाय इन्हें ignore करना
  3. 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.