File: perlfaq8.html

package info (click to toggle)
perl-doc-html 5.10.0-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 23,252 kB
  • ctags: 22,194
  • sloc: makefile: 6
file content (1100 lines) | stat: -rw-r--r-- 104,853 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
  <title>perlfaq8 - perldoc.perl.org</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <meta http-equiv="Content-Language" content="en-gb" />
  <link href="css.css" rel="stylesheet" rev="stylesheet" type="text/css" media="screen" />
</head>

<script language="JavaScript" type="text/javascript" src="label.js"></script>

<script language="JavaScript">
  pageDepth = 0;
  setPath();
</script>

<body onLoad="showToolbars();loadLabels()">

<div id="pageHeader">
  <div id="pageHeaderLogo">
    <img src="onion.gif">
  </div>
  <div id="pageHeaderText">
    <a href="http://perldoc.perl.org">perldoc.perl.org</a>
  </div>
</div>

<div id="pageBody">
  <div id="left">
    <div id="leftContent">
      <div id="leftClose">
        <a href="#" onClick="closeLeft()" title="Hide navigation" onmouseover="leftCloseIcon.src='close_purple.gif';" onmouseout="leftCloseIcon.src='close_blue.gif';"><img src="close_blue.gif" name="leftCloseIcon" id="leftCloseIcon" border=0></a>
      </div>
      <h1>Manual:</h1>
      <ul>
        <li><a href="index-overview.html">Overview</a></li>
        <li><a href="index-tutorials.html">Tutorials</a></li>
        <li><a href="index-faq.html">FAQs</a></li>
        <li><a href="index-history.html">History / Changes</a></li>
        <li><a href="index-licence.html">Licence</a></li>
      </ul>
      <h2>Reference:</h2>
      <ul>
        <li><a href="index-language.html">Language</a></li>
        <li><a href="index-functions.html">Functions</a></li>
        <li><a href="perlop.html">Operators</a></li>
        <li><a href="perlvar.html">Special variables</a></li>
        <li><a href="index-pragmas.html">Pragmas</a></li>
        <li><a href="index-modules-A.html">Core modules</a></li>
        <li><a href="index-utilities.html">Utilities</a></li>
        <li><a href="index-internals.html">Internals</a></li>
        <li><a href="index-platforms.html">Platform specific</a></li>
      </ul>
      <h2>Links:</h2>
      <ul>
        <li><a href="http://search.cpan.org">CPAN</a></li>
        <li><a href="http://www.perl.org">Perl.org</a></li>
        <li><a href="http://www.perl.com">Perl.com</a></li>
        <li><a href="http://perlbuzz.com">Perl Buzz</a></li>
        <li><a href="http://www.perlfoundation.org/perl5/index.cgi">Perl 5 Wiki</a></li>
        <li><a href="http://jobs.perl.org">Perl Jobs</a></li>
        <li><a href="http://www.pm.org">Perl Mongers</a></li>
        <li><a href="http://www.perlmonks.org">Perl Monks</a></li>
        <li><a href="http://planet.perl.org">Planet Perl</a></li>
        <li><a href="http://use.perl.org">Use Perl</a></li>
      </ul>
      <h2>Contact:</h2>
      <ul>
        <li>Site maintained by<br><a href="http://perl.jonallen.info">Jon Allen</a>
            (<a href="http://perl.jonallen.info">JJ</a>)</li>
        <li class="spaced">Last updated on<br>23 December 2007</li>
	<li class="spaced">See the <a href="http://perl.jonallen.info/projects/perldoc">project page</a> for
	more details</li>
      </ul>
    </div>
  </div>

  <div id="center">  
    <div id="centerContent">
      <div id="contentHeader">
        <div id="contentHeaderLeft"><a href="#" onClick="showLeft()">Show navigation</a></div>
        <div id="contentHeaderCentre">-- Perl 5.10.0 documentation --</div>
        <div id="contentHeaderRight"><a href="#" onClick="showRight()">Show toolbar</a></div>
      </div>
      <div id="breadCrumbs"><a href="index.html">Home</a> &gt; <a href="index-faq.html">FAQs</a> &gt; perlfaq8</div>
      <script language="JavaScript">fromSearch();</script>
      <div id="contentBody"><div class="title_container"><div class="page_title">perlfaq8</div></div><ul><li><a href="#NAME">NAME</a><li><a href="#DESCRIPTION">DESCRIPTION</a><ul><li><a href="#How-do-I-find-out-which-operating-system-I'm-running-under%3f">How do I find out which operating system I'm running under?</a><li><a href="#How-come-exec()-doesn't-return%3f">How come exec() doesn't return?</a><li><a href="#How-do-I-do-fancy-stuff-with-the-keyboard%2fscreen%2fmouse%3f">How do I do fancy stuff with the keyboard/screen/mouse?</a><li><a href="#How-do-I-print-something-out-in-color%3f">How do I print something out in color?</a><li><a href="#How-do-I-read-just-one-key-without-waiting-for-a-return-key%3f">How do I read just one key without waiting for a return key?</a><li><a href="#How-do-I-check-whether-input-is-ready-on-the-keyboard%3f">How do I check whether input is ready on the keyboard?</a><li><a href="#How-do-I-clear-the-screen%3f">How do I clear the screen?</a><li><a href="#How-do-I-get-the-screen-size%3f">How do I get the screen size?</a><li><a href="#How-do-I-ask-the-user-for-a-password%3f">How do I ask the user for a password?</a><li><a href="#How-do-I-read-and-write-the-serial-port%3f">How do I read and write the serial port?</a><li><a href="#How-do-I-decode-encrypted-password-files%3f">How do I decode encrypted password files?</a><li><a href="#How-do-I-start-a-process-in-the-background%3f">How do I start a process in the background?</a><li><a href="#How-do-I-trap-control-characters%2fsignals%3f">How do I trap control characters/signals?</a><li><a href="#How-do-I-modify-the-shadow-password-file-on-a-Unix-system%3f">How do I modify the shadow password file on a Unix system?</a><li><a href="#How-do-I-set-the-time-and-date%3f">How do I set the time and date?</a><li><a href="#How-can-I-sleep()-or-alarm()-for-under-a-second%3f">How can I sleep() or alarm() for under a second?
   </a><li><a href="#How-can-I-measure-time-under-a-second%3f">How can I measure time under a second?
   </a><li><a href="#How-can-I-do-an-atexit()-or-setjmp()%2flongjmp()%3f-(Exception-handling)">How can I do an atexit() or setjmp()/longjmp()? (Exception handling)</a><li><a href="#Why-doesn't-my-sockets-program-work-under-System-V-(Solaris)%3f--What-does-the-error-message-%22Protocol-not-supported%22-mean%3f">Why doesn't my sockets program work under System V (Solaris)?  What does the error message "Protocol not supported" mean?</a><li><a href="#How-can-I-call-my-system's-unique-C-functions-from-Perl%3f">How can I call my system's unique C functions from Perl?</a><li><a href="#Where-do-I-get-the-include-files-to-do-ioctl()-or-syscall()%3f">Where do I get the include files to do ioctl() or syscall()?</a><li><a href="#Why-do-setuid-perl-scripts-complain-about-kernel-problems%3f">Why do setuid perl scripts complain about kernel problems?</a><li><a href="#How-can-I-open-a-pipe-both-to-and-from-a-command%3f">How can I open a pipe both to and from a command?</a><li><a href="#Why-can't-I-get-the-output-of-a-command-with-system()%3f">Why can't I get the output of a command with system()?</a><li><a href="#How-can-I-capture-STDERR-from-an-external-command%3f">How can I capture STDERR from an external command?</a><li><a href="#Why-doesn't-open()-return-an-error-when-a-pipe-open-fails%3f">Why doesn't open() return an error when a pipe open fails?</a><li><a href="#What's-wrong-with-using-backticks-in-a-void-context%3f">What's wrong with using backticks in a void context?</a><li><a href="#How-can-I-call-backticks-without-shell-processing%3f">How can I call backticks without shell processing?</a><li><a href="#Why-can't-my-script-read-from-STDIN-after-I-gave-it-EOF-(%5eD-on-Unix%2c-%5eZ-on-MS-DOS)%3f">Why can't my script read from STDIN after I gave it EOF (^D on Unix, ^Z on MS-DOS)?</a><li><a href="#How-can-I-convert-my-shell-script-to-perl%3f">How can I convert my shell script to perl?</a><li><a href="#Can-I-use-perl-to-run-a-telnet-or-ftp-session%3f">Can I use perl to run a telnet or ftp session?</a><li><a href="#How-can-I-write-expect-in-Perl%3f">How can I write expect in Perl?</a><li><a href="#Is-there-a-way-to-hide-perl's-command-line-from-programs-such-as-%22ps%22%3f">Is there a way to hide perl's command line from programs such as "ps"?</a><li><a href="#I-%7bchanged-directory%2c-modified-my-environment%7d-in-a-perl-script.--How-come-the-change-disappeared-when-I-exited-the-script%3f--How-do-I-get-my-changes-to-be-visible%3f">I {changed directory, modified my environment} in a perl script.  How come the change disappeared when I exited the script?  How do I get my changes to be visible?</a><li><a href="#How-do-I-close-a-process's-filehandle-without-waiting-for-it-to-complete%3f">How do I close a process's filehandle without waiting for it to complete?</a><li><a href="#How-do-I-fork-a-daemon-process%3f">How do I fork a daemon process?</a><li><a href="#How-do-I-find-out-if-I'm-running-interactively-or-not%3f">How do I find out if I'm running interactively or not?</a><li><a href="#How-do-I-timeout-a-slow-event%3f">How do I timeout a slow event?</a><li><a href="#How-do-I-set-CPU-limits%3f">How do I set CPU limits?
  </a><li><a href="#How-do-I-avoid-zombies-on-a-Unix-system%3f">How do I avoid zombies on a Unix system?</a><li><a href="#How-do-I-use-an-SQL-database%3f">How do I use an SQL database?</a><li><a href="#How-do-I-make-a-system()-exit-on-control-C%3f">How do I make a system() exit on control-C?</a><li><a href="#How-do-I-open-a-file-without-blocking%3f">How do I open a file without blocking?</a><li><a href="#How-do-I-tell-the-difference-between-errors-from-the-shell-and-perl%3f">How do I tell the difference between errors from the shell and perl?</a><li><a href="#How-do-I-install-a-module-from-CPAN%3f">How do I install a module from CPAN?</a><li><a href="#What's-the-difference-between-require-and-use%3f">What's the difference between require and use?</a><li><a href="#How-do-I-keep-my-own-module%2flibrary-directory%3f">How do I keep my own module/library directory?</a><li><a href="#How-do-I-add-the-directory-my-program-lives-in-to-the-module%2flibrary-search-path%3f">How do I add the directory my program lives in to the module/library search path?</a><li><a href="#How-do-I-add-a-directory-to-my-include-path-(%40INC)-at-runtime%3f">How do I add a directory to my include path (@INC) at runtime?</a><li><a href="#What-is-socket.ph-and-where-do-I-get-it%3f">What is socket.ph and where do I get it?</a></ul><li><a href="#REVISION">REVISION</a><li><a href="#AUTHOR-AND-COPYRIGHT">AUTHOR AND COPYRIGHT</a></ul><a name="NAME"></a><h1>NAME</h1>
