HTML Tutorials |
|
XML Tutorials |
|
Browser Scripting |
|
Server Scripting |
|
.NET (dotnet) |
|
Multimedia |
|
Web Building |
|
Java Tutorials |
|
Programming Langauges |
|
Soft Skills |
|
Database Tutorials |
|
Operating System |
|
Software Testing |
|
SAP Module |
|
Networking Programming |
|
Microsoft Office |
|
Accounting |
|
|
Manipulação da lima do Perl |
In PERL files are given a name, also called handle. All the input and output of the file is
achieved by filehandling functions. Filehandles are also a means to communicate from one program
to another program.
|
How to assign handles
|
A filehandle is nothing but name given for the files which you intend to use in your
PERL programs and scripts. A handle is a name which is temporarly assigned to a file.
The example below shows how to use a file handle in your PERL program.
|
#!/usr/bin/perl
print "content-type: text/html \n\n"; #The header
$FilePath = "home/html/myhtml.html"
sysopen(HANDLE, $FilePath, O_RDWR);
printf HANDLE "Welcome to Tizag!";
close (HANDLE);
|
|
Files with the die Function
|
The die function also exists in several other programming languages. It is used to
kill your scripts and also helps to pinpoint where/if your code is failing. We use this function as
as shown below.
|
#!/usr/bin/perl
print "content-type: text/html \n\n"; #The header
$filepath = "htmlpage.html";
sysopen (HTML, '$filepath', O_RDWR|O_EXCL|O_CREAT, 0755) or die "$filepath
cannot be opened.";
printf HTML "<html>\n";
printf HTML "<head>\n";
printf HTML "vtitle>My Home Page</title>";
printf HTML "</head>\n";
printf HTML "<body>\n";
printf HTML "<p align='center'>Here we have an HTML
page with a paragraph.</p>";
printf HTML "v/body>\n";
printf HTML "</html>\n";
close (HTML);
|
|
If due to some problem PERL is unable to open or create our file, we will be informed.
It is good practice to make use of the die function and we will be using it as we go
deeper into the file handling.
|
How to Open the File
|
Files can be opened using either of open and sysopen function.
for either of the function can pass upto 4 arguments, the first argument is always the
file handle, then the file name also known as a URL or filepath, flags, and
finally any of the permissions that are to be granted to the file.
The following program opens up a previously saved HTML document.
|
#!/usr/bin/perl
print "content-type: text/html \n\n"; #The header
$FH = "filehandle";
$FilePath = "htmlpage.html";
open(FH, $FilePath, permissions);
or
sysopen(FH, $FileName, permission);
|
|
Files which are having unusual file names or special characters are all best opened by
declaring the URL first, as a variable. This method removes the confusion that might
occur when PERL tries to interpret the program. However, filenames require a step for a
brief character substitution before which they can be placed into the open statements.
|
Various File Permissions
|
File permissions are crucial to the file function and security. For instance, in order to
function, a PERL file (.pl) must have executable file permissions in order to function
on your web server. Also, you may not want all of your HTML files to be set to allow
others to write to them or over them. Here's a listing of what to pass to the open
function when working with file handles.
|
Shorthand Flags:
Entities Definition
< or r Read Only Access
> or w Creates, Writes, and Truncates
>>or a Writes, Appends, and Creates
+< or r+ Reads and Writes
+> or w+ Reads, Writes, Creates, and Truncates
+>> or a+ Reads, Writes, Appends, and Creates
|
|
O_ Flags:
Value Definition
O_RDWR Read and Write
O_RDONLY Read Only
O_WRONLY Write Only
O_CREAT Create the file
O_APPEND Append the file
O_TRUNC Truncate the file
O_EXCL Stops if file already exists
O_NONBLOCK Non-Blocking usability
|
|
#!/usr/bin/perl
print "content-type: text/html \n\n"; #The header
use Fcntl; #The Module
sysopen (HTML, '/home/html/htmlpage.html', O_RDWR|O_EXCL|O_CREAT, 0755);
sysopen (HTML, >htmlpage.html');
|
|
File Creation
|
Files are opened and created using the same function "sysopen".
Our syntax open(FILEHANDLE, '$filename', permissions, CHMOD);
or sysopen(FILEHANDLE, $filename, permissions, CHMOD);
|
#!/usr/bin/perl
use Fcntl; #The Module
print "content-type: text/html \n\n"; #The header
sysopen (HTML, 'myhtml.html', O_RDWR|O_EXCL|O_CREAT, 0755);
printf HTML "<html>\n";
printf HTML "<head>\n";
printf HTML "<title>My Home Page";
printf HTML "</head>\n";
printf HTML "<bodyv\n";
printf HTML "<p align='center'>Here we have an HTML
page with a paragraph.</p>";
printf HTML "</body>\n";
printf HTML "</html>\n";
close (HTML);
|
|
With sysopen you can also set hexadecimal priviledges; CHMOD values. Sysopen needs
the declaration of a new module for PERL. We will now be using the Fcntl module.
|
Reading from a File
|
It is easy to read lines from files and then input them using the input operator <>. By
placing the file handler inside the input operator, then your script will input that line
of the file..
|
#!/usr/bin/perl
print "content-type: text/html \n\n"; #The header
$HTML = "htmlpage.html";
open (HTML) or die "Can't open the file!";
print <HTML>;
close (HTML);
|
|
Here we have a small PERL script to display several lines of HTML code. Each line is
stored into an array and it is automatically printed to the browser in HTML format using
the input operator <> .
|
Copy uma lima |
Usando a função da cópia nós podemos duplicar a lima. O copy faz exame de dois argumentos, o URL da lima que necessidades ser copí e o URL da lima/diretório novos a que a lima deve ser copí. Se a mesma lima - o nome está ou o mesmo URL, reescrita do Perl sobre a lima se as permissões permitidas.
|
#!/usr/bin/perl
use File::Copy;
print "content-type: text/html \n\n"; #The header
$filetobecopied = "htmlpage.html.";
$newfile = "html/htmlpage.html.";
copy($filetobecopied, $newfile) or die "File cannot be copied.";
|
|
Aqui, nós duplicamos simplesmente a lima de “htmlpage.html” e estaremos usando-a nos exemplos futuros. Ao usar o Perl na correia fotorreceptora, é a mais melhor usar o URL completo do Internet. Nós usamos uma maneira do shorthand no exemplo, mas é melhor ao hardcode o URL cheio como: http://www.vyom.co.in/htmlpage.html.
|
Movendo as limas
|
Mover a lima requer o uso da função do “movimento”. Esta função trabalha similarmente à função da cópia de acima e nós emitimos o mesmo módulo ao Perl. A diferença é aqui está, em vez copí dos nós apenas “cortados” a lima e emite-a a uma posição nova. Este que funciona é mesmo que o corte e o texto colando do original do escritório a outro.
|
#!/usr/bin/perl
use File::Copy;
print "content-type: text/html \n\n"; #The header
$oldlocation = "htmlpage.html";
$newlocation = "html/htmlpage.html";
move($oldlocation, $newlocation);
|
|
Nossa lima tem sido removida agora completly de sua posição atual à posição nova.
|
Suprimindo as limas
|
Para suprimir limas específicas de seu web server usar “unlink” a função. A mais melhor maneira é frequentemente ajustar um nome variável igual ao URL da lima que você deseja suprimir.
|
#!/usr/bin/perl
print "content-type: text/html \n\n"; #The header
$file = "newtext.txt";
if (unlink($file) == 0) {
print "File deleted successfully.";
} else {
print "File was not deleted.";
}
|
|
Removendo as limas múltiplas em uma vez
|
Para remover as limas múltiplas em uma vez, nós devemos primeiramente criar uma disposição das limas que tenha que ser suprimida e então dado laços com cada. Há muitas outras maneiras ir sobre este processo.
|
#!/usr/bin/perl
print "content-type: text/html \n\n"; #The header
@files = ("newtext.txt","moretext.txt","yetmoretext.txt");
foreach $file (@files) {
unlink($file);
}
|
|
Keywords:perl tutorial, perl scripts, perl programming, active perl, perl download,
blackberry perl, perl regular expressions, perl split, perl array, perl script page
|
|
HTML Quizes |
|
XML Quizes |
|
Browser Scripting Quizes |
|
Server Scripting Quizes |
|
.NET (dotnet) Quizes |
|
Multimedia Quizes |
|
Web Building Quizes |
|
Java Quizes |
|
Programming Langauges Quizes |
|
Soft Skills Quizes |
|
Database Quizes |
|
Operating System Quizes |
|
Software Testing Quizes |
|
SAP Module Quizes |
|
Networking Programming Quizes |
|
Microsoft Office Quizes |
|
Accounting Quizes |
|
|