Found 157 Articles for PERL

The Match Operator in Perl

Mohd Mohtashim
Updated on 29-Nov-2019 11:57:31

1K+ Views

The match operator m// in Perl, is used to match a string or statement to a regular expression. For example, to match the character sequence "foo" against the scalar $bar, you might use a statement like this −Example Live Demo#!/usr/bin/perl $bar = "This is foo and again foo"; if ($bar =~ /foo/) {    print "First time is matching";    } else {    print "First time is not matching"; } $bar = "foo"; if ($bar =~ /foo/) {    print "Second time is matching";    } else {    print "Second time is not matching"; }When above program is executed, ... Read More

Filehandle Special Variables in Perl

Mohd Mohtashim
Updated on 29-Nov-2019 11:55:01

164 Views

There are various File Handle related Special Variables in Perl. We have listed them in different below in tabular form:$|If set to nonzero, forces an fflush(3) after every write or print on the currently selected output channel.$OUTPUT_AUTOFLUSH$%The current page number of the currently selected output channel.$FORMAT_PAGE_NUMBER$=The current page length (printable lines) of the currently selected output channel. Default is 60.$FORMAT_LINES_PER_PAGE$-The number of lines left on the page of the currently selected output channe$FORMAT_LINES_LEFT$~The name of the current report format for the currently selected output channel. Default is the name of the filehandle.$FORMAT_NAME$^The name of the current top-of-page format for the ... Read More

Regular Expression Special Variables in Perl

Mohd Mohtashim
Updated on 29-Nov-2019 11:52:35

500 Views

There are various Regular Expression Special Variables in Perl. We have listed them in different below in tabular form −$digitContains the text matched by the corresponding set of parentheses in the last pattern matched. For example, $1 matches whatever was contained in the first set of parentheses in the previous regular expression.$&The string matched by the last successful pattern match.$MATCH$`The string preceding whatever was matched by the last successful pattern match.$PREMATCH$'The string following whatever was matched by the last successful pattern match.$POSTMATCH$+The last bracket matched by the last search pattern. This is useful if you don't know which of a ... Read More

Global Special Variable Types in Perl

Mohd Mohtashim
Updated on 29-Nov-2019 11:51:07

282 Views

There are various global special variables in Perl. We have listed them in different blocks based on their usage and nature −Global Array Special Variables@ARGVThe array containing the command-line arguments intended for the script.@INCThe array containing the list of places to look for Perl scripts to be evaluated by the do, require, or use constructs.@FThe array into which the input lines are split when the -a command-line switch is given.Global Hash Special Variables%INCThe hash containing entries for the filename of each file that has been included via do or require.%ENVThe hash containing your current environment.%SIGThe hash used to set signal ... Read More

The confess Function in Perl

Mohd Mohtashim
Updated on 29-Nov-2019 11:49:28

985 Views

The confess function in Perl is like cluck; it calls die and then prints a stack trace all the way up to the origination script.package T; require Exporter; @ISA = qw/Exporter/; @EXPORT = qw/function/; use Carp; sub function {    confess "Error in module!"; } 1;When called from a script like below −use T; function();It will produce the following result −Error in module! at T.pm line 9    T::function() called at test.pl line 4

The croak Function in Perl

Mohd Mohtashim
Updated on 29-Nov-2019 11:47:44

2K+ Views

The croak function in Perl is equivalent to die, except that it reports the caller one level up. Like die, this function also exits the script after reporting the error to STDERR −package T; require Exporter; @ISA = qw/Exporter/; @EXPORT = qw/function/; use Carp; sub function {    croak "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 4As with carp, the same basic rules apply regarding the including of line and file information according to the warn and die functions.

The cluck Function in Perl

Mohd Mohtashim
Updated on 29-Nov-2019 11:46:36

642 Views

The cluck function in Perl is a sort of supercharged carp, it follows the same basic principle but also prints a stack trace of all the modules that led to the function being called, including the information on the original script.package T; require Exporter; @ISA = qw/Exporter/; @EXPORT = qw/function/; use Carp qw(cluck); sub function {    cluck "Error in module!"; } 1;When called from a script like below −use T; function();It will produce the following result −Error in module! at T.pm line 9    T::function() called at test.pl line 4

The carp Function in Perl

Mohd Mohtashim
Updated on 29-Nov-2019 11:44:40

742 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

Errors within Perl Modules

Mohd Mohtashim
Updated on 29-Nov-2019 11:42:46

114 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
Updated on 29-Nov-2019 11:41:22

674 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

Previous 1 ... 4 5 6 7 8 ... 16 Next
Advertisements