use lib; use strict; use warnings; use v5.10; # doar versiuni Perl de la 5.10 în sus my $nume_fisier = undef; my @continut = (); my $record = undef; my $index = undef; my $i = undef; $/ = "|"; #$\ = "|"; print "\n"; print 'Separatorul input este: ',"$/"; $nume_fisier = $ARGV[0]; #@ARGV[0] open (FISIER, "<$nume_fisier"); @continut = ; close FISIER; print "\n\n"; $index = $#continut; print "Au fost identificate ",$index+1," înregistrări\n"; print "Acestea sunt urmatoarele:\n\n"; for ($i=0; $i <= $index; $i++) { $continut[$i] =~ s/\n//g; print "Inregistrarea $i: $continut[$i]\n"; } # se poate încerca și varianta foreach # În cazul fișierelor, trebuie avut în vedere că # Perl nu citește rânduri, el doar citește niște înregistrări. # Fiecare înregistrare este identificată pe # baza unui separator care, în mod implicit, este setat să fie \n # Din aceasta cauză se creează impresia că, atunci când sunt citite fișiere text, # înregistrările coincid cu rândurile. Există două variabile scalare speciale # $/ și $\ pentru input (citire) respectiv output (scriere) # care stochează acest separator ce marchează o înregistrare și care pot fi modificate. # Implicit $/ are valoarea \n, iar $\ nu are nicio valoare și este rar utilizată. # Variabila $\ introduce un șir/caracter la sfarsitul execuției oricarei instrucțiuni print. # https://www.perl.com/pub/2004/06/18/variables.html/ # Se creează un nou fisier (inregistrari.txt) în care # se va scrie mereu prin adaugare >> conținutul # obținut prin concatenarea secvențelor, ori de câte ori este rulat programul. # Dacă operatorul >> este înlocuit cu > atunci, scrierea noilor date # se face prin ștergerea completă a ceea ce există inițial în fisier. # Într-o astfel de situație, ori de câte ori este rulat programul, # în fișier se vor regăsi doar datele trimise în cadrul ultimei operații de scriere. #open (SCRIERE, ">inregistrari.txt"); #print SCRIERE @continut; #close SCRIERE; #my $fisier = @ARGV[0]; #unless ( -e $fisier && -f $fisier && -r $fisier) #{ #die "$fisier NU poate fi accesat. Exista un astfel de fisier?\n"; #} =pod A file test. This unary operator takes one argument, either a filename or a filehandle, and tests the associated file to see if something is true about it. If the argument is omitted, tests $_, except for -t, which tests STDIN. It returns 1 for true and '' for false, or the undefined value if the file doesn't exist. Precedence is higher than logical and relational operators, but lower than arithmetic operators. The operator may be any of: -r File is readable by effective uid/gid. -w File is writable by effective uid/gid. -x File is executable by effective uid/gid. -o File is owned by effective uid. -R File is readable by real uid/gid. -W File is writable by real uid/gid. -X File is executable by real uid/gid. -O File is owned by real uid. -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. -u File has setuid bit set. -g File has setgid bit set. -k File has sticky bit set. -t Filehandle is opened to a tty. -T File is a text file. -B File is a binary file (opposite of -T). -M Age of file in days when script started. -A Same for access time. -C Same for inode change time. =cut # Using the DATA File Handle # This file handle lets you store read-only data in the same file # as your Perl script, which might come in handy if you need to send # both code and data to someone via e-mail. # When using the DATA file handle, you don't need to open or close the file handle, # just start reading from the file handle using the diamond operator. # The following simple example shows you how to use the DATA file handle. # The mothod is simple enough: # #@lines = ; #foreach (@lines) # { # print("$_"); # } # If you want print the special character value, try regex for substitute the \ with empty. #my $var = "\\n"; #$var =~s/\\//g; #print "$'";