<dd id="sga4y"><optgroup id="sga4y"></optgroup></dd>
<nav id="sga4y"><code id="sga4y"></code></nav>
  • <optgroup id="sga4y"></optgroup>

    ??????  ?????

    ??????????/???????
      1??????I/O????
        1)open????
        2)??open?????????
        3)????????
        4)?????忱???
        5)close????
        6)print, printf??write????
        7)select????
        8)eof????
        9)??????????
      2???????????????
      3??????忱????
      4????getc??????
      5????binmode????????????
    ?????????????
      1??mkdir
      2??chdir
      3??opendir
      4??closedir
      5??readdir
      6??telldir
      7??seekdir
      8??rewinddir
      9??rmdir
    ??????????????
      1????????竹????
      2?????????????????
      3?????????????
      4?????????????
    ??????DBM???

        ??????????????????????UNIX????????????????UNIX???孝??宏??????????忪?????志???????????????????Perl?????????
    ??????????/???????
        ?????????????忪????????????忱????????????????
    1??????I/O????
        ?宏I/O??????????????????扶???????
  • open???????????????
  • close????????????
  • print?????忱???????
  • write???????忱?????????
  • printf??????????????????????
  •     ??????????㏒?????宏???汛?????????
    1)open????
        open?????????????????????????????????????????????鉸open(MYVAR, "/u/file"); ????????????????????????????????????open????????????????????????????忱??????????????????????????open(MYVAR, ">/u/file"); ?????快?????汕???????????????????open(MYVAR, ">>/u/file"); ????????????????????????????????????????(|)??open(MAIL, "|mail dave");
    2)??open?????????
        ??????????????????????????????????????????????????????(|)???鉸
        open(CAT, "cat file*|");
        ??open?????????????cat file* ?????????????????????????????????????????file???????????????????????????????????????????????????CAT??????鉸
        $input = ;
        ????????????????w????????忍????????????????????
    1 : #!/usr/local/bin/perl
    2 :
    3 : open (WOUT, "w|");
    4 : $time = <WOUT>;
    5 : $time =~ s/^ *//;
    6 : $time =~ s/ .*//;
    7 : ; # skip headings line
    8 : @users = ;
    9 : close (WOUT);
    10: foreach $user (@users) {
    11:   $user =~ s/ .*//;
    12: }
    13: print ("Current time: $time");
    14: print ("Users logged on:\n");
    15: $prevuser = "";
    16: foreach $user (sort @users) {
    17:   if ($user ne $prevuser) {
    18:     print ("\t$user");
    19:     $prevuser = $user;
    20:   }
    21: }
        ?????????㏒?
    Current time: 4:25pm
    Users logged on:
      dave
      kilroy
      root
      zarquon
        w?????忍??????????????????????????????????????????????快??????鉸
      4:25pm  up 1 day,  6:37,  6 users,  load average: 0.79, 0.36, 0.28
    User     tty       login@  idle   JCPU   PCPU what
    dave     ttyp0     2:26pm           27      3 w
    kilroy   ttyp1     9:01am  2:27   1:04     11 -csh
    kilroy   ttyp2     9:02am    43   1:46     27 rn
    root     ttyp3     4:22pm     2               -csh
    zarquon  ttyp4     1:26pm     4     43     16 cc myprog.c
    kilroy   ttyp5     9:03am         2:14     48 /usr/games/hack
    
        ?????忱?w???????????????????????????????????????????3??????w?????????open????????w???????????????????????????WOUT?????????????4?忪????????????????
        4:25pm up 1 day, 6:37, 6 users, load average: 0.79, 0.36, 0.28
        ???????????忱??????忍??????????????5????????????????6???????????汕???戒?????????????????????$time??
        ??7?忱?WOUT???????孝??????????????????????????????8?忘???米??我???????@users??????9?抄??WOUT?????????w?????????
        @users?快????????????????????????????????????快?????????????????????10~12?????????戒?????????????????????????@users??????????????忌??
        ??13???????????$time?快??????????print???????????戒??????$time???妊?16~21?忪?@users?快??????????????????????????????汍??????????$preuser???????????????????????∩???????????$user??????????$preser???????????
    3)????????
        ???UNIX shell?????????????(STDOUT)???????????(STDERR)??????????????????????Bourne Shell??sh???孝?????
        $ foo > file1 2>&1
        ????????foo???????????????????????????????????????????file1?妊?????????Perl????????????????
    1: #!/usr/local/bin/perl
    2:
    3: open (STDOUT, ">file1") || die ("open STDOUT failed");
    4: open (STDERR, ">&STDOUT") || die ("open STDERR failed");
    5: print STDOUT ("line 1\n");
    6: print STDERR ("line 2\n");
    7: close (STDOUT);
    8: close (STDERR);
        ???抗????file1?快????????
         line 2
         line 1
        ??????????????志?汛????????????????????????????????????????污???
        ??3???????????????????????????file1?????????????STDOUT??????????????????????????4??????????????????????>&STDOUT????Perl????????????????STDOUT????????????????????STDERR?????STDOUT????????????5??6?戒????STDOUT??STDERR忱????????????????????????????????????????????????忱?????file1?孝????????????????????????
        ????????UNIX??????????????????print??????????????忱??STDOUT????????UNIX?????????????????????????????????????r???????孝????????????????????忱?????????忱???????????????????????????????????????????忱????????????????????????????????汎?????????????????????????????????I/O?????????????
        ???????????百灰?????????????????????????STDOUT??STDERR?????????????????????????STDERR???????????????STDERR????????快?????line 2???????????STDOUT????????快?????line 1????
        ???????????????????Perl?????????????????????????
         1????select??????????
         2?????1??????????$|
        ??????$|???????????抖?????????????????????^???$|?????????????^$|????????$~??$^尿?????????汛????select???????$|????????????????????????????
    1 : #!/usr/local/bin/perl
    2 :
    3 : open (STDOUT, ">file1") || die ("open STDOUT failed");
    4 : open (STDERR, ">&STDOUT") || die ("open STDERR failed");
    5 : $| = 1;
    6 : select (STDERR);
    7 : $| = 1;
    8 : print STDOUT ("line 1\n");
    9 : print STDERR ("line 2\n");
    10: close (STDOUT);
    11: close (STDERR);
        ???????抗????file1?????????
         line 1
         line 2
        ??5?扶?$|????1??????Perl???????????????????抖?????汛????select?????????????????????file1??STDOUT????6?扶????????????STDERR????7????????$|?1????????????file1???????????????^????STDOUT??STDERR?????????????????????????忱??????孝????line 1?????????妊?
    4)?????忱???
        ????????????忱???????????????????????"+>"?????㏒?
         open (READWRITE, "+>file1");
        ?????????????忱?????file1??????????忱???快???????????忱????????????seek??tell???????????????????????百????
        ??????????"+<"??????忱????
    5)close????
        ??????????????????close????????????????????????????????????????????鉸
         open (MYPIPE, "cat file*|");
         close (MYPIPE);
        ??????????????????????????孝????????cat file*????????
    6)print, printf??write????
        print????????????????????????????????????????汛?????????????????????孝??鉸
         print ("Hello, there!\n");
         print OUTFILE ("Hello, there!\n");
        ???????????????????孝???汛????select?????STDOUT?????????????????????OUTFILE?????????妊?
        printf?????????????????????????????????????孝??鉸
         printf OUTFILE (??You owe me %8.2f", $owing);
        ????????????$owing??????I????快?%8.2f??%8.2f???????????????$owing???????????????
        write???????????????????????????孝??鉸
         select (OUTFILE);
         $~ = "MYFORMAT";
         write;
        ????printf??write?????????x?? ????????????
    7)select????
        select??????????????????????????????米???????????鉸
         select (MYFILE);
        ??????MYFILE??????????????????print??write??printf?????汛????????????????MYFILE?妊?
    8)eof????
        eof???????????汎???????????????????????????????????????????????????????????????
        ???????㏒???eof????辰???????????eof??eof()???完???????<>???????????????eof??eof()????????????????????????????????????file1??file2??file1?????????
         This is a line from the first file.
         Here is the last line of the first file.
        file2?????????
         This is a line from the second and last file.
         Here is the last line of the last file.
        ????????????eof??eof()??????????????????
    1: #!/usr/local/bin/perl
    2:
    3: while ($line = <>) {
    4:   print ($line);
    5:   if (eof) {
    6:     print ("-- end of current file --\n");
    7:   }
    8: }
        ???扶?????㏒?
    $ program file1 file2
    This is a line from the first file.
    Here is the last line of the first file.
    -- end of current file --
    This is a line from the second and last file.
    Here is the last line of the last file.
    -- end of current file --
    $
        ?????eof???eof()??????????????
    1: #!/usr/local/bin/perl
    2:
    3: while ($line = <>) {
    4:   print ($line);
    5:   if (eof()) {
    6:     print ("-- end of output --\n");
    7:   }
    8: }
        ???扶?????㏒?
    $ program file1 file2
    This is a line from the first file.
    Here is the last line of the first file.
    This is a line from the second and last file.
    Here is the last line of the last file.
    -- end of output --$
        ????????????????????????eof()???????????????????????????汕????????????????????????????
    9)??????????
        ??????????????open, close, print, printf, write, select??eof?????????邦?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????open, close, write, select??eof??????????????????????????????????????????????????????????????
    1: #!/usr/local/bin/perl
    2:
    3: &open_file("INFILE", "", "file1");
    4: &open_file("OUTFILE", ">", "file2");
    5: while ($line = &read_from_file("INFILE")) {
    6:   &print_to_file("OUTFILE", $line);
    7: }
    8:
    9: sub open_file {
    10:   local ($filevar, $filemode, $filename) = @_;
    11:
    12:   open ($filevar, $filemode . $filename) ||
    13:     die ("Can't open $filename");
    14: }
    15: sub read_from_file {
    16:   local ($filevar) = @_;
    17:
    18:   <$filevar>;
    19: }
    20: sub print_to_file {
    21:   local ($filevar, $line) = @_;
    22:
    23:   print $filevar ($line);
    24: }
    2???????????????

    ?????? seek
    ?????? seek (filevar, distance, relative_to);
    ??? ??????????/???????????????????
    1??filevar?????????
    2??distance?????????????????????????????????????
    3??reletive_to??????0??1??2???0??????????????????1??????????竹??????????????孝???????2????????????汕?????
    ???忍?????????????????????????????tell???????芍?

    ?????? tell
    ?????? tell (filevar);
    ??? ??????????????竹??????
    ???
    1??seek??tell????????????????????????
    2??seek??tell???????????????????????

    3??????忱????

    ?????? read
    ?????? read (filevar, result, length, skipval);
    ??? read??????????UNIX??fread??????完???????????????????????????????????????????????????
    1??filevar?????????
    2??result????????????????????????????
    3??length????????????
    4??skipval??????????????????????????????
    ????????????????????????????????汕????????????????????????

    ?????? sysread
    ?????? sysread (filevar, result, length, skipval);
    ??? ??????????????UNIX????read??完????????read?????

    ?????? syswrite
    ?????? syswrite (filevar, data, length, skipval);
    ??? ?????忱?????????UNIX????write??完????????
    1??filevar?????忱??????
    2??data???????忱??????????
    3??length???忱????????
    4??skipval忱???????????????????

    4????getc??????

    ?????? getc
    ?????? $char = getc (infile);
    ??? ??????忪???????????

    5????binmode????????????

    ?????? binmode
    ?????? binmode (filevar);
    ??? ?????????????DOS?????????????????????????????????芍??????????????????????芍?

    ?????????????

    ?????? mkdir
    ?????? mkdir (dirname, permissions);
    ??? ?????????????????
    1??dirname????????????????????????????????
    2??permissions??8?????????????????????????????????㊣?????????????????????????

    ? ???
    4000   ????????????ID  
    2000 ???????????ID
    1000 ???竹
    0400 ?????????
    0200 ?????忱???
    0100 ???????????
    0040 ??????
    0020 ??忱???
    0010 ????????
    0004 ??????????
    0002 ??????忱???
    0001 ????????????

    ?????? chdir
    ?????? chdir (dirname);
    ??? ????????????????dirname????????????????????????

    ?????? opendir
    ?????? opendir (dirvar, dirname);
    ??? ???????????u???????????????????????忌?????????
    1??dirvar???????????????????????
    2??dirname?????????????????????
    ????????????????????
    ????????扭????????????????????????????????????????

    ?????? closedir
    ?????? closedir (mydir);
    ??? ??????????

    ?????? readdir
    ?????? readdir (mydir);
    ??? ???????????????牟?????????????????????????????????????????????

    ?????? telldir
    ?????? location = telldir (mydir);
    ??? ????????????????????telldir???????seekdir?????????忌???????????

    ?????? seekdir
    ?????? seekdir(mydir, location);
    ??? location?????telldir????????

    ?????? rewinddir
    ?????? rewinddir (mydir);
    ??? ?????????竹????????????????????????忌??

    ?????? rmdir
    ?????? rmdir (dirname);
    ??? ?????????????????????????????????????????

    ??????????????
    1????????竹????

    ?????? rename
    ?????? rename (oldname, newname);
    ??? ???????????????????????孝????????????????????

    ?????? unlink
    ?????? num = unlink (filelist);
    ??? ???????????????????忌????????????????????????
    ???????????unlink??????delete???????????????????????????????

    2?????????????????

    ?????? link
    ?????? link (newlink, file);
    ??? ?????????????????--??????file?????????????newlink??????????????
    ????????????????
    ????????????????快????????????????????????????????

    ?????? symlink
    ?????? symlink (newlink, file);
    ??? ????????????????????????????????????????????????????????????????
    ??????????????鉸??unlinke??????????????????????????????????????????????????????????????

    ?????? readlink
    ?????? filename = readlink (linkname);
    ??? ???linkname??????????????????????????????????????????

    3?????????????

    ?????? chmod
    ?????? chmod (permissions, filelist);
    ??? ??????????????????????
    1??permissions??????????????儔???????mkdir??????
    2??filelist??????????????忌?

    ?????? chown
    ?????? chown (userid, groupid, filelist);
    ??? ??????????????????????????
    1??userid??????????(????)ID??
    2??groupid???米???(????)ID???-1????????
    3??filelist?????????????????忌?

    ?????? umask
    ?????? oldmaskval = umask (maskval);
    ??? ???????????????????????????????

    4?????????????

    ?????? truncate
    ?????? truncate (filename, length);
    ??? ??????????????length?????????????????妊??length???????百????????filename?????????????????????????

    ?????? stat
    ?????? stat (file);
    ??? ??????????????file??????????????????????????忌???????????
  • ????????蘟
  • ????羊???(inode)
  • ???????
  • ???????
  • ??????(????)ID
  • ???????(????)ID
  • ?蘟????????file???蘟?????
  • ?????妊?????????
  • ?????????
  • ?????????
  • ??????????
  • I/O?????????妊
  • ???????????????
  • ?????? lstat
    ?????? lstat (file);
    ??? ??stat????????????file??????????????

    ?????? time
    ?????? currtime = time();
    ??? ?????1970??1??1?????????????

    ?????? gmtime
    ?????? timelist = gmtime (timeval);
    ??? ????time, stat ?? -A ?? -M ????????????????????????????????????????忌???????????
  • ??
  • ????
  • 妊???0~23
  • ????
  • ?﹞??0~11(???~?????)
  • ???
  • ?????0~6(????~????)
  • ????快??????0~364
  • ????????????

  • ???UNIX??gmtime??????

    ?????? localtime
    ?????? timelist = localtime (timeval);
    ??? ??gmtime???????????????????????????

    ?????? utime
    ?????? utime (acctime, modtime, filelist);
    ??? ??????????????????????????????鉸
    $acctime = -A "file1";
    $modtime = -M "file1";
    @filelist = ("file2", "file3");
    utime ($acctime, $modtime, @filelist);

    ?????? fileno
    ?????? filedesc = fileno (filevar);
    ??? ????????????UNIX?????????????filevar??????????

    ?????? fcntl
    flock
    ?????? fcntl (filevar, fcntlrtn, value);
    flock (filevar, flockop);
    ??? ??????UNIX??????????

    ??????DBM???
       Perl?扭??迄?????????????DBM????????迆????dbmopen??dbmclose????Perl5?孝?????tie??untie???n

    ?????? dbmopen
    ?????? dbmopen (array, dbmfilename, permissions);
    ??? ????????????DBM?????????????????
    1??array?????迄???????
    2??dbmfilename????????DBM?????
    3?????????????mkdir

    ?????? dbmclose
    ?????? dbmclose (array);
    ??? ???DBM?????????????????????????



    ????? ????? ??

    paper | appdir | ssv 97av