Server Side Programming Articles - Page 2065 of 2650

Grouping Matching in Perl

Mohd Mohtashim
Updated on 29-Nov-2019 12:07:15

2K+ Views

From a regular-expression point of view in Perl, there is no difference between the following two expressions except that the former is slightly clearer.$string =~ /(\S+)\s+(\S+)/; and $string =~ /\S+\s+\S+/;However, the benefit of grouping is that it allows us to extract a sequence from a regular expression. Groupings are returned as a list in the order in which they appear in the original. For example, in the following fragment we have pulled out the hours, minutes, and seconds from a string.my ($hours, $minutes, $seconds) = ($time =~ m/(\d+):(\d+):(\d+)/);As well as this direct method, matched groups are also available within the ... Read More

Matching Boundaries & Selecting Alternatives in Perl

Mohd Mohtashim
Updated on 29-Nov-2019 12:04:17

293 Views

Matching Boundaries in PerlThe \b matches at any word boundary in Perl, as defined by the difference between the \w class and the \W class. Because \w includes the characters for a word, and \W the opposite, this normally means the termination of a word. The \B assertion matches any position that is not a word boundary. For example −/\bcat\b/ # Matches 'the cat sat' but not 'cat on the mat' /\Bcat\B/ # Matches 'verification' but not 'the cat on the mat' /\bcat\B/ # Matches 'catatonic' but not 'polecat' /\Bcat\b/ # Matches 'polecat' but not 'catatonic'Selecting Alternatives in PerlThe | character is just like ... Read More

The Translation Operator in Perl

Mohd Mohtashim
Updated on 29-Nov-2019 12:03:02

795 Views

Translation is similar, but not identical, to the principles of substitution in Perl, but unlike substitution, translation (or transliteration) does not use regular expressions for its search on replacement values. The translation operators are −tr/SEARCHLIST/REPLACEMENTLIST/cds y/SEARCHLIST/REPLACEMENTLIST/cdsThe translation replaces all occurrences of the characters in SEARCHLIST with the corresponding characters in REPLACEMENTLIST. For example, using the "The cat sat on the mat." string we have been using in this chapter −Example Live Demo#/user/bin/perl $string = 'The cat sat on the mat'; $string =~ tr/a/o/; print "$string";When above program is executed, it produces the following result −The cot sot on the mot.Standard Perl ... Read More

The Substitution Operator in Perl

Mohd Mohtashim
Updated on 29-Nov-2019 12:00:43

3K+ Views

The substitution operator s/// in Perl is really just an extension of the match operator that allows you to replace the text matched with some new text. The basic form of the operator is −s/PATTERN/REPLACEMENT/;The PATTERN is the regular expression for the text that we are looking for. The REPLACEMENT is a specification for the text or regular expression that we want to use to replace the found text with. For example, we can replace all occurrences of dog with cat using the following regular expression −Example Live Demo#/user/bin/perl $string = "The cat sat on the mat"; $string =~ s/cat/dog/; print "$string";When the above program ... Read More

Matching Only Once in Perl

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

302 Views

There is a simpler version of the match operator in Perl - the ?PATTERN? operator. This is basically identical to the m// operator except that it only matches once within the string you are searching between each call to reset.For example, you can use this to get the first and last elements within a list −Example Live Demo#!/usr/bin/perl @list = qw/food foosball subeo footnote terfoot canic footbrdige/; foreach (@list) {    $first = $1 if /(foo.*?)/;    $last = $1 if /(foo.*)/; } print "First: $first, Last: $last";When the above program is executed, it produces the following result −First: foo, Last: footbrdige

The Match Operator in Perl

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

2K+ 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

262 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

780 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

419 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

2K+ 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

Advertisements