Perl- What function am I looking for? Assigning multiple rules to a specified outcome

Active3 hr before
Viewed126 times

7 Answers


Meta Stack Overflow ,Stack Overflow en español,Stack Overflow em Português, Stack Overflow Public questions & answers

This should help you. It simply tests each line for the failure condition. Everything else is a pass.

use strict;
use warnings 'all';

my $input = 1978;

while ( my $line = <DATA> ) {

    $line =~ s/\s*\z//;

    my $result = $line && $input && $line ne $input ? 'Fail' : 'Pass';

    printf "%4s %s\n", $line, $result;





1978 Pass
1989 Fail
1978 Pass
1999 Fail
1768 Fail
load more v

Let's have a look into the following example, which defines a simple function and then call it. Because Perl compiles your program before executing it, it doesn't matter where you declare your subroutine.,Let's try the following example, which takes a list of numbers and then prints their average −,Let's try the following example, which takes a list of numbers and then returns their average −,Let's check the following example to distinguish between global and local variables −

The general form of a subroutine definition in Perl programming language is as follows −

sub subroutine_name {
   body of the subroutine
load more v

This page describes the syntax of regular expressions in Perl.,If none of the above apply, for backwards compatibility reasons, the /d modifier is the one in effect by default. As this can lead to unexpected results, it is best to specify which other rule set should be used.,Flags described further in "Using regular expressions in Perl" in perlretut are:,The caret allows for simpler stringification of compiled regular expressions. These look like

There are a number of Unicode characters that match a sequence of multiple characters under /i. For example, LATIN SMALL LIGATURE FI should match the sequence fi. Perl is not currently able to do this when the multiple characters are in the pattern and are split between groupings, or when one or more are quantified. Thus

# Matches
   "\N{LATIN SMALL LIGATURE FI}" = ~/[fi][fi]/i;
# Doesn 't match!
# Doesn 't match!

# The below doesn 't match, and it isn'
t clear what $1 and $2 would
# be even
if it did!!
   "\N{LATIN SMALL LIGATURE FI}" = ~/(f)(i)/i;
# Doesn 't match!
load more v

Perl provides us with two major types of functions:,Perl | String functions (length, lc, uc, index, rindex),Built-in Functions: Perl provides us with a huge collection of built-in library functions. These functions are already coded and stored in the form of functions. To use those we just need to call them as per our requirement like sin(), cos(), chr(), return(), shift(), etc.,Perl | File Handling Introduction

Variables are user-defined words that are used to hold the values passed to the program which will be used to evaluate the Code. Every Perl program contains values on which the Code performs its operations. These values can’t be manipulated or stored without the use of a Variable. A value can be processed only if it is stored in a variable, by using the variable’s name.
A value is the data passed to the program to perform manipulation operation. This data can be either number, strings, characters, lists, etc.


   $a = 5;
$b = "geeks";
$c = 15;

Value 10 is an expression, $x + $y is an expression that returns their sum, etc.

A statement in Perl holds instructions for the compiler to perform operations. These statements perform the operations on the variables and values during the Run-time. every statement in Perl must end with a semicolon(;). Basically, instructions written in the source code for execution are called statements. There are different types of statements in the Perl programming language like Assignment statement, Conditional statement, Looping statements, etc. These all help the user to get the required output. For example, n = 50 is an assignment statement.
Multi-Line Statements: Statements in Perl can be extended to one or more lines by simply dividing it into parts. Unlike other languages like Python, Perl looks for a semicolon to end the statement. Every line between two semicolons is considered as a single statement.
When the programmer needs to do long calculations and cannot fit his statements into one line, one can easily divide it into multiple lines.

$x = $a + $b + $c +
   $d + $e + $f;

A block is a group of statements that are used to perform a relative operation. In Perl, multiple statements can be executed simultaneously (under a single condition or loop) by using curly-braces ({}). This forms a block of statements which gets executed simultaneously. This block can be used to make the program more optimized by organizing the statements in groups.
Variables that are declared inside a block have their scope limited to that specific block and will be of no use outside the block. They will get executed only till that specific block is getting executed.

   $x = 15;
   $x = $x + 25;

Whitespaces in Perl are the blanks that are used between the variables and operators or between keywords, etc. Perl has no effect of whitespaces unless they are used within quotes. Whitespaces such as spaces, tabs, newlines, etc. have the same meaning in Perl if used outside the quotes.
Example 1:

$a = $b + $c;
Here, spaces are of no use,
   it will cause no effect even
if it is written as
$a = $b + $c;

Example 2:

print "Geeks for Geeks";
will print
for geeks
print "Geeks       for
Geeks ";   
will print

Here, in the above examples, it is shown that whitespaces have their effect only if used within the quotes.
Similarly, the process of indentation is used to arrange the code in an organized way to make it easier for the readers. Whenever a block of statements is used then the indentation will help reducing the reading complexity of the code.

Using Indentation: {
   $a = $b + $c;
   print "$a";

Without using Indentation: {
   $a = $b + $c;
   print "$a";

Keywords or Reserved words are the words in a language that are used for some internal process or represent some predefined actions. They have a special meaning to the compiler. These words are therefore not allowed to use as variable names or objects. Doing this will result in a compile-time error. In Perl, keywords include built-in functions as well along with the control words.
These keywords can sometimes be used as a variable name but that will result in confusion and hence, debugging of such a program will be difficult.

One can use $print as a variable along with the keyword print().
load more v

The following example variable assignment statements should be inside an <EQN> or <eqn> tag.,where variable is the variable name and value is the value to be assigned. The value can be either a single value or a Perl expression that results in a value.,Some variable names are used by WebAssign. These variables are listed in the documentation.,Often, you only need to use a variable to store a single text or numeric value. Such variables are referred to as scalar variables in Perl.

$variable = value;
load more v

If you are familiar with other programming languages, then you would know that there are certain rules about naming variables. Similarly, Perl has three rules for naming scalars.,Operator used to check whether both variables are not equal, If you are interested in Perl Automation, then it would be advisable to learn about Storage & Networking Concepts.,Operator used to check whether both variables are equal


print "Hello, world!";
load more v

The Data key names tab is empty if no result data key names have been specified in the Source page of the Perl Script policy.,Rules define the action a policy should take in response to a specific type of incoming event. Each rule consists of the following:,In the Policy Rules section, click and select the type of rule to define what the policy should do in response to a specific string in the Perl script data. Each policy must have at least one rule. , In the Policy Rules section, click and select the type of rule to define what the policy should do in response to a specific string in the Perl script data. Each policy must have at least one rule.

This pattern is then compared using pattern matching rule against the event keys for all events in the OMi event database. Any key that you provide in the policy is treated as a simplified OM pattern in OMi. Therefore a plain string is treated as a substring and not as a complete match. The key in our example will match:

critical: TEST
critical: TEST1
critical: TEST2A
load more v

Other "multiple-function" queries related to "Perl- What function am I looking for? Assigning multiple rules to a specified outcome"