🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: algonet.amiga
Article #43

Re: perl fil I/O

#43
From: Jesper Nilsson
Date: Wed, 28 Feb 1996 00:00
88 lines
3415 bytes
In swnet.unix hansmbg@algonet.se (Hans Malmberg) stated:
>PERL.
Saviour of the world.

>Jag har b�rjat arbeta p� att ha separata r�knare p�
>varje sida i mitt 'public_html' bibliotek.
>
>D� uppst�r behov att spara r�knare f�r varje sida,
>just nu �r tanken att spara de i en fil 'counter_##'.
>
>$count_file = "../log-files/counter_" . $postfix ;
>
>sid-numret �r argument till rutinen, extraheras och
>adderas skapande filnamn f�r aktuell r�knare.
>
>Men, jag m�ste testa om det finns en fil ( om jag l�gger
>upp en ny sida vill jag inte kladda med cgi-bin-filer utan de
>skall klara av det sj�lv).

Om du bara vill kolla om det finns en fil s� kan du anv�nda
operatorn "-e":

if ( -e "$count_file") {
  # Filen finns
  do_whatever();
} else {
  # filen finns inte
  open(CHECK_LOG, ">$count_file") || die "Can't open $count_file";
  print(CHECK_LOG, "0");
  close(CHECK_LOG) || die "Can't close $count_file";
}

Ytterligare intressanta filoperatorer (perl5):

    -e  File exists.
    -z  File has zero size.
    -s  File has non-zero size (returns size).
    
    -f  File is a plain file.
    -d  File is a directory.
    -l  File is a symbolic link.
    -p  File is a named pipe (FIFO).
    -S  File is a socket.
    -b  File is a block special file.
    -c  File is a character special file.
    -t  Filehandle is opened to a tty.
    
    -T  File is a text file.
    -B  File is a binary file (opposite of -T).

Se annars i manualsidan perlfunc.

>D� m�ste jag testa om r�knarfilen
>"../log-files/counter_xx" finns, och jag har gjort s� h�r:
>
>/*--------------------------------------------*/
>
>$res1 = open(CHECK_LOG, "$count_file");
>
>if($res1 == "") { 
>   open(CHECK_LOG, ">$count_file") || print "Can't Open $count_file:$!\n";
>   print(CHECK_LOG, "0");
>   close(CHECK_LOG);
>   }
>/*--------------------------------------------*/
>
>lite os�ker p� hakar och parenteser, men det fixar sig s�kert.
>
>instruktion efter '||' �r just nu f�r avlusning, den skippas senare.
>hrhrrm, jag ligger ju knappast i shell n�r detta intr�ffar..
>under drift, s� att s�ga.

Man b�r alltid av�nda die efter anrop som kan misslyckas.
Om inte annat kan man uppt�cka felet i httpd-loggarna...

>Men, jag undrar om n�gon har synpunkter p� just det h�r med att
>�ppna -/- kontrollera -/- skapa fil i 'perl'.
>
>Uppskattar s�dana....d� jag �r s� os�ker p� att jag g�r 'r�tt'.

Hoppas detta hj�lper.

>Hans

/^JN - Jesper Nilsson
--
              I've heard of UNIseX, but I've never had it.
      Jesper Nilsson -- dat92jni@ludat.lth.se || jesper@df.lth.se
Technical Details
Message-ID: <4h1r95$amf@news.lth.se>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <1422.6630T1243T2563@algonet.se>

Show Full Headers
Complete Article Headers
X-Google-Language: SWEDISH,ASCII
X-Google-Thread: fad40,31d8b5f4ccecfc75
X-Google-Attributes: gidfad40,public
X-Google-Thread: f7e14,31d8b5f4ccecfc75
X-Google-Attributes: gidf7e14,public
X-Google-Thread: f7a1c,31d8b5f4ccecfc75
X-Google-Attributes: gidf7a1c,public
X-Google-Thread: 10e356,31d8b5f4ccecfc75
X-Google-Attributes: gid10e356,public
From: dat92jni@ludat.lth.se (Jesper Nilsson)
Subject: Re: perl fil I/O
Date: 1996/02/28
Message-ID: <4h1r95$amf@news.lth.se>
X-Deja-AN: 141498865
references: <1422.6630T1243T2563@algonet.se>
organization: Dept. of Computer Science, Lund University, Sweden
newsgroups: algonet.amiga,algonet.test,algonet.unix,swnet.unix