Updated April 5, 2023
Introduction to Perl eq
The Perl eq is one of the operators which is used to compare the string values while the user gives the inputs to either run time or compile time. The values are stored in the separate variables; with the help of these variables, the values are compared using the loop or other variables which is to be displayed on the screen. When we compare the string values, it must be checked and validated in both the left and right set of parameters; it is also used in the == operator for comparing the reference, which is related to the specific variables.
Syntax
In Perl, eq is the operator that compares the values related to the user-specified variables. These variables will be used to store and retrieve the values and displayed them on the output console, like that “eq” operator, which satisfies and also used the other operators like “==” in the script, which is used to validate the result after comparison of the two string variables.
#! /usr/local/bin/perl
$var = "";
$var1 = "';
$result = $var eq $var1;
Conditional loop($result == values)
---some perl script logics which depends upon the user requirements----
The above code is the basic syntax for using the “eq” and “==” operators used in the script. Based on the user needs, it may vary upon the places for utilizing these conditional operators.
How does the eq operator work in Perl?
The eq operator is one of the most used in the script and other object-oriented languages. It is also the simplest and probably used with the other comparison operators to validate one value is equal to another value that value may be any data type like string, integer or number, etc. If the two set of variables values are equal, it will return the Boolean value called true else; if the two values are not equal, it returns the false as the value on the loop. I suppose we compare the two integer or number data type values using the comparison operator called “==” for string type of variable values using “eq” as the comparison operator for the loop conditional statements.
In Perl comparison, the string values are compared using some standard codes like ASCII format; if the input values are satisfied with the conditions, it will return the true value; else, if the user input values are not satisfied with the conditions, it will return the false as the value in the script. Each value has its own reference and address of the memory location in the variable; once we compared the variables, it also compares its memory reference if we use the ‘==’ operator in the script.
Examples of Perl eq
Here are the following examples mention below
Example #1
Code:
#!/usr/local/bin/perl
$vars = "Welcome To My Domain";
$vars1 = "Welcome";
$result = $vars eq $vars1;
if($result == 1)
{
print"Welcome User You have the same strings which you have to initialise the values in the variables \n \n ";
}
elsif($result == 2)
{
print "Thanks User You comparison of strings has partially correct \n \n";
}
else {
print "Your strings are not matched please try again \n \n";
}
Output:
In the above example, we used the “eq” operator and “==” to check the conditions in both the set of variables. It will compare the values of the two variables; it has its own storage location in the memory so that it will create its own reference of these variables. Once compared and removed any of the values, it will affect the memory of its reference variables. Not only used for ‘==’ operator in conditional loops, but we may also try some other operators like ‘>,<’ etc. these operators are used for comparing the two variable values after we compared the string data type variable values by using the ‘eq’ operator.
Example #2
Code:
#!/usr/local/bin/perl
$vars = "hsdv hdg jd hwej wjhed wjhe jwhe whewjhh wje wjhe wjeg wjeh wjeh wje wje i23e8 899w e wjhe wkj webj webj khwekj kbjwh hwej khwekjn wehj bwwje wjej hwek khe hej hwej";
$vars1 = "hsdv hdg jd hwej wjhed wjhe jwhe whewjhh wje wjhe wjeg wjeh wjeh wje wje i23e8 899w e wjhe wkj webj webj khwekj kbjwh hwej khwekjn wehj bwwje wjej hwek khe hej hwej";
print "Welcome To My Domain\n";
if( $vars == $vars1 ) {
print "Welcome Users your inputs are compared and please see the results:\n";
} else {
print "Thank you users have a nice day your input values are not same\n";
}
$results = $vars eq $vars1;
print "Users your ouput is: $results \n";
Output:
In the above second example, we used the same eq and == operators, which we have compared the two variable values. When we compare the string values, it also compares whether it’s case-sensitive or not; using some regular expressions will handle those things for comparison of the two values in the memory. Hereafter comparison of the two variable values with the help of == operator again we used eq operator for comparing and stored the results in the third variable, but it will return the Boolean type of results like 1 means it’s true if the result is false there is no value shown on the screen.
Example #3
Code:
use strict;
use warnings;
use 5.010;
print "Welcome To My DOmain Please enter your input: ";
my $vars = <STDIN>;
chomp $vars;
if ( $vars eq "sivaraman" ) {
say "Thanks User your Runtime input are same";
} else {
say "Please try again your Runtime input are not same";
}
Output:
In the final example, we used the eq operator for comparing the run-time user input values. And also, we have initialized the string value in the if condition, we will validate the value to the run-time inputs. If we use the ‘==’ operator, it returns no value because it will compare the memory reference by using the eq operator it compares the variable values.
Conclusion
In Perl script language, we used many comparison operators and logical operators to implement the application with some logic based on the user requirements. Like that, ‘eq’ is one of the comparison operators to compare the values, which depends upon the application logic and is based on the user requirements.
Recommended Articles
This is a guide to Perl eq. Here we discuss How does eq operator work in Perl and Examples along with the codes and outputs. You may also look at the following articles to learn more –