Server Side Programming Articles - Page 2068 of 2650

Reading and Writing Files in Perl

Mohd Mohtashim
Updated on 29-Nov-2019 10:20:28

910 Views

Once you have an open file handle in Perl, you need to be able to read and write information. There are a number of different ways of reading and writing data into the file.The OperatorThe main method of reading the information from an open filehandle is the operator. In a scalar context, it returns a single line from the filehandle. For example −#!/usr/bin/perl print "What is your name?"; $name = ; print "Hello $name";When you use the operator in a list context, it returns a list of lines from the specified filehandle. For example, to import all ... Read More

Opening and Closing Files in Perl

Mohd Mohtashim
Updated on 29-Nov-2019 10:17:57

4K+ Views

There are following two functions with multiple forms, which can be used to open any new or existing file in Perl.open FILEHANDLE, EXPR open FILEHANDLE sysopen FILEHANDLE, FILENAME, MODE, PERMS sysopen FILEHANDLE, FILENAME, MODEHere FILEHANDLE is the file handle returned by the open function and EXPR is the expression having file name and mode of opening the file.Open FunctionFollowing is the syntax to open file.txt in read-only mode. Here less than < sign indicates that file has to be opened in read-only mode.open(DATA, ">file.txt") || die "Couldn't open file file.txt, $!";A double >> opens the file for appending, placing the file pointer ... Read More

Define a Report Footer in Perl

Mohd Mohtashim
Updated on 29-Nov-2019 10:13:57

192 Views

While $^ or $FORMAT_TOP_NAME contains the name of the current header format in Perl, there is no corresponding mechanism to automatically do the same thing for a footer. If you have a fixed-size footer, you can get footers by checking variable $- or $FORMAT_LINES_LEFT before each write() and print the footer yourself if necessary using another format defined as follows −format EMPLOYEE_BOTTOM = End of Page @<           $% .For a complete set of variables related to formatting, please refer to the Perl Special Variables section.

Define a Pagination in Perl

Mohd Mohtashim
Updated on 29-Nov-2019 10:09:51

221 Views

What about if your report is taking more than one page? You have a good solution in Perl to create pagination. We simply use $% or $FORMAT_PAGE_NUMBER variable along with header as follows −format EMPLOYEE_TOP = =================================== Name                   Age Page @< $% =================================== .Now your output will look like as follows −=================================== Name                  Age Page 1 =================================== =================================== Ali                   20 2000.00 =================================== =================================== Raza                  30 2500.00 =================================== =================================== Jaffer                40 4000.00 ===================================

Create a Report Header using Perl

Mohd Mohtashim
Updated on 29-Nov-2019 10:07:32

305 Views

Sometime you would be interested in adding a header to your report. This header will be printed on top of each page. It is very simple to do this using Perl. Apart from defining a template you would have to define a header and assign it to $^ or $FORMAT_TOP_NAME variable −Example Live Demo#!/usr/bin/perl format EMPLOYEE = =================================== @

How to use Formats in Perl?

Mohd Mohtashim
Updated on 29-Nov-2019 10:04:54

269 Views

In order to invoke a format declaration in Perl Script, we would use the write keyword −write EMPLOYEE;The problem is that the format name is usually the name of an open file handle, and the write statement will send the output to this file handle. As we want the data sent to the STDOUT, we must associate EMPLOYEE with the STDOUT filehandle. First, however, we must make sure that that STDOUT is our selected file handle, using the select() function.select(STDOUT);We would then associate EMPLOYEE with STDOUT by setting the new format name with STDOUT, using the special variable $~ or $FORMAT_NAME as ... Read More

References to Functions in Perl

Mohd Mohtashim
Updated on 29-Nov-2019 09:59:08

220 Views

While using Perl script, this might happen if you need to create a signal handler so you can produce a reference to a function by preceding that function name with \& and to dereference that reference you simply need to prefix reference variable using ampersand &. Following is an example −Example Live Demo#!/usr/bin/perl # Function definition sub PrintHash {    my (%hash) = @_;    foreach $item (%hash) {       print "Item : $item";    } } %hash = ('name' => 'Tom', 'age' => 19); # Create a reference to above function. $cref = \&PrintHash; # Function call using ... Read More

How to Define a Format in Perl?

Mohd Mohtashim
Updated on 29-Nov-2019 10:01:08

157 Views

Perl uses a writing template called a 'format' to output reports. To use the format feature of Perl, you have to define a format first and then you can use that format to write formatted data.SyntaxFollowing is the syntax to define a Perl format −format FormatName = fieldline value_one, value_two, value_three fieldline value_one, value_two .Here FormatName represents the name of the format. The fieldline is the specific way, the data should be formatted. The values lines represent the values that will be entered into the field line. You end the format with a single period.Next fieldline can contain any text or field holders. ... Read More

Circular References in Perl

Mohd Mohtashim
Updated on 29-Nov-2019 07:19:21

336 Views

A circular reference in Perl occurs when two references contain a reference to each other. You have to be careful while creating references otherwise a circular reference can lead to memory leaks. Following is an example −Example Live Demo#!/usr/bin/perl my $foo = 100; $foo = \$foo; print "Value of foo is : ", $$foo, "";OutputWhen the above program is executed, it produces the following result −Value of foo is : REF(0x9aae38)

Dereferencing in Perl

Mohd Mohtashim
Updated on 29-Nov-2019 07:16:39

2K+ Views

Dereferencing in Perl returns the value from a reference point to the location. To dereference a reference simply use $, @ or % as a prefix of the reference variable depending on whether the reference is pointing to a scalar, array, or hash. Following is the example to explain the concept −Example Live Demo#!/usr/bin/perl $var = 10; # Now $r has reference to $var scalar. $r = \$var; # Print value available at the location stored in $r. print "Value of $var is : ", $$r, ""; @var = (1, 2, 3); # Now $r has reference to ... Read More

Advertisements