← Back to Perl Course | Chapter 8: Modules | Lesson 5 of 7

POSIX का उपयोग

POSIX module कई standard C-style math और time functions लाता है।

In this page:

  1. POSIX

POSIX

POSIX नीचे round करने और ऊपर round करने के लिए floor और ceil देता है, floating-point remainders के लिए fmod, और dates और times format करने के लिए strftime। यह INT_MAX और DBL_MAX जैसे constants भी define करता है। Default से सब कुछ import करना noisy है, इसलिए सिर्फ उन names के लिए पूछें जो आपको चाहिए।

Note: Perl का built-in int zero की ओर truncate करता है, जबकि POSIX floor हमेशा negative infinity की ओर round करता है।

उदाहरण: POSIX

perl
use strict;
use warnings;
use POSIX qw(floor ceil fmod strftime INT_MAX);

print "floor(3.7)=", floor(3.7), " ceil(3.2)=", ceil(3.2), "\n";
print "floor(-3.7)=", floor(-3.7), " int(-3.7)=", int(-3.7), "\n";
print "fmod(10.5, 3)=", fmod(10.5, 3), "\n";
print "INT_MAX=", INT_MAX, "\n";
print "epoch start: ", strftime("%Y-%m-%d %H:%M:%S", gmtime(0)), "\n";
print "a fixed moment: ", strftime("%A, %d %B %Y", gmtime(86400 * 365)), "\n";
printf "rounded: %d\n", floor(2.5 + 0.5);

# Output:
# floor(3.7)=3 ceil(3.2)=4
# floor(-3.7)=-4 int(-3.7)=-3
# fmod(10.5, 3)=1.5
# INT_MAX=2147483647
# epoch start: 1970-01-01 00:00:00
# a fixed moment: Friday, 01 January 1971
# rounded: 3
{# 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. सारा POSIX import करना और built-in names clobber करना
  2. यह उम्मीद करना कि int negative numbers नीचे round करेगा
  3. strftime को local time pass करना और अलग machines पर अलग output पाना
चैप्टर सारांश
  • floor और ceil नीचे और ऊपर round करते हैं
  • fmod एक floating-point remainder देता है
  • strftime time values format करता है
  • सिर्फ ज़रूरी names import करें
🔒

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.