Why are there separate comparison operators for strings in Perl?

At work, I had to deal with programming in Perl, and I noticed such an illogical thing:

[do somesthing] unless $a < $b;

It will not work if $a and $b are strings, you need to write lt, gt, and so on for them. And what is the point of using the $ % @ signs, if you still need to know their type more accurately to work with variables correctly? Or is this another special exception so that some programmers work better, and the rest complain about the illogic?

 2
Author: saigono, 2011-02-02

7 answers

  1. Do not confuse warm with soft. The $ % @ signs do not indicate the type, but the structure of the value - a scalar, a hash, an array. This is not related to the string/number typing in any way.
  2. The whole question is: which is more, 100 or 20? Who told you they were numbers? Defining the type of values is actually less convenient than different operations. On the one hand, it is much more time-consuming to determine the type for each variable, and on the other hand, very often there are tasks to compare two variables with the string type as numbers or two variables of the number type as strings. Look at any program in C, pcp, or other languages, where you will very often see the construction toInt (a)
  3. In fact, this approach is really convenient and very logical. You just need to understand it and accept it. Here we wrote about the historical reasons for the appearance of different operations and about the fact that it is now too late to change Perl. In fact, none at all there are no historical reasons. Different operations are introduced meaningfully precisely because they are actually different operations, and of course no one is going to change this. If we compare with the same Bash, then in Perl the value of the operations lt,gt and is intentionally different than in Bash (where lt, gt are numeric,and are string).
  4. ATTENTION! The gt operation does not make a lexicographic comparison. This is the operation of the string comparison. Lexicographic comparison operation is good sort the following lines: 'picture 100', 'picture 20' (twenty will be less than a hundred). Didn't pay attention to sorting file names in Windows? So, the gt operation does not know how to do this.
 4
Author: reshu, 2011-08-23 05:47:05

I will immediately make a reservation that I have never written anything useful in Pearl in my life (although, in principle, I am familiar with the language, especially in connection with regexps).

I do not know if there is a actually typing or not, it's pretty obvious that these operators just do different things:

  • > - numerical comparison
  • gt-lexicographic comparison

Here is an example:

  DB<16> print('11' < '2')

  DB<17> print('11' > '2') # число 11 больше числа 2 (хотя тип — строка)
1
  DB<19> print('11' gt '2')

  DB<18> print('11' lt '2') # строка '11' лексикографически меньше строки '2'
1
 3
Author: kirelagin, 2011-02-02 09:35:22

I think this is done to avoid confusion. are operators for working with numeric data, and strings can't be compared as if they were numbers.

 0
Author: cy6erGn0m, 2011-02-02 08:00:55

It is also not logical to compare rows by more or less =)

 0
Author: psyhitus, 2011-02-02 08:18:31
  1. (lt, gt) vs ( ) - in perlop (perldoc perlop or any other reference system - perlop section) it is clearly written: Binary "

  2. $, @, % - this is already data aggregation: scalar, list (operations on one-dimensional arrays, pop/push-shift/unshift, etc.) and hash arrays (in other map languages, reference books, etc.). They have no relation to the number/string value type. Moreover, the typing is weak and dynamic, and you can write $a = "001"; $b = $a +1; .

It probably happened historically, something inherited from the shell, something from C, something (for example, closures) even from lisp. And the compote turned out to be very original. But perl has also had an impact on other scripting languages.

 0
Author: alexlz, 2011-02-02 09:48:31

And in the case of comparing a number with a string - what does it lead to? And if we have numbers in a large bundle of string data sorted?

Here it is already necessary to either always explicitly give the types, or do as in pearl or bash.

 0
Author: dallaylaen, 2011-02-04 15:38:38

The point is that there is no strict typing. If it were, it would be possible to do with single characters for comparing strings and numbers. And because perl never knows whether a string or a number is in front of it, you need to use different operators to compare strings and numbers.

 0
Author: odmink0, 2011-09-13 16:49:41