perl encapsulate single variable in double quotes

后端 未结 5 1623
南方客
南方客 2021-01-12 11:06

In Perl, is there any reason to encapsulate a single variable in double quotes (no concatenation) ?

I often find this in the source of the program I am working on (w

5条回答
  •  我在风中等你
    2021-01-12 12:06

    In your case quotes are completely useless. We can even says that it is wrong because this is not idiomatic, as others wrote.

    However quoting a variable may sometime be necessary: this explicitely triggers stringification of the value of the variable. Stringification may give a different result for some values if thoses values are dual vars or if they are blessed values with overloaded stringification.

    Here is an example with dual vars:

    use 5.010;
    use strict;
    use Scalar::Util 'dualvar';
    
    my $x = dualvar 1, "2";
    say 0+$x;
    say 0+"$x";
    

    Output:

    1
    2
    

提交回复
热议问题