<p>perlfaq8 - System Interaction ($Revision: 10183 $)</p>
<a name="DESCRIPTION"></a><h1>DESCRIPTION</h1>
<p>This section of the Perl FAQ covers questions involving operating
system interaction.  Topics include interprocess communication (IPC),
control over the user-interface (keyboard, screen and pointing
devices), and most anything else not related to data manipulation.</p>
<p>Read the FAQs and documentation specific to the port of perl to your
operating system (eg, <a href="perlvms.html">perlvms</a>, <a href="perlplan9.html">perlplan9</a>, ...).  These should
contain more detailed information on the vagaries of your perl.</p>
<a name="How-do-I-find-out-which-operating-system-I'm-running-under%3f"></a><h2>How do I find out which operating system I'm running under?</h2>
<p>The $^O variable ($OSNAME if you use English) contains an indication of
the name of the operating system (not its release number) that your perl
binary was built for.</p>
<a name="How-come-exec()-doesn't-return%3f"></a><h2>How come exec() doesn't return?</h2>
<p>Because that's what it does: it replaces your currently running
program with a different one.  If you want to keep going (as is
probably the case if you're asking this question) use system()
instead.</p>
<a name="How-do-I-do-fancy-stuff-with-the-keyboard%2fscreen%2fmouse%3f"></a><h2>How do I do fancy stuff with the keyboard/screen/mouse?</h2>
<p>How you access/control keyboards, screens, and pointing devices
("mice") is system-dependent.  Try the following modules:</p>
<ul>
<li><a name="Keyboard"></a><b>Keyboard</b>
<pre class="verbatim">	<span class="w">Term::Cap</span>               <span class="w">Standard</span> <span class="w">perl</span> <span class="w">distribution</span>
	<span class="w">Term::ReadKey</span>           <span class="w">CPAN</span>
	<span class="w">Term::ReadLine::Gnu</span>     <span class="w">CPAN</span>
	<span class="w">Term::ReadLine::Perl</span>    <span class="w">CPAN</span>
	<span class="w">Term::Screen</span>            <span class="w">CPAN</span></pre>
</li>
<li><a name="Screen"></a><b>Screen</b>
<pre class="verbatim">	<span class="w">Term::Cap</span>               <span class="w">Standard</span> <span class="w">perl</span> <span class="w">distribution</span>
	<span class="w">Curses</span>                  <span class="w">CPAN</span>
	<span class="w">Term::ANSIColor</span>         <span class="w">CPAN</span></pre>
</li>
<li><a name="Mouse"></a><b>Mouse</b>
<pre class="verbatim">	<span class="w">Tk</span>                      <span class="w">CPAN</span></pre>
</li>
</ul>
<p>Some of these specific cases are shown as examples in other answers
in this section of the perlfaq.</p>
<a name="How-do-I-print-something-out-in-color%3f"></a><h2>How do I print something out in color?</h2>
<p>In general, you don't, because you don't know whether
the recipient has a color-aware display device.  If you
know that they have an ANSI terminal that understands
color, you can use the Term::ANSIColor module from CPAN:</p>
<pre class="verbatim">	<a class="l_k" href="functions/use.html">use</a> <span class="w">Term::ANSIColor</span><span class="sc">;</span>
	<a class="l_k" href="functions/print.html">print</a> <span class="i">color</span><span class="s">(</span><span class="q">&quot;red&quot;</span><span class="s">)</span><span class="cm">,</span> <span class="q">&quot;Stop!\n&quot;</span><span class="cm">,</span> <span class="i">color</span><span class="s">(</span><span class="q">&quot;reset&quot;</span><span class="s">)</span><span class="sc">;</span>
	<a class="l_k" href="functions/print.html">print</a> <span class="i">color</span><span class="s">(</span><span class="q">&quot;green&quot;</span><span class="s">)</span><span class="cm">,</span> <span class="q">&quot;Go!\n&quot;</span><span class="cm">,</span> <span class="i">color</span><span class="s">(</span><span class="q">&quot;reset&quot;</span><span class="s">)</span><span class="sc">;</span></pre>
<p>Or like this:</p>
<pre class="verbatim">	<a class="l_k" href="functions/use.html">use</a> <span class="w">Term::ANSIColor</span> <span class="q">qw(:constants)</span><span class="sc">;</span>
	<a class="l_k" href="functions/print.html">print</a> <span class="i">RED</span><span class="cm">,</span> <span class="q">&quot;Stop!\n&quot;</span><span class="cm">,</span> <span class="w">RESET</span><span class="sc">;</span>
	<a class="l_k" href="functions/print.html">print</a> <span class="i">GREEN</span><span class="cm">,</span> <span class="q">&quot;Go!\n&quot;</span><span class="cm">,</span> <span class="w">RESET</span><span class="sc">;</span></pre>
<a name="How-do-I-read-just-one-key-without-waiting-for-a-return-key%3f"></a><h2>How do I read just one key without waiting for a return key?</h2>
<p>Controlling input buffering is a remarkably system-dependent matter.
On many systems, you can just use the <b>stty</b> command as shown in
<a href="functions/getc.html">getc</a>, but as you see, that's already getting you into
portability snags.</p>
<pre class="verbatim">	<a class="l_k" href="functions/open.html">open</a><span class="s">(</span><span class="w">TTY</span><span class="cm">,</span> <span class="q">&quot;+&lt;/dev/tty&quot;</span><span class="s">)</span> or <a class="l_k" href="functions/die.html">die</a> <span class="q">&quot;no tty: $!&quot;</span><span class="sc">;</span>
	<a class="l_k" href="functions/system.html">system</a> <span class="q">&quot;stty  cbreak &lt;/dev/tty &gt;/dev/tty 2&gt;&amp;1&quot;</span><span class="sc">;</span>
	<span class="i">$key</span> = <a class="l_k" href="functions/getc.html">getc</a><span class="s">(</span><span class="w">TTY</span><span class="s">)</span><span class="sc">;</span>		<span class="c"># perhaps this works</span>
	<span class="c"># OR ELSE</span>
	<a class="l_k" href="functions/sysread.html">sysread</a><span class="s">(</span><span class="w">TTY</span><span class="cm">,</span> <span class="i">$key</span><span class="cm">,</span> <span class="n">1</span><span class="s">)</span><span class="sc">;</span>	<span class="c"># probably this does</span>
	<a class="l_k" href="functions/system.html">system</a> <span class="q">&quot;stty -cbreak &lt;/dev/tty &gt;/dev/tty 2&gt;&amp;1&quot;</span><span class="sc">;</span></pre>
<p>The Term::ReadKey module from CPAN offers an easy-to-use interface that
should be more efficient than shelling out to <b>stty</b> for each key.
It even includes limited support for Windows.</p>
<pre class="verbatim">	<a class="l_k" href="functions/use.html">use</a> <span class="w">Term::ReadKey</span><span class="sc">;</span>
	<span class="i">ReadMode</span><span class="s">(</span><span class="q">'cbreak'</span><span class="s">)</span><span class="sc">;</span>
	<span class="i">$key</span> = <span class="i">ReadKey</span><span class="s">(</span><span class="n">0</span><span class="s">)</span><span class="sc">;</span>
	<span class="i">ReadMode</span><span class="s">(</span><span class="q">'normal'</span><span class="s">)</span><span class="sc">;</span></pre>
<p>However, using the code requires that you have a working C compiler
and can use it to build and install a CPAN module.  Here's a solution
using the standard POSIX module, which is already on your systems
(assuming your system supports POSIX).</p>
<pre class="verbatim">	<a class="l_k" href="functions/use.html">use</a> <span class="w">HotKey</span><span class="sc">;</span>
	<span class="i">$key</span> = <span class="i">readkey</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></pre>
<p>And here's the HotKey module, which hides the somewhat mystifying calls
to manipulate the POSIX termios structures.</p>
<pre class="verbatim">	<span class="c"># HotKey.pm</span>
<a name="package-HotKey"></a>	package <span class="i">HotKey</span><span class="sc">;</span></pre>
<pre class="verbatim">	<span class="i">@ISA</span> = <span class="q">qw(Exporter)</span><span class="sc">;</span>
	<span class="i">@EXPORT</span> = <span class="q">qw(cbreak cooked readkey)</span><span class="sc">;</span></pre>
<pre class="verbatim">	<a class="l_k" href="functions/use.html">use</a> <span class="w">strict</span><span class="sc">;</span>
	<a class="l_k" href="functions/use.html">use</a> <span class="w">POSIX</span> <span class="q">qw(:termios_h)</span><span class="sc">;</span>
	<a class="l_k" href="functions/my.html">my</a> <span class="s">(</span><span class="i">$term</span><span class="cm">,</span> <span class="i">$oterm</span><span class="cm">,</span> <span class="i">$echo</span><span class="cm">,</span> <span class="i">$noecho</span><span class="cm">,</span> <span class="i">$fd_stdin</span><span class="s">)</span><span class="sc">;</span></pre>
<pre class="verbatim">	<span class="i">$fd_stdin</span> = <a class="l_k" href="functions/fileno.html">fileno</a><span class="s">(</span><span class="w">STDIN</span><span class="s">)</span><span class="sc">;</span>
	<span class="i">$term</span>     = <span class="w">POSIX::Termios</span><span class="w">-&gt;new</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span>
	<span class="i">$term</span><span class="i">-&gt;getattr</span><span class="s">(</span><span class="i">$fd_stdin</span><span class="s">)</span><span class="sc">;</span>
	<span class="i">$oterm</span>     = <span class="i">$term</span><span class="i">-&gt;getlflag</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></pre>
<pre class="verbatim">	<span class="i">$echo</span>     = <span class="w">ECHO</span> | <span class="w">ECHOK</span> | <span class="w">ICANON</span><span class="sc">;</span>
	<span class="i">$noecho</span>   = <span class="i">$oterm</span> &amp; ~<span class="i">$echo</span><span class="sc">;</span></pre>
<pre class="verbatim"><a name="cbreak"></a>	sub <span class="m">cbreak</span> <span class="s">{</span>
		<span class="i">$term</span><span class="i">-&gt;setlflag</span><span class="s">(</span><span class="i">$noecho</span><span class="s">)</span><span class="sc">;</span>  <span class="c"># ok, so i don't want echo either</span>
		<span class="i">$term</span><span class="i">-&gt;setcc</span><span class="s">(</span><span class="w">VTIME</span><span class="cm">,</span> <span class="n">1</span><span class="s">)</span><span class="sc">;</span>
		<span class="i">$term</span><span class="i">-&gt;setattr</span><span class="s">(</span><span class="i">$fd_stdin</span><span class="cm">,</span> <span class="w">TCSANOW</span><span class="s">)</span><span class="sc">;</span>
	<span class="s">}</span></pre>
<pre class="verbatim"><a name="cooked"></a>	sub <span class="m">cooked</span> <span class="s">{</span>
		<span class="i">$term</span><span class="i">-&gt;setlflag</span><span class="s">(</span><span class="i">$oterm</span><span class="s">)</span><span class="sc">;</span>
		<span class="i">$term</span><span class="i">-&gt;setcc</span><span class="s">(</span><span class="w">VTIME</span><span class="cm">,</span> <span class="n">0</span><span class="s">)</span><span class="sc">;</span>
		<span class="i">$term</span><span class="i">-&gt;setattr</span><span class="s">(</span><span class="i">$fd_stdin</span><span class="cm">,</span> <span class="w">TCSANOW</span><span class="s">)</span><span class="sc">;</span>
	<span class="s">}</span></pre>
<pre class="verbatim"><a name="readkey"></a>	sub <span class="m">readkey</span> <span class="s">{</span>
		<a class="l_k" href="functions/my.html">my</a> <span class="i">$key</span> = <span class="q">''</span><span class="sc">;</span>
		<span class="i">cbreak</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span>
		<a class="l_k" href="functions/sysread.html">sysread</a><span class="s">(</span><span class="w">STDIN</span><span class="cm">,</span> <span class="i">$key</span><span class="cm">,</span> <span class="n">1</span><span class="s">)</span><span class="sc">;</span>
		<span class="i">cooked</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span>
		<a class="l_k" href="functions/return.html">return</a> <span class="i">$key</span><span class="sc">;</span>
	<span class="s">}</span></pre>
<pre class="verbatim">	END <span class="s">{</span> <span class="i">cooked</span><span class="s">(</span><span class="s">)</span> <span class="s">}</span></pre>
<pre class="verbatim">	<span class="n">1</span><span class="sc">;</span></pre>
<a name="How-do-I-check-whether-input-is-ready-on-the-keyboard%3f"></a><h2>How do I check whether input is ready on the keyboard?</h2>
<p>The easiest way to do this is to read a key in nonblocking mode with the
Term::ReadKey module from CPAN, passing it an argument of -1 to indicate
not to block:</p>
<pre class="verbatim">	<a class="l_k" href="functions/use.html">use</a> <span class="w">Term::ReadKey</span><span class="sc">;</span></pre>
<pre class="verbatim">	<span class="i">ReadMode</span><span class="s">(</span><span class="q">'cbreak'</span><span class="s">)</span><span class="sc">;</span></pre>
<pre class="verbatim">	if <span class="s">(</span><a class="l_k" href="functions/defined.html">defined</a> <span class="s">(</span><span class="i">$char</span> = <span class="i">ReadKey</span><span class="s">(</span><span class="n">-1</span><span class="s">)</span><span class="s">)</span> <span class="s">)</span> <span class="s">{</span>
		<span class="c"># input was waiting and it was $char</span>
	<span class="s">}</span> else <span class="s">{</span>
		<span class="c"># no input was waiting</span>
	<span class="s">}</span></pre>
<pre class="verbatim">	<span class="i">ReadMode</span><span class="s">(</span><span class="q">'normal'</span><span class="s">)</span><span class="sc">;</span>                  <span class="c"># restore normal tty settings</span></pre>
<a name="How-do-I-clear-the-screen%3f"></a><h2>How do I clear the screen?</h2>
<p>If you only have do so infrequently, use <code class="inline"><a class="l_k" href="functions/system.html">system</a></code>:</p>
<pre class="verbatim">	<a class="l_k" href="functions/system.html">system</a><span class="s">(</span><span class="q">&quot;clear&quot;</span><span class="s">)</span><span class="sc">;</span></pre>
<p>If you have to do this a lot, save the clear string
so you can print it 100 times without calling a program
100 times:</p>
<pre class="verbatim">	<span class="i">$clear_string</span> = <span class="q">`clear`</span><span class="sc">;</span>
	<a class="l_k" href="functions/print.html">print</a> <span class="i">$clear_string</span><span class="sc">;</span></pre>
<p>If you're planning on doing other screen manipulations, like cursor
positions, etc, you might wish to use Term::Cap module:</p>
<pre class="verbatim">	<a class="l_k" href="functions/use.html">use</a> <span class="w">Term::Cap</span><span class="sc">;</span>
	<span class="i">$terminal</span> = <span class="w">Term::Cap</span><span class="w">-&gt;Tgetent</span><span class="s">(</span> <span class="s">{</span><span class="w">OSPEED</span> <span class="cm">=&gt;</span> <span class="n">9600</span><span class="s">}</span> <span class="s">)</span><span class="sc">;</span>
	<span class="i">$clear_string</span> = <span class="i">$terminal</span><span class="i">-&gt;Tputs</span><span class="s">(</span><span class="q">'cl'</span><span class="s">)</span><span class="sc">;</span></pre>
<a name="How-do-I-get-the-screen-size%3f"></a><h2>How do I get the screen size?</h2>
<p>If you have Term::ReadKey module installed from CPAN,
you can use it to fetch the width and height in characters
and in pixels:</p>
<pre class="verbatim">	<a class="l_k" href="functions/use.html">use</a> <span class="w">Term::ReadKey</span><span class="sc">;</span>
	<span class="s">(</span><span class="i">$wchar</span><span class="cm">,</span> <span class="i">$hchar</span><span class="cm">,</span> <span class="i">$wpixels</span><span class="cm">,</span> <span class="i">$hpixels</span><span class="s">)</span> = <span class="i">GetTerminalSize</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></pre>
<p>This is more portable than the raw <code class="inline"><a class="l_k" href="functions/ioctl.html">ioctl</a></code>, but not as
illustrative:</p>
<pre class="verbatim">	<a class="l_k" href="functions/require.html">require</a> <span class="q">'sys/ioctl.ph'</span><span class="sc">;</span>
	<a class="l_k" href="functions/die.html">die</a> <span class="q">&quot;no TIOCGWINSZ &quot;</span> unless <a class="l_k" href="functions/defined.html">defined</a> <span class="i">&amp;TIOCGWINSZ</span><span class="sc">;</span>
	<a class="l_k" href="functions/open.html">open</a><span class="s">(</span><span class="w">TTY</span><span class="cm">,</span> <span class="q">&quot;+&lt;/dev/tty&quot;</span><span class="s">)</span>                     or <a class="l_k" href="functions/die.html">die</a> <span class="q">&quot;No tty: $!&quot;</span><span class="sc">;</span>
	unless <span class="s">(</span><a class="l_k" href="functions/ioctl.html">ioctl</a><span class="s">(</span><span class="w">TTY</span><span class="cm">,</span> <span class="i">&amp;TIOCGWINSZ</span><span class="cm">,</span> <span class="i">$winsize</span>=<span class="q">''</span><span class="s">)</span><span class="s">)</span> <span class="s">{</span>
		<a class="l_k" href="functions/die.html">die</a> <a class="l_k" href="functions/sprintf.html">sprintf</a> <span class="q">&quot;$0: ioctl TIOCGWINSZ (%08x: $!)\n&quot;</span><span class="cm">,</span> <span class="i">&amp;TIOCGWINSZ</span><span class="sc">;</span>
	<span class="s">}</span>
	<span class="s">(</span><span class="i">$row</span><span class="cm">,</span> <span class="i">$col</span><span class="cm">,</span> <span class="i">$xpixel</span><span class="cm">,</span> <span class="i">$ypixel</span><span class="s">)</span> = <a class="l_k" href="functions/unpack.html">unpack</a><span class="s">(</span><span class="q">'S4'</span><span class="cm">,</span> <span class="i">$winsize</span><span class="s">)</span><span class="sc">;</span>
	<a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;(row,col) = ($row,$col)&quot;</span><span class="sc">;</span>
	<a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;  (xpixel,ypixel) = ($xpixel,$ypixel)&quot;</span> if <span class="i">$xpixel</span> || <span class="i">$ypixel</span><span class="sc">;</span>
	<a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;\n&quot;</span><span class="sc">;</span></pre>
<a name="How-do-I-ask-the-user-for-a-password%3f"></a><h2>How do I ask the user for a password?</h2>
<p>(This question has nothing to do with the web.  See a different
FAQ for that.)</p>
<p>There's an example of this in <a href="functions/crypt.html">crypt</a>).  First, you put the
terminal into "no echo" mode, then just read the password normally.
You may do this with an old-style ioctl() function, POSIX terminal
control (see <a href="POSIX.html">POSIX</a> or its documentation the Camel Book), or a call
to the <b>stty</b> program, with varying degrees of portability.</p>
<p>You can also do this for most systems using the Term::ReadKey module
from CPAN, which is easier to use and in theory more portable.</p>
<pre class="verbatim">	<a class="l_k" href="functions/use.html">use</a> <span class="w">Term::ReadKey</span><span class="sc">;</span></pre>
<pre class="verbatim">	<span class="i">ReadMode</span><span class="s">(</span><span class="q">'noecho'</span><span class="s">)</span><span class="sc">;</span>
	<span class="i">$password</span> = <span class="i">ReadLine</span><span class="s">(</span><span class="n">0</span><span class="s">)</span><span class="sc">;</span></pre>
<a name="How-do-I-read-and-write-the-serial-port%3f"></a><h2>How do I read and write the serial port?</h2>
<p>This depends on which operating system your program is running on.  In
the case of Unix, the serial ports will be accessible through files in
/dev; on other systems, device names will doubtless differ.
Several problem areas common to all device interaction are the
following:</p>
<ul>
<li><a name="lockfiles"></a><b>lockfiles</b>
<p>Your system may use lockfiles to control multiple access.  Make sure
you follow the correct protocol.  Unpredictable behavior can result
from multiple processes reading from one device.</p>
</li>
<li><a name="open-mode"></a><b>open mode</b>
<p>If you expect to use both read and write operations on the device,
you'll have to open it for update (see <a href="functions/open.html">open</a> for
details).  You may wish to open it without running the risk of
blocking by using sysopen() and <code class="inline"><span class="w">O_RDWR</span>|<span class="w">O_NDELAY</span>|<span class="w">O_NOCTTY</span></code>
 from the
