← Back to Perl Course | Chapter 5: File I/O | Lesson 7 of 7

Standard Streams और In-memory Files

STDIN, STDOUT, और STDERR हमेशा open हैं, और आप एक string को भी एक file की तरह खोल सकते हैं।

Standard Streams और In-Memory Files

Perl आपके program शुरू होने से पहले input के लिए STDIN, normal output के लिए STDOUT, और errors और warnings के लिए STDERR खोलता है। print default से STDOUT में लिखता है, और आप एक handle name करके output कहीं और direct कर सकते हैं। एक scalar variable के एक reference को एक file की तरह खोलना आपको same file functions से एक string पढ़ने या लिखने देता है, जो testing के लिए बहुत handy है।

Note: STDERR, STDOUT जैसे buffered नहीं है, इसलिए error messages normal output के साथ out of order appear हो सकते हैं।

उदाहरण: Standard streams and in-memory files

perl
use strict;
use warnings;

print STDOUT "normal output\n";
print STDERR "an error message\n";

my $buffer = '';
open(my $mem, '>', \$buffer) or die "Cannot open in-memory file: $!";
print $mem "line one\n";
print $mem "line two\n";
close $mem;
print "buffer holds ", length($buffer), " characters\n";

open(my $read, '<', \$buffer) or die "Cannot read in-memory file: $!";
while (my $line = <$read>) {
    chomp $line;
    print "got: $line\n";
}
close $read;

# Output:
# normal output
# buffer holds 18 characters
# got: line one
# got: line two
{# 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. STDERR की बजाय STDOUT पर errors print करना
  2. यह भूल जाना कि STDIN एक line को इसकी newline समेत पढ़ता है
  3. string पढ़ने से पहले एक in-memory write handle close करना भूल जाना
चैप्टर सारांश
  • STDIN, STDOUT, और STDERR हमेशा available हैं
  • print default से STDOUT में जाता है
  • Warnings और die messages STDERR में जाते हैं
  • एक string को file की तरह इस्तेमाल करने के लिए एक scalar reference open करें
🔒

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.