Mohd Mohtashim

Mohd Mohtashim

185 Articles Published

Articles by Mohd Mohtashim

Page 16 of 19

The carp Function in Perl

Mohd Mohtashim
Mohd Mohtashim
Updated on 29-Nov-2019 1K+ Views

The carp function in Perl is the basic equivalent of warn and prints the message to STDERR without actually exiting the script and printing the script name.package T; require Exporter; @ISA = qw/Exporter/; @EXPORT = qw/function/; use Carp; sub function {    carp "Error in module!"; } 1;When called from a script like below −use T; function();It will produce the following result −Error in module! at test.pl line 4

Read More

Errors within Perl Modules

Mohd Mohtashim
Mohd Mohtashim
Updated on 29-Nov-2019 260 Views

There are two different situations we should be able to handle while using Perl Programming −Reporting an error in a module that quotes the module's filename and line number - this is useful when debugging a module, or when you specifically want to raise a module-related, rather than script-related, error.Reporting an error within a module that quotes the caller's information so that you can debug the line within the script that caused the error. Errors raised in this fashion are useful to the end-user, because they highlight the error in relation to the calling script's origination line.The warn and die ...

Read More

The unless & die Function in Perl

Mohd Mohtashim
Mohd Mohtashim
Updated on 29-Nov-2019 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 an error or alternative only if the expression fails. The statement also makes sense when used in a single-line statement −die "Error: Can't change directory!: $!" unless(chdir("/etc"));Here we die only if the chdir operation fails.The die FunctionThe die function works just like warn, except that it also calls exit. Within ...

Read More

Create, Delete and Change Directories in Perl

Mohd Mohtashim
Mohd Mohtashim
Updated on 29-Nov-2019 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 can use rmdir function in Perl to remove a directory. You will need to have the required permission to remove a directory. Additionally this directory should be empty before you try to remove it.#!/usr/bin/perl $dir = "/tmp/perl"; # This removes perl directory from /tmp directory. rmdir( $dir ) or die "Couldn't ...

Read More

Display all the Files in Perl

Mohd Mohtashim
Mohd Mohtashim
Updated on 29-Nov-2019 848 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( $dir ); foreach (@files ) {    print $_ . ""; } # Display all the C source files in /tmp directory. $dir = "/tmp/*.c"; @files = glob( $dir ); foreach (@files ) {    print $_ . ""; } # Display all the hidden files. $dir = "/tmp/.*"; @files ...

Read More

Getting File Information in Perl

Mohd Mohtashim
Mohd Mohtashim
Updated on 29-Nov-2019 674 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 (-e $file) {    push @description, 'binary' if (-B _);    push @description, 'a socket' if (-S _);    push @description, 'a text file' if (-T _);    push @description, 'a block special file' if (-b _);    push @description, 'a character special file' if (-c _);    push @description, ...

Read More

Positioning inside a File in Perl

Mohd Mohtashim
Mohd Mohtashim
Updated on 29-Nov-2019 404 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 of the file pointer, in bytes, within FILEHANDLE if specified, or the current default selected filehandle if none is specified.Perl seek FunctionThe seek function positions the file pointer to the specified number of bytes within a file −seek FILEHANDLE, POSITION, WHENCEThe function uses the fseek system function, and you have ...

Read More

Copy, Rename and Delete Files in Perl

Mohd Mohtashim
Mohd Mohtashim
Updated on 29-Nov-2019 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 );Renaming a fileHere is the Perl example, which shows how we can rename a file file1.txt to file2.txt. Assuming file is available in /usr/test directory.#!/usr/bin/perl rename ("/usr/test/file1.txt", "/usr/test/file2.txt" );This function renames takes two arguments and it just renames the existing file.Deleting an Existing FileHere is an example, which shows how ...

Read More

Reading and Writing Files in Perl

Mohd Mohtashim
Mohd Mohtashim
Updated on 29-Nov-2019 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. 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
Mohd Mohtashim
Updated on 29-Nov-2019 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
Showing 151–160 of 185 articles
Advertisements