Fcntl module (part of the standard perl distribution).  See
<a href="functions/sysopen.html">sysopen</a> for more on this approach.</p>
</li>
<li><a name="end-of-line"></a><b>end of line</b>
<p>Some devices will be expecting a "\r" at the end of each line rather
than a "\n".  In some ports of perl, "\r" and "\n" are different from
their usual (Unix) ASCII values of "\012" and "\015".  You may have to
give the numeric values you want directly, using octal ("\015"), hex
("0x0D"), or as a control-character specification ("\cM").</p>
<pre class="verbatim">	<a class="l_k" href="functions/print.html">print</a> <span class="i">DEV</span> <span class="q">&quot;atv1\012&quot;</span><span class="sc">;</span>	<span class="c"># wrong, for some devices</span>
	<a class="l_k" href="functions/print.html">print</a> <span class="i">DEV</span> <span class="q">&quot;atv1\015&quot;</span><span class="sc">;</span>	<span class="c"># right, for some devices</span></pre>
<p>Even though with normal text files a "\n" will do the trick, there is
still no unified scheme for terminating a line that is portable
between Unix, DOS/Win, and Macintosh, except to terminate <i>ALL</i> line
ends with "\015\012", and strip what you don't need from the output.
This applies especially to socket I/O and autoflushing, discussed
next.</p>
</li>
<li><a name="flushing-output"></a><b>flushing output</b>
<p>If you expect characters to get to your device when you print() them,
you'll want to autoflush that filehandle.  You can use select()
and the <code class="inline"><span class="i">$|</span></code>
 variable to control autoflushing (see <i>perlvar/$</i>
and <a href="functions/select.html">select</a>, or <a href="perlfaq5.html">perlfaq5</a>, "How do I flush/unbuffer an
output filehandle?  Why must I do this?"):</p>
<pre class="verbatim">	<span class="i">$oldh</span> = <a class="l_k" href="functions/select.html">select</a><span class="s">(</span><span class="w">DEV</span><span class="s">)</span><span class="sc">;</span>
	<span class="i">$|</span> = <span class="n">1</span><span class="sc">;</span>
	<a class="l_k" href="functions/select.html">select</a><span class="s">(</span><span class="i">$oldh</span><span class="s">)</span><span class="sc">;</span></pre>
<p>You'll also see code that does this without a temporary variable, as in</p>
<pre class="verbatim">	<a class="l_k" href="functions/select.html">select</a><span class="s">(</span><span class="s">(</span><a class="l_k" href="functions/select.html">select</a><span class="s">(</span><span class="w">DEV</span><span class="s">)</span><span class="cm">,</span> <span class="i">$|</span> = <span class="n">1</span><span class="s">)</span>[<span class="n">0</span>]<span class="s">)</span><span class="sc">;</span></pre>
<p>Or if you don't mind pulling in a few thousand lines
of code just because you're afraid of a little $| variable:</p>
<pre class="verbatim">	<a class="l_k" href="functions/use.html">use</a> <span class="w">IO::Handle</span><span class="sc">;</span>
	<span class="w">DEV</span><span class="w">-&gt;autoflush</span><span class="s">(</span><span class="n">1</span><span class="s">)</span><span class="sc">;</span></pre>
<p>As mentioned in the previous item, this still doesn't work when using
socket I/O between Unix and Macintosh.  You'll need to hard code your
line terminators, in that case.</p>
</li>
<li><a name="non-blocking-input"></a><b>non-blocking input</b>
<p>If you are doing a blocking read() or sysread(), you'll have to
arrange for an alarm handler to provide a timeout (see
<a href="functions/alarm.html">alarm</a>).  If you have a non-blocking open, you'll likely
have a non-blocking read, which means you may have to use a 4-arg
select() to determine whether I/O is ready on that device (see
<a href="functions/select.html">select</a>.</p>
</li>
</ul>
<p>While trying to read from his caller-id box, the notorious Jamie Zawinski
<code class="inline"><span class="q">&lt;jwz@netscape.com&gt;</span></code>
, after much gnashing of teeth and fighting with sysread,
sysopen, POSIX's tcgetattr business, and various other functions that
go bump in the night, finally came up with this:</p>
<pre class="verbatim"><a name="open_modem"></a>	sub <span class="m">open_modem</span> <span class="s">{</span>
		<a class="l_k" href="functions/use.html">use</a> <span class="w">IPC::Open2</span><span class="sc">;</span>
		<a class="l_k" href="functions/my.html">my</a> <span class="i">$stty</span> = <span class="q">`/bin/stty -g`</span><span class="sc">;</span>
		<span class="i">open2</span><span class="s">(</span> \<span class="i">*MODEM_IN</span><span class="cm">,</span> \<span class="i">*MODEM_OUT</span><span class="cm">,</span> <span class="q">&quot;cu -l$modem_device -s2400 2&gt;&amp;1&quot;</span><span class="s">)</span><span class="sc">;</span>
		<span class="c"># starting cu hoses /dev/tty's stty settings, even when it has</span>
		<span class="c"># been opened on a pipe...</span>
		<a class="l_k" href="functions/system.html">system</a><span class="s">(</span><span class="q">&quot;/bin/stty $stty&quot;</span><span class="s">)</span><span class="sc">;</span>
		<span class="i">$_</span> = <span class="q">&lt;MODEM_IN&gt;</span><span class="sc">;</span>
		<a class="l_k" href="functions/chomp.html">chomp</a><span class="sc">;</span>
		if <span class="s">(</span> !<span class="q">m/^Connected/</span> <span class="s">)</span> <span class="s">{</span>
			<a class="l_k" href="functions/print.html">print</a> <span class="i">STDERR</span> <span class="q">&quot;$0: cu printed `$_' instead of `Connected'\n&quot;</span><span class="sc">;</span>
		<span class="s">}</span>
	<span class="s">}</span></pre>
<a name="How-do-I-decode-encrypted-password-files%3f"></a><h2>How do I decode encrypted password files?</h2>
<p>You spend lots and lots of money on dedicated hardware, but this is
bound to get you talked about.</p>
<p>Seriously, you can't if they are Unix password files--the Unix
password system employs one-way encryption.  It's more like hashing
than encryption.  The best you can do is check whether something else
hashes to the same string.  You can't turn a hash back into the
original string. Programs like Crack can forcibly (and intelligently)
try to guess passwords, but don't (can't) guarantee quick success.</p>
<p>If you're worried about users selecting bad passwords, you should
proactively check when they try to change their password (by modifying
passwd(1), for example).</p>
<a name="How-do-I-start-a-process-in-the-background%3f"></a><h2>How do I start a process in the background?</h2>
<p>Several modules can start other processes that do not block
your Perl program.  You can use IPC::Open3, Parallel::Jobs,
IPC::Run, and some of the POE modules.  See CPAN for more
details.</p>
<p>You could also use</p>
<pre class="verbatim">	<a class="l_k" href="functions/system.html">system</a><span class="s">(</span><span class="q">&quot;cmd &amp;&quot;</span><span class="s">)</span></pre>
<p>or you could use fork as documented in <a href="functions/fork.html">fork</a>, with
further examples in <a href="perlipc.html">perlipc</a>.  Some things to be aware of, if you're
on a Unix-like system:</p>
<ul>
<li><a name="STDIN%2c-STDOUT%2c-and-STDERR-are-shared"></a><b>STDIN, STDOUT, and STDERR are shared</b>
<p>Both the main process and the backgrounded one (the "child" process)
share the same STDIN, STDOUT and STDERR filehandles.  If both try to
access them at once, strange things can happen.  You may want to close
or reopen these for the child.  You can get around this with
<code class="inline"><a class="l_k" href="functions/open.html">open</a></code>ing a pipe (see <a href="functions/open.html">open</a>) but on some systems this
means that the child process cannot outlive the parent.</p>
</li>
<li><a name="Signals"></a><b>Signals</b>
<p>You'll have to catch the SIGCHLD signal, and possibly SIGPIPE too.
SIGCHLD is sent when the backgrounded process finishes.  SIGPIPE is
sent when you write to a filehandle whose child process has closed (an
untrapped SIGPIPE can cause your program to silently die).  This is
not an issue with <code class="inline"><a class="l_k" href="functions/system.html">system("cmd&")</a></code>.</p>
</li>
<li><a name="Zombies"></a><b>Zombies</b>
<p>You have to be prepared to "reap" the child process when it finishes.</p>
<pre class="verbatim">	<span class="i">$SIG</span>{<span class="w">CHLD</span>} = <a class="l_k" href="functions/sub.html">sub</a> <span class="s">{</span> <a class="l_k" href="functions/wait.html">wait</a> <span class="s">}</span><span class="sc">;</span></pre>
<pre class="verbatim">	<span class="i">$SIG</span>{<span class="w">CHLD</span>} = <span class="q">'IGNORE'</span><span class="sc">;</span></pre>
<p>You can also use a double fork. You immediately wait() for your
first child, and the init daemon will wait() for your grandchild once
it exits.</p>
<pre class="verbatim">	unless <span class="s">(</span><span class="i">$pid</span> = <a class="l_k" href="functions/fork.html">fork</a><span class="s">)</span> <span class="s">{</span>
	    unless <span class="s">(</span><a class="l_k" href="functions/fork.html">fork</a><span class="s">)</span> <span class="s">{</span>
		<a class="l_k" href="functions/exec.html">exec</a> <span class="q">&quot;what you really wanna do&quot;</span><span class="sc">;</span>
		<a class="l_k" href="functions/die.html">die</a> <span class="q">&quot;exec failed!&quot;</span><span class="sc">;</span>
	    <span class="s">}</span>
	    <a class="l_k" href="functions/exit.html">exit</a> <span class="n">0</span><span class="sc">;</span>
	<span class="s">}</span>
	<a class="l_k" href="functions/waitpid.html">waitpid</a><span class="s">(</span><span class="i">$pid</span><span class="cm">,</span> <span class="n">0</span><span class="s">)</span><span class="sc">;</span></pre>
<p>See <a href="perlipc.html#Signals">"Signals" in perlipc</a> for other examples of code to do this.
Zombies are not an issue with <code class="inline"><a class="l_k" href="functions/system.html">system</a><span class="s">(</span><span class="q">&quot;prog &amp;&quot;</span><span class="s">)</span></code>
.</p>
</li>
</ul>
<a name="How-do-I-trap-control-characters%2fsignals%3f"></a><h2>How do I trap control characters/signals?</h2>
<p>You don't actually "trap" a control character.  Instead, that character
generates a signal which is sent to your terminal's currently
foregrounded process group, which you then trap in your process.
Signals are documented in <a href="perlipc.html#Signals">"Signals" in perlipc</a> and the
section on "Signals" in the Camel.</p>
<p>You can set the values of the %SIG hash to be the functions you want
to handle the signal.  After perl catches the signal, it looks in %SIG
for a key with the same name as the signal, then calls the subroutine
value for that key.</p>
<pre class="verbatim">	<span class="c"># as an anonymous subroutine</span></pre>
<pre class="verbatim">	<span class="i">$SIG</span>{<span class="w">INT</span>} = <a class="l_k" href="functions/sub.html">sub</a> <span class="s">{</span> <a class="l_k" href="functions/syswrite.html">syswrite</a><span class="s">(</span><span class="w">STDERR</span><span class="cm">,</span> <span class="q">&quot;ouch\n&quot;</span><span class="cm">,</span> <span class="n">5</span> <span class="s">)</span> <span class="s">}</span><span class="sc">;</span></pre>
<pre class="verbatim">	<span class="c"># or a reference to a function</span></pre>
<pre class="verbatim">	<span class="i">$SIG</span>{<span class="w">INT</span>} = \<span class="i">&amp;ouch</span><span class="sc">;</span></pre>
<pre class="verbatim">	<span class="c"># or the name of the function as a string</span></pre>
<pre class="verbatim">	<span class="i">$SIG</span>{<span class="w">INT</span>} = <span class="q">&quot;ouch&quot;</span><span class="sc">;</span></pre>
<p>Perl versions before 5.8 had in its C source code signal handlers which
would catch the signal and possibly run a Perl function that you had set
in %SIG.  This violated the rules of signal handling at that level
causing perl to dump core. Since version 5.8.0, perl looks at %SIG
*after* the signal has been caught, rather than while it is being caught.
Previous versions of this answer were incorrect.</p>
<a name="How-do-I-modify-the-shadow-password-file-on-a-Unix-system%3f"></a><h2>How do I modify the shadow password file on a Unix system?</h2>
<p>If perl was installed correctly and your shadow library was written
properly, the getpw*() functions described in <a href="perlfunc.html">perlfunc</a> should in
theory provide (read-only) access to entries in the shadow password
file.  To change the file, make a new shadow password file (the format
varies from system to system--see <a href="http://search.cpan.org/perldoc/passwd">passwd</a> for specifics) and use
pwd_mkdb(8) to install it (see <a href="http://search.cpan.org/perldoc/pwd_mkdb">pwd_mkdb</a> for more details).</p>
<a name="How-do-I-set-the-time-and-date%3f"></a><h2>How do I set the time and date?</h2>
<p>Assuming you're running under sufficient permissions, you should be
able to set the system-wide date and time by running the date(1)
program.  (There is no way to set the time and date on a per-process
basis.)  This mechanism will work for Unix, MS-DOS, Windows, and NT;
the VMS equivalent is <code class="inline"><span class="w">set</span> <a class="l_k" href="functions/time.html">time</a></code>
.</p>
<p>However, if all you want to do is change your time zone, you can
probably get away with setting an environment variable:</p>
<pre class="verbatim">	<span class="i">$ENV</span>{<span class="w">TZ</span>} = <span class="q">&quot;MST7MDT&quot;</span><span class="sc">;</span>		   <span class="c"># unixish</span>
	<span class="i">$ENV</span>{<span class="q">'SYS$TIMEZONE_DIFFERENTIAL'</span>}=<span class="q">&quot;-5&quot;</span> <span class="c"># vms</span>
	<a class="l_k" href="functions/system.html">system</a> <span class="q">&quot;trn comp.lang.perl.misc&quot;</span><span class="sc">;</span></pre>
<a name="How-can-I-sleep()-or-alarm()-for-under-a-second%3f"></a><h2>How can I sleep() or alarm() for under a second?
   </h2>
<p>If you want finer granularity than the 1 second that the <code class="inline"><a class="l_k" href="functions/sleep.html">sleep()</a></code>
function provides, the easiest way is to use the <code class="inline"><a class="l_k" href="functions/select.html">select()</a></code> function as
documented in <a href="functions/select.html">select</a>.  Try the <code class="inline"><span class="w">Time::HiRes</span></code>
 and
the <code class="inline"><span class="w">BSD::Itimer</span></code>
 modules (available from CPAN, and starting from
Perl 5.8 <code class="inline"><span class="w">Time::HiRes</span></code>
 is part of the standard distribution).</p>
<a name="How-can-I-measure-time-under-a-second%3f"></a><h2>How can I measure time under a second?
   </h2>
<p>(contributed by brian d foy)</p>
<p>The <code class="inline"><span class="w">Time::HiRes</span></code>
 module (part of the standard distribution as of
Perl 5.8) measures time with the <code class="inline"><span class="i">gettimeofday</span><span class="s">(</span><span class="s">)</span></code>
 system call, which
returns the time in microseconds since the epoch. If you can't install
<code class="inline"><span class="w">Time::HiRes</span></code>
 for older Perls and you are on a Unixish system, you
may be able to call <code class="inline"><span class="i">gettimeofday</span><span class="s">(</span><span class="n">2</span><span class="s">)</span></code>
 directly. See
<a href="functions/syscall.html">syscall</a>.</p>
<a name="How-can-I-do-an-atexit()-or-setjmp()%2flongjmp()%3f-(Exception-handling)"></a><h2>How can I do an atexit() or setjmp()/longjmp()? (Exception handling)</h2>
<p>Release 5 of Perl added the END block, which can be used to simulate
atexit().  Each package's END block is called when the program or
thread ends (see <a href="perlmod.html">perlmod</a> manpage for more details).</p>
<p>For example, you can use this to make sure your filter program
managed to finish its output without filling up the disk:</p>
<pre class="verbatim">	END <span class="s">{</span>
		<a class="l_k" href="functions/close.html">close</a><span class="s">(</span><span class="w">STDOUT</span><span class="s">)</span> || <a class="l_k" href="functions/die.html">die</a> <span class="q">&quot;stdout close failed: $!&quot;</span><span class="sc">;</span>
	<span class="s">}</span></pre>
<p>The END block isn't called when untrapped signals kill the program,
though, so if you use END blocks you should also use</p>
<pre class="verbatim">	<a class="l_k" href="functions/use.html">use</a> <span class="w">sigtrap</span> <span class="q">qw(die normal-signals)</span><span class="sc">;</span></pre>
<p>Perl's exception-handling mechanism is its eval() operator.  You can
use eval() as setjmp and die() as longjmp.  For details of this, see
the section on signals, especially the time-out handler for a blocking
flock() in <a href="perlipc.html#Signals">"Signals" in perlipc</a> or the section on "Signals" in
the Camel Book.</p>
<p>If exception handling is all you're interested in, try the
exceptions.pl library (part of the standard perl distribution).</p>
<p>If you want the atexit() syntax (and an rmexit() as well), try the
AtExit module available from CPAN.</p>
<a name="Why-doesn't-my-sockets-program-work-under-System-V-(Solaris)%3f--What-does-the-error-message-%22Protocol-not-supported%22-mean%3f"></a><h2>Why doesn't my sockets program work under System V (Solaris)?  What does the error message "Protocol not supported" mean?</h2>
<p>Some Sys-V based systems, notably Solaris 2.X, redefined some of the
standard socket constants.  Since these were constant across all
architectures, they were often hardwired into perl code.  The proper
way to deal with this is to "use Socket" to get the correct values.</p>
<p>Note that even though SunOS and Solaris are binary compatible, these
values are different.  Go figure.</p>
<a name="How-can-I-call-my-system's-unique-C-functions-from-Perl%3f"></a><h2>How can I call my system's unique C functions from Perl?</h2>
<p>In most cases, you write an external module to do it--see the answer
to "Where can I learn about linking C with Perl? [h2xs, xsubpp]".
However, if the function is a system call, and your system supports
syscall(), you can use the syscall function (documented in
<a href="perlfunc.html">perlfunc</a>).</p>
<p>Remember to check the modules that came with your distribution, and
CPAN as well--someone may already have written a module to do it. On
Windows, try Win32::API.  On Macs, try Mac::Carbon.  If no module
has an interface to the C function, you can inline a bit of C in your
Perl source with Inline::C.</p>
<a name="Where-do-I-get-the-include-files-to-do-ioctl()-or-syscall()%3f"></a><h2>Where do I get the include files to do ioctl() or syscall()?</h2>
<p>Historically, these would be generated by the h2ph tool, part of the
standard perl distribution.  This program converts cpp(1) directives
in C header files to files containing subroutine definitions, like
&amp;SYS_getitimer, which you can use as arguments to your functions.
It doesn't work perfectly, but it usually gets most of the job done.
Simple files like <i>errno.h</i>, <i>syscall.h</i>, and <i>socket.h</i> were fine,
but the hard ones like <i>ioctl.h</i> nearly always need to hand-edited.
Here's how to install the *.ph files:</p>
<pre class="verbatim">	1.  become super-user
	2.  cd /usr/include
	3.  h2ph *.h */*.h</pre><p>If your system supports dynamic loading, for reasons of portability and
sanity you probably ought to use h2xs (also part of the standard perl
distribution).  This tool converts C header files to Perl extensions.
See <a href="perlxstut.html">perlxstut</a> for how to get started with h2xs.</p>
<p>If your system doesn't support dynamic loading, you still probably
ought to use h2xs.  See <a href="perlxstut.html">perlxstut</a> and <a href="ExtUtils/MakeMaker.html">ExtUtils::MakeMaker</a> for
more information (in brief, just use <b>make perl</b> instead of a plain
<b>make</b> to rebuild perl with a new static extension).</p>
<a name="Why-do-setuid-perl-scripts-complain-about-kernel-problems%3f"></a><h2>Why do setuid perl scripts complain about kernel problems?</h2>
<p>Some operating systems have bugs in the kernel that make setuid
scripts inherently insecure.  Perl gives you a number of options
(described in <a href="perlsec.html">perlsec</a>) to work around such systems.</p>
<a name="How-can-I-open-a-pipe-both-to-and-from-a-command%3f"></a><h2>How can I open a pipe both to and from a command?</h2>
<p>The IPC::Open2 module (part of the standard perl distribution) is an
easy-to-use approach that internally uses pipe(), fork(), and exec() to do
the job.  Make sure you read the deadlock warnings in its documentation,
though (see <a href="IPC/Open2.html">IPC::Open2</a>).  See
<a href="perlipc.html#Bidirectional-Communication-with-Another-Process">"Bidirectional Communication with Another Process" in perlipc</a> and
<a href="perlipc.html#Bidirectional-Communication-with-Yourself">"Bidirectional Communication with Yourself" in perlipc</a></p>
<p>You may also use the IPC::Open3 module (part of the standard perl
distribution), but be warned that it has a different order of
arguments from IPC::Open2 (see <a href="IPC/Open3.html">IPC::Open3</a>).</p>
<a name="Why-can't-I-get-the-output-of-a-command-with-system()%3f"></a><h2>Why can't I get the output of a command with system()?</h2>
<p>You're confusing the purpose of system() and backticks (``).  system()
runs a command and returns exit status information (as a 16 bit value:
the low 7 bits are the signal the process died from, if any, and
the high 8 bits are the actual exit value).  Backticks (``) run a
command and return what it sent to STDOUT.</p>
<pre class="verbatim">	<span class="i">$exit_status</span>   = <a class="l_k" href="functions/system.html">system</a><span class="s">(</span><span class="q">&quot;mail-users&quot;</span><span class="s">)</span><span class="sc">;</span>
	<span class="i">$output_string</span> = <span class="q">`ls`</span><span class="sc">;</span></pre>
<a name="How-can-I-capture-STDERR-from-an-external-command%3f"></a><h2>How can I capture STDERR from an external command?</h2>
<p>There are three basic ways of running external commands:</p>
<pre class="verbatim">	<a class="l_k" href="functions/system.html">system</a> <span class="i">$cmd</span><span class="sc">;</span>		<span class="c"># using system()</span>
	<span class="i">$output</span> = <span class="q">`$cmd`</span><span class="sc">;</span>		<span class="c"># using backticks (``)</span>
	<a class="l_k" href="functions/open.html">open</a> <span class="s">(</span><span class="w">PIPE</span><span class="cm">,</span> <span class="q">&quot;cmd |&quot;</span><span class="s">)</span><span class="sc">;</span>	<span class="c"># using open()</span></pre>
<p>With system(), both STDOUT and STDERR will go the same place as the
script's STDOUT and STDERR, unless the system() command redirects them.
Backticks and open() read <b>only</b> the STDOUT of your command.</p>
<p>You can also use the open3() function from IPC::Open3.  Benjamin
Goldberg provides some sample code:</p>
<p>To capture a program's STDOUT, but discard its STDERR:</p>
<pre class="verbatim">	<a class="l_k" href="functions/use.html">use</a> <span class="w">IPC::Open3</span><span class="sc">;</span>
	<a class="l_k" href="functions/use.html">use</a> <span class="w">File::Spec</span><span class="sc">;</span>
	<a class="l_k" href="functions/use.html">use</a> <span class="w">Symbol</span> <span class="q">qw(gensym)</span><span class="sc">;</span>
	<a class="l_k" href="functions/open.html">open</a><span class="s">(</span><span class="w">NULL</span><span class="cm">,</span> <span class="q">&quot;&gt;&quot;</span><span class="cm">,</span> <span class="w">File::Spec</span><span class="w">-&gt;devnull</span><span class="s">)</span><span class="sc">;</span>
	<a class="l_k" href="functions/my.html">my</a> <span class="i">$pid</span> = <span class="i">open3</span><span class="s">(</span><span class="w">gensym</span><span class="cm">,</span> \<span class="i">*PH</span><span class="cm">,</span> <span class="q">&quot;&gt;&amp;NULL&quot;</span><span class="cm">,</span> <span class="q">&quot;cmd&quot;</span><span class="s">)</span><span class="sc">;</span>
	while<span class="s">(</span> <span class="q">&lt;PH&gt;</span> <span class="s">)</span> <span class="s">{</span> <span class="s">}</span>
	<a class="l_k" href="functions/waitpid.html">waitpid</a><span class="s">(</span><span class="i">$pid</span><span class="cm">,</span> <span class="n">0</span><span class="s">)</span><span class="sc">;</span></pre>
<p>To capture a program's STDERR, but discard its STDOUT:</p>
<pre class="verbatim">	<a class="l_k" href="functions/use.html">use</a> <span class="w">IPC::Open3</span><span class="sc">;</span>
	<a class="l_k" href="functions/use.html">use</a> <span class="w">File::Spec</span><span class="sc">;</span>
	<a class="l_k" href="functions/use.html">use</a> <span class="w">Symbol</span> <span class="q">qw(gensym)</span><span class="sc">;</span>
	<a class="l_k" href="functions/open.html">open</a><span class="s">(</span><span class="w">NULL</span><span class="cm">,</span> <span class="q">&quot;&gt;&quot;</span><span class="cm">,</span> <span class="w">File::Spec</span><span class="w">-&gt;devnull</span><span class="s">)</span><span class="sc">;</span>
	<a class="l_k" href="functions/my.html">my</a> <span class="i">$pid</span> = <span class="i">open3</span><span class="s">(</span><span class="w">gensym</span><span class="cm">,</span> <span class="q">&quot;&gt;&amp;NULL&quot;</span><span class="cm">,</span> \<span class="i">*PH</span><span class="cm">,</span> <span class="q">&quot;cmd&quot;</span><span class="s">)</span><span class="sc">;</span>
	while<span class="s">(</span> <span class="q">&lt;PH&gt;</span> <span class="s">)</span> <span class="s">{</span> <span class="s">}</span>
	<a class="l_k" href="functions/waitpid.html">waitpid</a><span class="s">(</span><span class="i">$pid</span><span class="cm">,</span> <span class="n">0</span><span class="s">)</span><span class="sc">;</span></pre>
<p>To capture a program's STDERR, and let its STDOUT go to our own STDERR:</p>
<pre class="verbatim">	<a class="l_k" href="functions/use.html">use</a> <span class="w">IPC::Open3</span><span class="sc">;</span>
	<a class="l_k" href="functions/use.html">use</a> <span class="w">Symbol</span> <span class="q">qw(gensym)</span><span class="sc">;</span>
	<a class="l_k" href="functions/my.html">my</a> <span class="i">$pid</span> = <span class="i">open3</span><span class="s">(</span><span class="w">gensym</span><span class="cm">,</span> <span class="q">&quot;&gt;&amp;STDERR&quot;</span><span class="cm">,</span> \<span class="i">*PH</span><span class="cm">,</span> <span class="q">&quot;cmd&quot;</span><span class="s">)</span><span class="sc">;</span>
	while<span class="s">(</span> <span class="q">&lt;PH&gt;</span> <span class="s">)</span> <span class="s">{</span> <span class="s">}</span>
	<a class="l_k" href="functions/waitpid.html">waitpid</a><span class="s">(</span><span class="i">$pid</span><span class="cm">,</span> <span class="n">0</span><span class="s">)</span><span class="sc">;</span></pre>
<p>To read both a command's STDOUT and its STDERR separately, you can
redirect them to temp files, let the command run, then read the temp
files:</p>
<pre class="verbatim">	<a class="l_k" href="functions/use.html">use</a> <span class="w">IPC::Open3</span><span class="sc">;</span>
	<a class="l_k" href="functions/use.html">use</a> <span class="w">Symbol</span> <span class="q">qw(gensym)</span><span class="sc">;</span>
	<a class="l_k" href="functions/use.html">use</a> <span class="w">IO::File</span><span class="sc">;</span>
	<a class="l_k" href="functions/local.html">local</a> <span class="i">*CATCHOUT</span> = <span class="w">IO::File</span><span class="w">-&gt;new_tmpfile</span><span class="sc">;</span>
	<a class="l_k" href="functions/local.html">local</a> <span class="i">*CATCHERR</span> = <span class="w">IO::File</span><span class="w">-&gt;new_tmpfile</span><span class="sc">;</span>
	<a class="l_k" href="functions/my.html">my</a> <span class="i">$pid</span> = <span class="i">open3</span><span class="s">(</span><span class="w">gensym</span><span class="cm">,</span> <span class="q">&quot;&gt;&amp;CATCHOUT&quot;</span><span class="cm">,</span> <span class="q">&quot;&gt;&amp;CATCHERR&quot;</span><span class="cm">,</span> <span class="q">&quot;cmd&quot;</span><span class="s">)</span><span class="sc">;</span>
	<a class="l_k" href="functions/waitpid.html">waitpid</a><span class="s">(</span><span class="i">$pid</span><span class="cm">,</span> <span class="n">0</span><span class="s">)</span><span class="sc">;</span>
	<a class="l_k" href="functions/seek.html">seek</a> <span class="i">$_</span><span class="cm">,</span> <span class="n">0</span><span class="cm">,</span> <span class="n">0</span> for \<span class="i">*CATCHOUT</span><span class="cm">,</span> \<span class="i">*CATCHERR</span><span class="sc">;</span>
	while<span class="s">(</span> <span class="q">&lt;CATCHOUT&gt;</span> <span class="s">)</span> <span class="s">{</span><span class="s">}</span>
	while<span class="s">(</span> <span class="q">&lt;CATCHERR&gt;</span> <span class="s">)</span> <span class="s">{</span><span class="s">}</span></pre>
<p>But there's no real need for *both* to be tempfiles... the following
should work just as well, without deadlocking:</p>
<pre class="verbatim">	<a class="l_k" href="functions/use.html">use</a> <span class="w">IPC::Open3</span><span class="sc">;</span>
	<a class="l_k" href="functions/use.html">use</a> <span class="w">Symbol</span> <span class="q">qw(gensym)</span><span class="sc">;</span>
	<a class="l_k" href="functions/use.html">use</a> <span class="w">IO::File</span><span class="sc">;</span>
	<a class="l_k" href="functions/local.html">local</a> <span class="i">*CATCHERR</span> = <span class="w">IO::File</span><span class="w">-&gt;new_tmpfile</span><span class="sc">;</span>
	<a class="l_k" href="functions/my.html">my</a> <span class="i">$pid</span> = <span class="i">open3</span><span class="s">(</span><span class="w">gensym</span><span class="cm">,</span> \<span class="i">*CATCHOUT</span><span class="cm">,</span> <span class="q">&quot;&gt;&amp;CATCHERR&quot;</span><span class="cm">,</span> <span class="q">&quot;cmd&quot;</span><span class="s">)</span><span class="sc">;</span>
	while<span class="s">(</span> <span class="q">&lt;CATCHOUT&gt;</span> <span class="s">)</span> <span class="s">{</span><span class="s">}</span>
	<a class="l_k" href="functions/waitpid.html">waitpid</a><span class="s">(</span><span class="i">$pid</span><span class="cm">,</span> <span class="n">0</span><span class="s">)</span><span class="sc">;</span>
	<a class="l_k" href="functions/seek.html">seek</a> <span class="w">CATCHERR</span><span class="cm">,</span> <span class="n">0</span><span class="cm">,</span> <span class="n">0</span><span class="sc">;</span>
	while<span class="s">(</span> <span class="q">&lt;CATCHERR&gt;</span> <span class="s">)</span> <span class="s">{</span><span class="s">}</span></pre>
<p>And it'll be faster, too, since we can begin processing the program's
stdout immediately, rather than waiting for the program to finish.</p>
<p>With any of these, you can change file descriptors before the call:</p>
<pre class="verbatim">	<a class="l_k" href="functions/open.html">open</a><span class="s">(</span><span class="w">STDOUT</span><span class="cm">,</span> <span class="q">&quot;&gt;logfile&quot;</span><span class="s">)</span><span class="sc">;</span>
	<a class="l_k" href="functions/system.html">system</a><span class="s">(</span><span class="q">&quot;ls&quot;</span><span class="s">)</span><span class="sc">;</span></pre>
<p>or you can use Bourne shell file-descriptor redirection:</p>
<pre class="verbatim">	<span class="i">$output</span> = <span class="q">`$cmd 2&gt;some_file`</span><span class="sc">;</span>
	<a class="l_k" href="functions/open.html">open</a> <span class="s">(</span><span class="w">PIPE</span><span class="cm">,</span> <span class="q">&quot;cmd 2&gt;some_file |&quot;</span><span class="s">)</span><span class="sc">;</span></pre>
<p>You can also use file-descriptor redirection to make STDERR a
duplicate of STDOUT:</p>
<pre class="verbatim">	<span class="i">$output</span> = <span class="q">`$cmd 2&gt;&amp;1`</span><span class="sc">;</span>
	<a class="l_k" href="functions/open.html">open</a> <span class="s">(</span><span class="w">PIPE</span><span class="cm">,</span> <span class="q">&quot;cmd 2&gt;&amp;1 |&quot;</span><span class="s">)</span><span class="sc">;</span></pre>
<p>Note that you <i>cannot</i> simply open STDERR to be a dup of STDOUT
in your Perl program and avoid calling the shell to do the redirection.
This doesn't work:</p>
<pre class="verbatim">	<a class="l_k" href="functions/open.html">open</a><span class="s">(</span><span class="w">STDERR</span><span class="cm">,</span> <span class="q">&quot;&gt;&amp;STDOUT&quot;</span><span class="s">)</span><span class="sc">;</span>
	<span class="i">$alloutput</span> = <span class="q">`cmd args`</span><span class="sc">;</span>  <span class="c"># stderr still escapes</span></pre>
<p>This fails because the open() makes STDERR go to where STDOUT was
going at the time of the open().  The backticks then make STDOUT go to
a string, but don't change STDERR (which still goes to the old
STDOUT).</p>
<p>Note that you <i>must</i> use Bourne shell (sh(1)) redirection syntax in
backticks, not csh(1)!  Details on why Perl's system() and backtick
and pipe opens all use the Bourne shell are in the
<i>versus/csh.whynot</i> article in the "Far More Than You Ever Wanted To
Know" collection in <a href="http://www.cpan.org/misc/olddoc/FMTEYEWTK.tgz">http://www.cpan.org/misc/olddoc/FMTEYEWTK.tgz</a> .  To
capture a command's STDERR and STDOUT together:</p>
<pre class="verbatim">	<span class="i">$output</span> = <span class="q">`cmd 2&gt;&amp;1`</span><span class="sc">;</span>                       <span class="c"># either with backticks</span>
	<span class="i">$pid</span> = <a class="l_k" href="functions/open.html">open</a><span class="s">(</span><span class="w">PH</span><span class="cm">,</span> <span class="q">&quot;cmd 2&gt;&amp;1 |&quot;</span><span class="s">)</span><span class="sc">;</span>              <span class="c"># or with an open pipe</span>
	while <span class="s">(</span><span class="q">&lt;PH&gt;</span><span class="s">)</span> <span class="s">{</span> <span class="s">}</span>                            <span class="c">#    plus a read</span></pre>
<p>To capture a command's STDOUT but discard its STDERR:</p>
<pre class="verbatim">	<span class="i">$output</span> = <span class="q">`cmd 2&gt;/dev/null`</span><span class="sc">;</span>                <span class="c"># either with backticks</span>
	<span class="i">$pid</span> = <a class="l_k" href="functions/open.html">open</a><span class="s">(</span><span class="w">PH</span><span class="cm">,</span> <span class="q">&quot;cmd 2&gt;/dev/null |&quot;</span><span class="s">)</span><span class="sc">;</span>       <span class="c"># or with an open pipe</span>
	while <span class="s">(</span><span class="q">&lt;PH&gt;</span><span class="s">)</span> <span class="s">{</span> <span class="s">}</span>                            <span class="c">#    plus a read</span></pre>
<p>To capture a command's STDERR but discard its STDOUT:</p>
<pre class="verbatim">	<span class="i">$output</span> = <span class="q">`cmd 2&gt;&amp;1 1&gt;/dev/null`</span><span class="sc">;</span>           <span class="c"># either with backticks</span>
	<span class="i">$pid</span> = <a class="l_k" href="functions/open.html">open</a><span class="s">(</span><span class="w">PH</span><span class="cm">,</span> <span class="q">&quot;cmd 2&gt;&amp;1 1&gt;/dev/null |&quot;</span><span class="s">)</span><span class="sc">;</span>  <span class="c"># or with an open pipe</span>
	while <span class="s">(</span><span class="q">&lt;PH&gt;</span><span class="s">)</span> <span class="s">{</span> <span class="s">}</span>                            <span class="c">#    plus a read</span></pre>
<p>To exchange a command's STDOUT and STDERR in order to capture the STDERR
but leave its STDOUT to come out our old STDERR:</p>
<pre class="verbatim">	<span class="i">$output</span> = <span class="q">`cmd 3&gt;&amp;1 1&gt;&amp;2 2&gt;&amp;3 3&gt;&amp;-`</span><span class="sc">;</span>        <span class="c"># either with backticks</span>
	<span class="i">$pid</span> = <a class="l_k" href="functions/open.html">open</a><span class="s">(</span><span class="w">PH</span><span class="cm">,</span> <span class="q">&quot;cmd 3&gt;&amp;1 1&gt;&amp;2 2&gt;&amp;3 3&gt;&amp;-|&quot;</span><span class="s">)</span><span class="sc">;</span><span class="c"># or with an open pipe</span>
	while <span class="s">(</span><span class="q">&lt;PH&gt;</span><span class="s">)</span> <span class="s">{</span> <span class="s">}</span>                            <span class="c">#    plus a read</span></pre>
<p>To read both a command's STDOUT and its STDERR separately, it's easiest
to redirect them separately to files, and then read from those files
when the program is done:</p>
<pre class="verbatim">	<a class="l_k" href="functions/system.html">system</a><span class="s">(</span><span class="q">&quot;program args 1&gt;program.stdout 2&gt;program.stderr&quot;</span><span class="s">)</span><span class="sc">;</span></pre>
<p>Ordering is important in all these examples.  That's because the shell
processes file descriptor redirections in strictly left to right order.</p>
<pre class="verbatim">	<a class="l_k" href="functions/system.html">system</a><span class="s">(</span><span class="q">&quot;prog args 1&gt;tmpfile 2&gt;&amp;1&quot;</span><span class="s">)</span><span class="sc">;</span>
	<a class="l_k" href="functions/system.html">system</a><span class="s">(</span><span class="q">&quot;prog args 2&gt;&amp;1 1&gt;tmpfile&quot;</span><span class="s">)</span><span class="sc">;</span></pre>
<p>The first command sends both standard out and standard error to the
temporary file.  The second command sends only the old standard output
there, and the old standard error shows up on the old standard out.</p>
<a name="Why-doesn't-open()-return-an-error-when-a-pipe-open-fails%3f"></a><h2>Why doesn't open() return an error when a pipe open fails?</h2>
<p>If the second argument to a piped open() contains shell
metacharacters, perl fork()s, then exec()s a shell to decode the
metacharacters and eventually run the desired program.  If the program
couldn't be run, it's the shell that gets the message, not Perl. All
your Perl program can find out is whether the shell itself could be
successfully started.  You can still capture the shell's STDERR and
check it for error messages.  See <a href="#How-can-I-capture-STDERR-from-an-external-command%3f">"How can I capture STDERR from an external command?"</a> elsewhere in this document, or use the
IPC::Open3 module.</p>
<p>If there are no shell metacharacters in the argument of open(), Perl
runs the command directly, without using the shell, and can correctly
report whether the command started.</p>
<a name="What's-wrong-with-using-backticks-in-a-void-context%3f"></a><h2>What's wrong with using backticks in a void context?</h2>
<p>Strictly speaking, nothing.  Stylistically speaking, it's not a good
way to write maintainable code.  Perl has several operators for
running external commands.  Backticks are one; they collect the output
from the command for use in your program.  The <code class="inline"><a class="l_k" href="functions/system.html">system</a></code> function is
another; it doesn't do this.</p>
<p>Writing backticks in your program sends a clear message to the readers
of your code that you wanted to collect the output of the command.
Why send a clear message that isn't true?</p>
<p>Consider this line:</p>
<pre class="verbatim">	<span class="q">`cat /etc/termcap`</span><span class="sc">;</span></pre>
<p>You forgot to check <code class="inline"><span class="i">$?</span></code>
 to see whether the program even ran
correctly.  Even if you wrote</p>
<pre class="verbatim">	<a class="l_k" href="functions/print.html">print</a> <span class="q">`cat /etc/termcap`</span><span class="sc">;</span></pre>
<p>this code could and probably should be written as</p>
<pre class="verbatim">	<a class="l_k" href="functions/system.html">system</a><span class="s">(</span><span class="q">&quot;cat /etc/termcap&quot;</span><span class="s">)</span> == <span class="n">0</span>
	or <a class="l_k" href="functions/die.html">die</a> <span class="q">&quot;cat program failed!&quot;</span><span class="sc">;</span></pre>
<p>which will echo the cat command's output as it is generated, instead
of waiting until the program has completed to print it out. It also
checks the return value.</p>
<p><code class="inline"><a class="l_k" href="functions/system.html">system</a></code> also provides direct control over whether shell wildcard
processing may take place, whereas backticks do not.</p>
<a name="How-can-I-call-backticks-without-shell-processing%3f"></a><h2>How can I call backticks without shell processing?</h2>
<p>This is a bit tricky.  You can't simply write the command
like this:</p>
<pre class="verbatim">	<span class="i">@ok</span> = <span class="q">`grep @opts '$search_string' @filenames`</span><span class="sc">;</span></pre>
<p>As of Perl 5.8.0, you can use <code class="inline"><a class="l_k" href="functions/open.html">open()</a></code> with multiple arguments.
Just like the list forms of <code class="inline"><a class="l_k" href="functions/system.html">system()</a></code> and <code class="inline"><a class="l_k" href="functions/exec.html">exec()</a></code>, no shell
escapes happen.</p>
<pre class="verbatim">	<a class="l_k" href="functions/open.html">open</a><span class="s">(</span> <span class="w">GREP</span><span class="cm">,</span> <span class="q">&quot;-|&quot;</span><span class="cm">,</span> <span class="q">'grep'</span><span class="cm">,</span> <span class="i">@opts</span><span class="cm">,</span> <span class="i">$search_string</span><span class="cm">,</span> <span class="i">@filenames</span> <span class="s">)</span><span class="sc">;</span>
	<a class="l_k" href="functions/chomp.html">chomp</a><span class="s">(</span><span class="i">@ok</span> = <span class="q">&lt;GREP&gt;</span><span class="s">)</span><span class="sc">;</span>
	<a class="l_k" href="functions/close.html">close</a> <span class="w">GREP</span><span class="sc">;</span></pre>
<p>You can also:</p>
<pre class="verbatim">	<a class="l_k" href="functions/my.html">my</a> <span class="i">@ok</span> = <span class="s">(</span><span class="s">)</span><span class="sc">;</span>
	if <span class="s">(</span><a class="l_k" href="functions/open.html">open</a><span class="s">(</span><span class="w">GREP</span><span class="cm">,</span> <span class="q">&quot;-|&quot;</span><span class="s">)</span><span class="s">)</span> <span class="s">{</span>
		while <span class="s">(</span><span class="q">&lt;GREP&gt;</span><span class="s">)</span> <span class="s">{</span>
			<a class="l_k" href="functions/chomp.html">chomp</a><span class="sc">;</span>
			<a class="l_k" href="functions/push.html">push</a><span class="s">(</span><span class="i">@ok</span><span class="cm">,</span> <span class="i">$_</span><span class="s">)</span><span class="sc">;</span>
		<span class="s">}</span>
		<a class="l_k" href="functions/close.html">close</a> <span class="w">GREP</span><span class="sc">;</span>
	<span class="s">}</span> else <span class="s">{</span>
		<a class="l_k" href="functions/exec.html">exec</a> <span class="q">'grep'</span><span class="cm">,</span> <span class="i">@opts</span><span class="cm">,</span> <span class="i">$search_string</span><span class="cm">,</span> <span class="i">@filenames</span><span class="sc">;</span>
	<span class="s">}</span></pre>
<p>Just as with <code class="inline"><a class="l_k" href="functions/system.html">system()</a></code>, no shell escapes happen when you <code class="inline"><a class="l_k" href="functions/exec.html">exec()</a></code> a
list. Further examples of this can be found in <a href="perlipc.html#Safe-Pipe-Opens">"Safe Pipe Opens" in perlipc</a>.</p>
<p>Note that if you're using Windows, no solution to this vexing issue is
even possible.  Even if Perl were to emulate <code class="inline"><a class="l_k" href="functions/fork.html">fork()</a></code>, you'd still be
stuck, because Windows does not have an argc/argv-style API.</p>
<a name="Why-can't-my-script-read-from-STDIN-after-I-gave-it-EOF-(%5eD-on-Unix%2c-%5eZ-on-MS-DOS)%3f"></a><h2>Why can't my script read from STDIN after I gave it EOF (^D on Unix, ^Z on MS-DOS)?</h2>
<p>Some stdio's set error and eof flags that need clearing.  The
POSIX module defines clearerr() that you can use.  That is the
technically correct way to do it.  Here are some less reliable
workarounds:</p>
<ol>
<li>
<p>Try keeping around the seekpointer and go there, like this:</p>
<pre class="verbatim">	<span class="i">$where</span> = <a class="l_k" href="functions/tell.html">tell</a><span class="s">(</span><span class="w">LOG</span><span class="s">)</span><span class="sc">;</span>
	<a class="l_k" href="functions/seek.html">seek</a><span class="s">(</span><span class="w">LOG</span><span class="cm">,</span> <span class="i">$where</span><span class="cm">,</span> <span class="n">0</span><span class="s">)</span><span class="sc">;</span></pre>
</li>
<li>
<p>If that doesn't work, try seeking to a different part of the file and
then back.</p>
</li>
<li>
<p>If that doesn't work, try seeking to a different part of
the file, reading something, and then seeking back.</p>
</li>
<li>
<p>If that doesn't work, give up on your stdio package and use sysread.</p>
</li>
</ol>
<a name="How-can-I-convert-my-shell-script-to-perl%3f"></a><h2>How can I convert my shell script to perl?</h2>
<p>Learn Perl and rewrite it.  Seriously, there's no simple converter.
Things that are awkward to do in the shell are easy to do in Perl, and
this very awkwardness is what would make a shell-&gt;perl converter
nigh-on impossible to write.  By rewriting it, you'll think about what
you're really trying to do, and hopefully will escape the shell's
pipeline datastream paradigm, which while convenient for some matters,
causes many inefficiencies.</p>
<a name="Can-I-use-perl-to-run-a-telnet-or-ftp-session%3f"></a><h2>Can I use perl to run a telnet or ftp session?</h2>
<p>Try the Net::FTP, TCP::Client, and Net::Telnet modules (available from
CPAN).  <a href="http://www.cpan.org/scripts/netstuff/telnet.emul.shar">http://www.cpan.org/scripts/netstuff/telnet.emul.shar</a>
will also help for emulating the telnet protocol, but Net::Telnet is
quite probably easier to use..</p>
<p>If all you want to do is pretend to be telnet but don't need
the initial telnet handshaking, then the standard dual-process
approach will suffice:</p>
<pre class="verbatim">	<a class="l_k" href="functions/use.html">use</a> <span class="w">IO::Socket</span><span class="sc">;</span>             <span class="c"># new in 5.004</span>
	<span class="i">$handle</span> = <span class="w">IO::Socket::INET</span><span class="w">-&gt;new</span><span class="s">(</span><span class="q">'www.perl.com:80'</span><span class="s">)</span>
	    or <a class="l_k" href="functions/die.html">die</a> <span class="q">&quot;can't connect to port 80 on www.perl.com: $!&quot;</span><span class="sc">;</span>
	<span class="i">$handle</span><span class="i">-&gt;autoflush</span><span class="s">(</span><span class="n">1</span><span class="s">)</span><span class="sc">;</span>
	if <span class="s">(</span><a class="l_k" href="functions/fork.html">fork</a><span class="s">(</span><span class="s">)</span><span class="s">)</span> <span class="s">{</span>               <span class="c"># XXX: undef means failure</span>
	    <a class="l_k" href="functions/select.html">select</a><span class="s">(</span><span class="i">$handle</span><span class="s">)</span><span class="sc">;</span>
	    <a class="l_k" href="functions/print.html">print</a> while <span class="q">&lt;STDIN&gt;</span><span class="sc">;</span>    <span class="c"># everything from stdin to socket</span>
	<span class="s">}</span> else <span class="s">{</span>
	    <a class="l_k" href="functions/print.html">print</a> while <span class="q">&lt;$handle&gt;</span><span class="sc">;</span>  <span class="c"># everything from socket to stdout</span>
	<span class="s">}</span>
	<a class="l_k" href="functions/close.html">close</a> <span class="i">$handle</span><span class="sc">;</span>
	<a class="l_k" href="functions/exit.html">exit</a><span class="sc">;</span></pre>
<a name="How-can-I-write-expect-in-Perl%3f"></a><h2>How can I write expect in Perl?</h2>
<p>Once upon a time, there was a library called chat2.pl (part of the
standard perl distribution), which never really got finished.  If you
find it somewhere, <i>don't use it</i>.  These days, your best bet is to
look at the Expect module available from CPAN, which also requires two
other modules from CPAN, IO::Pty and IO::Stty.</p>
<a name="Is-there-a-way-to-hide-perl's-command-line-from-programs-such-as-%22ps%22%3f"></a><h2>Is there a way to hide perl's command line from programs such as "ps"?</h2>
<p>First of all note that if you're doing this for security reasons (to
avoid people seeing passwords, for example) then you should rewrite
your program so that critical information is never given as an
argument.  Hiding the arguments won't make your program completely
secure.</p>
<p>To actually alter the visible command line, you can assign to the
variable $0 as documented in <a href="perlvar.html">perlvar</a>.  This won't work on all
operating systems, though.  Daemon programs like sendmail place their
state there, as in:</p>
<pre class="verbatim">	<span class="i">$0</span> = <span class="q">&quot;orcus [accepting connections]&quot;</span><span class="sc">;</span></pre>
<a name="I-%7bchanged-directory%2c-modified-my-environment%7d-in-a-perl-script.--How-come-the-change-disappeared-when-I-exited-the-script%3f--How-do-I-get-my-changes-to-be-visible%3f"></a><h2>I {changed directory, modified my environment} in a perl script.  How come the change disappeared when I exited the script?  How do I get my changes to be visible?</h2>
<ul>
<li><a name="Unix"></a><b>Unix</b>
<p>In the strictest sense, it can't be done--the script executes as a
different process from the shell it was started from.  Changes to a
process are not reflected in its parent--only in any children
created after the change.  There is shell magic that may allow you to
fake it by eval()ing the script's output in your shell; check out the
comp.unix.questions FAQ for details.</p>
</li>
</ul>
<a name="How-do-I-close-a-process's-filehandle-without-waiting-for-it-to-complete%3f"></a><h2>How do I close a process's filehandle without waiting for it to complete?</h2>
<p>Assuming your system supports such things, just send an appropriate signal
to the process (see <a href="functions/kill.html">kill</a>).  It's common to first send a TERM
signal, wait a little bit, and then send a KILL signal to finish it off.</p>
<a name="How-do-I-fork-a-daemon-process%3f"></a><h2>How do I fork a daemon process?</h2>
<p>If by daemon process you mean one that's detached (disassociated from
its tty), then the following process is reported to work on most
Unixish systems.  Non-Unix users should check their Your_OS::Process
module for other solutions.</p>
<ul>
<li>
<p>Open /dev/tty and use the TIOCNOTTY ioctl on it.  See <a href="http://search.cpan.org/perldoc/tty">tty</a>
for details.  Or better yet, you can just use the POSIX::setsid()
function, so you don't have to worry about process groups.</p>
</li>
<li>
<p>Change directory to /</p>
</li>
<li>
<p>Reopen STDIN, STDOUT, and STDERR so they're not connected to the old
tty.</p>
</li>
<li>
<p>Background yourself like this:</p>
<pre class="verbatim">	<a class="l_k" href="functions/fork.html">fork</a> &amp;&amp; <a class="l_k" href="functions/exit.html">exit</a><span class="sc">;</span></pre>
</li>
</ul>
<p>The Proc::Daemon module, available from CPAN, provides a function to
perform these actions for you.</p>
<a name="How-do-I-find-out-if-I'm-running-interactively-or-not%3f"></a><h2>How do I find out if I'm running interactively or not?</h2>
<p>Good question. Sometimes <code class="inline">-t <span class="w">STDIN</span></code>
 and <code class="inline">-t <span class="w">STDOUT</span></code>
 can give clues,
sometimes not.</p>
<pre class="verbatim">	if <span class="s">(</span>-t <span class="w">STDIN</span> &amp;&amp; -t <span class="w">STDOUT</span><span class="s">)</span> <span class="s">{</span>
		<a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;Now what? &quot;</span><span class="sc">;</span>
		<span class="s">}</span></pre>
<p>On POSIX systems, you can test whether your own process group matches
the current process group of your controlling terminal as follows:</p>
<pre class="verbatim">	<a class="l_k" href="functions/use.html">use</a> <span class="w">POSIX</span> <span class="q">qw/getpgrp tcgetpgrp/</span><span class="sc">;</span></pre>
<pre class="verbatim">	<span class="c"># Some POSIX systems, such as Linux, can be</span>
	<span class="c"># without a /dev/tty at boot time.</span>
	if <span class="s">(</span>!<a class="l_k" href="functions/open.html">open</a><span class="s">(</span><span class="w">TTY</span><span class="cm">,</span> <span class="q">&quot;/dev/tty&quot;</span><span class="s">)</span><span class="s">)</span> <span class="s">{</span>
		<a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;no tty\n&quot;</span><span class="sc">;</span>
	<span class="s">}</span> else <span class="s">{</span>
		<span class="i">$tpgrp</span> = <span class="i">tcgetpgrp</span><span class="s">(</span><a class="l_k" href="functions/fileno.html">fileno</a><span class="s">(</span><span class="i">*TTY</span><span class="s">)</span><span class="s">)</span><span class="sc">;</span>
		<span class="i">$pgrp</span> = <a class="l_k" href="functions/getpgrp.html">getpgrp</a><span class="s">(</span><span class="s">)</span><span class="sc">;</span>
		if <span class="s">(</span><span class="i">$tpgrp</span> == <span class="i">$pgrp</span><span class="s">)</span> <span class="s">{</span>
			<a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;foreground\n&quot;</span><span class="sc">;</span>
		<span class="s">}</span> else <span class="s">{</span>
			<a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;background\n&quot;</span><span class="sc">;</span>
		<span class="s">}</span>
	<span class="s">}</span></pre>
<a name="How-do-I-timeout-a-slow-event%3f"></a><h2>How do I timeout a slow event?</h2>
<p>Use the alarm() function, probably in conjunction with a signal
handler, as documented in <a href="perlipc.html#Signals">"Signals" in perlipc</a> and the section on
"Signals" in the Camel.  You may instead use the more flexible
Sys::AlarmCall module available from CPAN.</p>
<p>The alarm() function is not implemented on all versions of Windows.
Check the documentation for your specific version of Perl.</p>
<a name="How-do-I-set-CPU-limits%3f"></a><h2>How do I set CPU limits?
  </h2>
<p>(contributed by Xho)</p>
<p>Use the <code class="inline"><span class="w">BSD::Resource</span></code>
 module from CPAN. As an example:</p>
<pre class="verbatim">	<a class="l_k" href="functions/use.html">use</a> <span class="w">BSD::Resource</span><span class="sc">;</span>
	<span class="i">setrlimit</span><span class="s">(</span><span class="w">RLIMIT_CPU</span><span class="cm">,</span><span class="n">10</span><span class="cm">,</span><span class="n">20</span><span class="s">)</span> or <a class="l_k" href="functions/die.html">die</a> <span class="i">$!</span><span class="sc">;</span></pre>
<p>This sets the soft and hard limits to 10 and 20 seconds, respectively.
After 10 seconds of time spent running on the CPU (not "wall" time),
the process will be sent a signal (XCPU on some systems) which, if not
trapped, will cause the process to terminate.  If that signal is
trapped, then after 10 more seconds (20 seconds in total) the process
will be killed with a non-trappable signal.</p>
<p>See the <code class="inline"><span class="w">BSD::Resource</span></code>
 and your systems documentation for the gory
details.</p>
<a name="How-do-I-avoid-zombies-on-a-Unix-system%3f"></a><h2>How do I avoid zombies on a Unix system?</h2>
<p>Use the reaper code from <a href="perlipc.html#Signals">"Signals" in perlipc</a> to call wait() when a
SIGCHLD is received, or else use the double-fork technique described
in <a href="perlfaq8.html#How-do-I-start-a-process-in-the-background%3f">"How do I start a process in the background?" in perlfaq8</a>.</p>
<a name="How-do-I-use-an-SQL-database%3f"></a><h2>How do I use an SQL database?</h2>
<p>The DBI module provides an abstract interface to most database
servers and types, including Oracle, DB2, Sybase, mysql, Postgresql,
ODBC, and flat files.  The DBI module accesses each database type
through a database driver, or DBD.  You can see a complete list of
available drivers on CPAN: <a href="http://www.cpan.org/modules/by-module/DBD/">http://www.cpan.org/modules/by-module/DBD/</a> .
You can read more about DBI on <a href="http://dbi.perl.org">http://dbi.perl.org</a> .</p>
<p>Other modules provide more specific access: Win32::ODBC, Alzabo, iodbc,
and others found on CPAN Search: <a href="http://search.cpan.org">http://search.cpan.org</a> .</p>
<a name="How-do-I-make-a-system()-exit-on-control-C%3f"></a><h2>How do I make a system() exit on control-C?</h2>
<p>You can't.  You need to imitate the system() call (see <a href="perlipc.html">perlipc</a> for
sample code) and then have a signal handler for the INT signal that
passes the signal on to the subprocess.  Or you can check for it:</p>
<pre class="verbatim">	<span class="i">$rc</span> = <a class="l_k" href="functions/system.html">system</a><span class="s">(</span><span class="i">$cmd</span><span class="s">)</span><span class="sc">;</span>
	if <span class="s">(</span><span class="i">$rc</span> &amp; <span class="n">127</span><span class="s">)</span> <span class="s">{</span> <a class="l_k" href="functions/die.html">die</a> <span class="q">&quot;signal death&quot;</span> <span class="s">}</span></pre>
<a name="How-do-I-open-a-file-without-blocking%3f"></a><h2>How do I open a file without blocking?</h2>
<p>If you're lucky enough to be using a system that supports
non-blocking reads (most Unixish systems do), you need only to use the
O_NDELAY or O_NONBLOCK flag from the Fcntl module in conjunction with
sysopen():</p>
<pre class="verbatim">	use Fcntl;
	sysopen(FH, "/foo/somefile", O_WRONLY|O_NDELAY|O_CREAT, 0644)
		or die "can't open /foo/somefile: $!":</pre><a name="How-do-I-tell-the-difference-between-errors-from-the-shell-and-perl%3f"></a><h2>How do I tell the difference between errors from the shell and perl?</h2>
<p>(answer contributed by brian d foy)</p>
<p>When you run a Perl script, something else is running the script for you,
and that something else may output error messages.  The script might
emit its own warnings and error messages.  Most of the time you cannot
tell who said what.</p>
<p>You probably cannot fix the thing that runs perl, but you can change how
perl outputs its warnings by defining a custom warning and die functions.</p>
<p>Consider this script, which has an error you may not notice immediately.</p>
<pre class="verbatim">	<span class="c">#!/usr/locl/bin/perl</span></pre>
<pre class="verbatim">	<a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;Hello World\n&quot;</span><span class="sc">;</span></pre>
<p>I get an error when I run this from my shell (which happens to be
bash).  That may look like perl forgot it has a print() function,
but my shebang line is not the path to perl, so the shell runs the
script, and I get the error.</p>
<pre class="verbatim">	$ ./test
	./test: line 3: print: command not found</pre><p>A quick and dirty fix involves a little bit of code, but this may be all
you need to figure out the problem.</p>
<pre class="verbatim">	<span class="c">#!/usr/bin/perl -w</span></pre>
<pre class="verbatim">	BEGIN <span class="s">{</span>
	<span class="i">$SIG</span>{<span class="w">__WARN__</span>} = <a class="l_k" href="functions/sub.html">sub</a><span class="s">{</span> <a class="l_k" href="functions/print.html">print</a> <span class="i">STDERR</span> <span class="q">&quot;Perl: &quot;</span><span class="cm">,</span> <span class="i">@_</span><span class="sc">;</span> <span class="s">}</span><span class="sc">;</span>
	<span class="i">$SIG</span>{<span class="w">__DIE__</span>}  = <a class="l_k" href="functions/sub.html">sub</a><span class="s">{</span> <a class="l_k" href="functions/print.html">print</a> <span class="i">STDERR</span> <span class="q">&quot;Perl: &quot;</span><span class="cm">,</span> <span class="i">@_</span><span class="sc">;</span> <a class="l_k" href="functions/exit.html">exit</a> <span class="n">1</span><span class="s">}</span><span class="sc">;</span>
	<span class="s">}</span></pre>
<pre class="verbatim">	<span class="i">$a</span> = <span class="n">1</span> + <a class="l_k" href="functions/undef.html">undef</a><span class="sc">;</span>
	<span class="i">$x</span> / <span class="n">0</span><span class="sc">;</span>
<a name="__END__"></a>	__END__</pre>
<p>The perl message comes out with "Perl" in front.  The BEGIN block
works at compile time so all of the compilation errors and warnings
get the "Perl:" prefix too.</p>
<pre class="verbatim">	Perl: Useless use of division (/) in void context at ./test line 9.
	Perl: Name "main::a" used only once: possible typo at ./test line 8.
	Perl: Name "main::x" used only once: possible typo at ./test line 9.
	Perl: Use of uninitialized value in addition (+) at ./test line 8.
	Perl: Use of uninitialized value in division (/) at ./test line 9.
	Perl: Illegal division by zero at ./test line 9.
	Perl: Illegal division by zero at -e line 3.</pre><p>If I don't see that "Perl:", it's not from perl.</p>
<p>You could also just know all the perl errors, and although there are
some people who may know all of them, you probably don't.  However, they
all should be in the perldiag manpage. If you don't find the error in
there, it probably isn't a perl error.</p>
<p>Looking up every message is not the easiest way, so let perl to do it
for you.  Use the diagnostics pragma with turns perl's normal messages
into longer discussions on the topic.</p>
<pre class="verbatim">	<a class="l_k" href="functions/use.html">use</a> <span class="w">diagnostics</span><span class="sc">;</span></pre>
<p>If you don't get a paragraph or two of expanded discussion, it
might not be perl's message.</p>
<a name="How-do-I-install-a-module-from-CPAN%3f"></a><h2>How do I install a module from CPAN?</h2>
<p>The easiest way is to have a module also named CPAN do it for you.
This module comes with perl version 5.004 and later.</p>
<pre class="verbatim">	<span class="i">$ perl</span> -<span class="w">MCPAN</span> -e <span class="w">shell</span></pre>
<pre class="verbatim">	cpan shell -- CPAN exploration and modules installation (v1.59_54)
	ReadLine support enabled</pre><pre class="verbatim">	<span class="w">cpan</span>&gt; <span class="w">install</span> <span class="w">Some::Module</span></pre>
<p>To manually install the CPAN module, or any well-behaved CPAN module
for that matter, follow these steps:</p>
<ol>
<li>
<p>Unpack the source into a temporary area.</p>
</li>
<li>
<pre class="verbatim">	<span class="w">perl</span> <span class="w">Makefile</span>.<span class="w">PL</span></pre>
</li>
<li>
<pre class="verbatim">	<span class="w">make</span></pre>
</li>
<li>
<pre class="verbatim">	<span class="w">make</span> <span class="w">test</span></pre>
</li>
<li>
<pre class="verbatim">	<span class="w">make</span> <span class="w">install</span></pre>
</li>
</ol>
<p>If your version of perl is compiled without dynamic loading, then you
just need to replace step 3 (<b>make</b>) with <b>make perl</b> and you will
get a new <i>perl</i> binary with your extension linked in.</p>
<p>See <a href="ExtUtils/MakeMaker.html">ExtUtils::MakeMaker</a> for more details on building extensions.
See also the next question, "What's the difference between require
and use?".</p>
<a name="What's-the-difference-between-require-and-use%3f"></a><h2>What's the difference between require and use?</h2>
<p>Perl offers several different ways to include code from one file into
another.  Here are the deltas between the various inclusion constructs:</p>
<pre class="verbatim">	1)  do $file is like eval `cat $file`, except the former
	1.1: searches @INC and updates %INC.
	1.2: bequeaths an *unrelated* lexical scope on the eval'ed code.</pre><pre class="verbatim">	2)  require $file is like do $file, except the former
	2.1: checks for redundant loading, skipping already loaded files.
	2.2: raises an exception on failure to find, compile, or execute $file.</pre><pre class="verbatim">	3)  require Module is like require "Module.pm", except the former
	3.1: translates each "::" into your system's directory separator.
	3.2: primes the parser to disambiguate class Module as an indirect object.</pre><pre class="verbatim">	4)  use Module is like require Module, except the former
	4.1: loads the module at compile time, not run-time.
	4.2: imports symbols and semantics from that package to the current one.</pre><p>In general, you usually want <code class="inline"><a class="l_k" href="functions/use.html">use</a></code> and a proper Perl module.</p>
<a name="How-do-I-keep-my-own-module%2flibrary-directory%3f"></a><h2>How do I keep my own module/library directory?</h2>
<p>When you build modules, tell Perl where to install the modules.</p>
<p>For <code class="inline"><span class="w">Makefile</span>.<span class="w">PL</span></code>
-based distributions, use the PREFIX and LIB options
when generating Makefiles:</p>
<pre class="verbatim">	perl Makefile.PL PREFIX=/mydir/perl LIB=/mydir/perl/lib</pre><p>You can set this in your CPAN.pm configuration so modules automatically install
in your private library directory when you use the CPAN.pm shell:</p>
<pre class="verbatim">	% cpan
	cpan&gt; o conf makepl_arg PREFIX=/mydir/perl,LIB=/mydir/perl/lib
	cpan&gt; o conf commit</pre><p>For <code class="inline"><span class="w">Build</span>.<span class="w">PL</span></code>
-based distributions, use the --install_base option:</p>
<pre class="verbatim">	perl Build.PL --install_base /mydir/perl</pre><p>You can configure CPAN.pm to automatically use this option too:</p>
<pre class="verbatim">	% cpan
	cpan&gt; o conf mbuild_arg --install_base /mydir/perl
	cpan&gt; o conf commit</pre><a name="How-do-I-add-the-directory-my-program-lives-in-to-the-module%2flibrary-search-path%3f"></a><h2>How do I add the directory my program lives in to the module/library search path?</h2>
<p>(contributed by brian d foy)</p>
<p>If you know the directory already, you can add it to <code class="inline"><span class="i">@INC</span></code>
 as you would
for any other directory. You might &lt;use lib&gt; if you know the directory
at compile time:</p>
<pre class="verbatim">	<a class="l_k" href="functions/use.html">use</a> <span class="w">lib</span> <span class="i">$directory</span><span class="sc">;</span></pre>
<p>The trick in this task is to find the directory. Before your script does
anything else (such as a <code class="inline"><a class="l_k" href="functions/chdir.html">chdir</a></code>), you can get the current working
directory with the <code class="inline"><span class="w">Cwd</span></code>
 module, which comes with Perl:</p>
<pre class="verbatim">	BEGIN <span class="s">{</span>
		<a class="l_k" href="functions/use.html">use</a> <span class="w">Cwd</span><span class="sc">;</span>
		<a class="l_k" href="functions/our.html">our</a> <span class="i">$directory</span> = <span class="w">cwd</span><span class="sc">;</span>
		<span class="s">}</span></pre>
<pre class="verbatim">	<a class="l_k" href="functions/use.html">use</a> <span class="w">lib</span> <span class="i">$directory</span><span class="sc">;</span></pre>
<p>You can do a similar thing with the value of <code class="inline"><span class="i">$0</span></code>
, which holds the
script name. That might hold a relative path, but <code class="inline"><span class="w">rel2abs</span></code>
 can turn
it into an absolute path. Once you have the</p>
<pre class="verbatim">	BEGIN {	
		use File::Spec::Functions qw(rel2abs);
		use File::Basename qw(dirname);</pre><pre class="verbatim">		my $path   = rel2abs( $0 );
		our $directory = dirname( $path );
		}</pre><pre class="verbatim">	<a class="l_k" href="functions/use.html">use</a> <span class="w">lib</span> <span class="i">$directory</span><span class="sc">;</span></pre>
<p>The <code class="inline"><span class="w">FindBin</span></code>
 module, which comes with Perl, might work. It searches
through <code class="inline"><span class="i">$ENV</span>{<span class="w">PATH</span>}</code>
 (so your script has to be in one of those
directories). You can then use that directory (in <code class="inline"><span class="i">$FindBin::Bin</span></code>
)
to locate nearby directories you want to add:</p>
<pre class="verbatim">	<a class="l_k" href="functions/use.html">use</a> <span class="w">FindBin</span><span class="sc">;</span>
	<a class="l_k" href="functions/use.html">use</a> <span class="w">lib</span> <span class="q">&quot;$FindBin::Bin/../lib&quot;</span><span class="sc">;</span></pre>
<a name="How-do-I-add-a-directory-to-my-include-path-(%40INC)-at-runtime%3f"></a><h2>How do I add a directory to my include path (@INC) at runtime?</h2>
<p>Here are the suggested ways of modifying your include path, including
environment variables, run-time switches, and in-code statements:</p>
<ul>
<li><a name="the-PERLLIB-environment-variable"></a><b>the PERLLIB environment variable</b>
<pre class="verbatim">	$ export PERLLIB=/path/to/my/dir
	$ perl program.pl</pre></li>
<li><a name="the-PERL5LIB-environment-variable"></a><b>the PERL5LIB environment variable</b>
<pre class="verbatim">	$ export PERL5LIB=/path/to/my/dir
	$ perl program.pl</pre></li>
<li><a name="the-perl--Idir-command-line-flag"></a><b>the perl -Idir command line flag</b>
<pre class="verbatim">	$ perl -I/path/to/my/dir program.pl</pre></li>
<li><a name="the-use-lib-pragma%3a"></a><b>the use lib pragma:</b>
<pre class="verbatim">	<a class="l_k" href="functions/use.html">use</a> <span class="w">lib</span> <span class="q">&quot;$ENV{HOME}/myown_perllib&quot;</span><span class="sc">;</span></pre>
</li>
</ul>
<p>The last is particularly useful because it knows about machine
dependent architectures.  The lib.pm pragmatic module was first
included with the 5.002 release of Perl.</p>
<a name="What-is-socket.ph-and-where-do-I-get-it%3f"></a><h2>What is socket.ph and where do I get it?</h2>
<p>It's a Perl 4 style file defining values for system networking
constants.  Sometimes it is built using h2ph when Perl is installed,
but other times it is not.  Modern programs <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">Socket</span><span class="sc">;</span></code>
 instead.</p>
<a name="REVISION"></a><h1>REVISION</h1>
<p>Revision: $Revision: 10183 $</p>
<p>Date: $Date: 2007-11-07 09:35:12 +0100 (Wed, 07 Nov 2007) $</p>
<p>See <a href="perlfaq.html">perlfaq</a> for source control details and availability.</p>
<a name="AUTHOR-AND-COPYRIGHT"></a><h1>AUTHOR AND COPYRIGHT</h1>
<p>Copyright (c) 1997-2007 Tom Christiansen, Nathan Torkington, and
other authors as noted. All rights reserved.</p>
<p>This documentation is free; you can redistribute it and/or modify it
under the same terms as Perl itself.</p>
<p>Irrespective of its distribution, all code examples in this file
are hereby placed into the public domain.  You are permitted and
encouraged to use this code in your own programs for fun
or for profit as you see fit.  A simple comment in the code giving
credit would be courteous but is not required.</p>
</div>
      <div id="contentFooter"><a href="http://www.perl.org"><img src="perlpowered.png" border=0></a></div>
    </div>
  </div>

  <div id="right">
    <div id="rightContent">
      <div id="leftClose">
        <a href="#" onClick="closeRight()" title="Hide toolbar" onmouseover="rightCloseIcon.src='close_purple.gif';" onmouseout="rightCloseIcon.src='close_blue.gif';"><img src="close_blue.gif" name="rightCloseIcon" id="rightCloseIcon" border=0></a>
      </div>
      <h1>Search:</h1>
      <p>
        <form action="search.html" name="perldoc_search">
	  <input type="text" name="q" size="10" class="grey"><br>
	  <!--<select name="r"><option value="1" selected>Go to top result<option value="0">Show results list</select>-->
	</form>
      </p>
      <script language="JavaScript" type="text/javascript" src="/perl-version.js"></script>
      <h2>Labels:</h2>
      <p>
        <a href="#" onClick="addLabel('perlfaq8','perlfaq8.html')">Add this page</a>
      </p>
      <div class="labels" id="labels">
      </div>
    </div>
  </div>

</div>

</body>
</html>