Mohd Mohtashim has Published 202 Articles

The unless & die Function in Perl

Mohd Mohtashim

Mohd Mohtashim

Updated on 29-Nov-2019 11:41:22

1K+ Views

The unless function in Perl is the logical opposite to if: statements can completely bypass the success status and only be executed if the expression returns false. For example −unless(chdir("/etc")) {    die "Error: Can't change directory - $!"; }The unless statement is best used when you want to raise ... Read More

Create, Delete and Change Directories in Perl

Mohd Mohtashim

Mohd Mohtashim

Updated on 29-Nov-2019 11:39:54

4K+ Views

You can use mkdir function in Perl to create a new directory. You will need to have the required permission to create a directory.#!/usr/bin/perl $dir = "/tmp/perl"; # This creates perl directory in /tmp directory. mkdir( $dir ) or die "Couldn't create $dir directory, $!"; print "Directory created successfully";Remove a directoryYou ... Read More

Display all the Files in Perl

Mohd Mohtashim

Mohd Mohtashim

Updated on 29-Nov-2019 11:26:37

837 Views

There are various ways to list down all the files available in a particular directory using Perl. First let's use the simple way to get and list down all the files using the glob operator −#!/usr/bin/perl # Display all the files in /tmp directory. $dir = "/tmp/*"; my @files = glob( ... Read More

Getting File Information in Perl

Mohd Mohtashim

Mohd Mohtashim

Updated on 29-Nov-2019 10:29:21

668 Views

You can test certain features very quickly within Perl using a series of test operators known collectively as -X tests. For example, to perform a quick test of the various permissions on a file, you might use a script like this −#/usr/bin/perl my $file = "/usr/test/file1.txt"; my (@description, $size); if ... Read More

Positioning inside a File in Perl

Mohd Mohtashim

Mohd Mohtashim

Updated on 29-Nov-2019 10:24:09

398 Views

You can use to tell function in Perl to know the current position of a file and seek function to point a particular position inside the file.Perl tell FunctionThe first requirement is to find your position within a file, which you do using the tell function −tell FILEHANDLE tellThis returns the position ... Read More

Copy, Rename and Delete Files in Perl

Mohd Mohtashim

Mohd Mohtashim

Updated on 29-Nov-2019 10:22:33

2K+ Views

Here is the Perl example, which opens an existing file file1.txt and read it line by line and generate another copy file file2.txt.#!/usr/bin/perl # Open file to read open(DATA1, "file2.txt"); # Copy data from one file to another. while() {    print DATA2 $_; } close( DATA1 ); close( DATA2 ... Read More

Reading and Writing Files in Perl

Mohd Mohtashim

Mohd Mohtashim

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

1K+ 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. ... Read More

Opening and Closing Files in Perl

Mohd Mohtashim

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 ... Read More

Define a Report Footer in Perl

Mohd Mohtashim

Mohd Mohtashim

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

210 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 ... Read More

Define a Pagination in Perl

Mohd Mohtashim

Mohd Mohtashim

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

245 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 @< $% ... Read More

Advertisements