At work I had to face Perl programming, and I noticed this illogical thing:

[do somesthing] unless $a < $b; 

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

  • 10 > 2 , but "10" < "2" . - Smit Johnth

7 answers 7

  1. Do not confuse warm with soft. The $% @ signs denote not the type, but the structure of the value - scalar, hash, array. With string / number typing this is not related at all.
  2. The whole question is: what's more, 100 or 20? And who told you that these are numbers? Determining the type of values ​​is actually less convenient than different operations. On the one hand, determining the type for each variable is much more laborious, but 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, php, other languages, where you will often see the toInt (a) <toInt (b) construct. In Perl, this is much easier. You just have to accept the rules of the game.
  3. In fact, this approach is really convenient and very logical. You just need to understand it and accept. They wrote about the historical reasons for the appearance of various operations and about the fact that now it’s too late to change Perl. In fact, there is no historical reason. Different operations are introduced meaningfully precisely because these are actually different operations, and naturally no one is going to change this. If we compare it with the same Bash, then in Perl the meaning of the operations lt, gt and <,> is intentionally different than in Bash (there lt, gt are numeric, and <,> are string).
  4. ATTENTION! The gt operation does not make a lexicographical comparison. This is a string comparison operation. The lexicographic comparison operation will sort the following lines well: 'picture 100', 'picture 20' (twenty will be less than a hundred). Not paying attention to the sorting of file names in Windows? So, the operation gt does not know how.

    Immediately, I’ll make a reservation that I have never written anything meaningful in Perl (although, in principle, I am familiar with the language, especially in connection with regexps).

    I do not know whether typing is actually there or not, but it’s clear that these operators just do different things:

    • > - numerical comparison
    • gt - lexicographical 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 

      I think this is done so that there is no confusion. <and> are operators for working with numeric data, and strings cannot be compared as if they were some numbers.

      • Why then even say that there are no strings and numbers, but only scalars? Introduced more characters for them: type ^ string and #number - saigono
      • Well, you know that in fact it is not quite so, and that types actually do exist;) It’s not by chance that lines cannot be added with a plus. However, it is impossible to perform arithmetic operations with strings, unless these strings are numbers. - cy6erGn0m
      • Well, that's why I say that, it turns out, the use of $ as a scalar symbol does not make sense, but it is better to separate them (since the perlovschik likes to overload the code with obscure symbols) - saigono
      • one
        @saigono. dangerous things you say! They can give for execution too :) - cy6erGn0m
      • 2
        This is done for sh compatibility. Initially, Perl was written as a development for sh, where most of the commands similar to unix commands were built in, which greatly increased speed. Then everything developed, changed ... but legs grow from there. - avp

      Lines to compare for more or less is also not logical =)

      • one
        Have you ever heard of such a thing as a "lexicographical order"? - kirelagin
      • Have you ever used it for strings, not for characters?) - psyhitus
      • When sorting. - alexlz
      • Do you think it came up with a pure joke, without any practical application? - kirelagin
      • Nobody does anything for fun (= - psyhitus
      1. (lt, gt) vs (<,>) - in perlop (perldoc perlop or any other help system - the perlop section) is clearly written: Binary "<" returns true if the left argument is less than the right argument. Binary "lt" returns true if left is stringwise less than the right argument.

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

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

      • and what will happen if you execute $ a -> (1); if $ a is a string? And accordingly, what happens if you run substr $ a -4; if $ a is a function pointer? - saigono
      • Swearing will be bad English words. "And it is right." (c) M.S. Gorbachev - alexlz
      • Well, what for such aggregation? :) - saigono
      • And what result should produce substr from function? With this approach, brainfuck is better or something else from the same group. Perl is considered by many to be write-only. - alexlz

      And in the case of comparing a number with a string - what is the result? And if at us numbers as a part of the big pack of line data are sorted?

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

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

        • How is no typing? In Perl5, it is quite a real, quite normal dynamic typing. The same, by the way, as in PHP, Python or Ruby. - drdaeman
        • Ok, I did not quite put it correctly - I meant that there is no strict separation between the types "number" and "string", i.e. no strong typing. Do not find fault with the words;) - odmink0
        • This is the main problem: the pearl forces the programmer to follow the context of the use of the variable, instead of doing it himself. - saigono 2:01