Perl encapsulates a single variable in double quotes

In Perl, is there any reason for encapsulating a single variable in double quotes (without concatenation)?

I often find this in the source of the program I'm working on (written 10 years ago by people who no longer work here):

my $sql_host = "something"; my $sql_user = "somethingelse"; # a few lines down my $db = sub_for_sql_conection("$sql_host", "$sql_user", "$sql_pass", "$sql_db"); 

As far as I know, there is no reason for this. When I work in an old script, I usually remove quotes, so the editor changes them as variables, not like strings.

I think they saw it somewhere and copied the style, not understanding why this is so. Did I miss something?

Thanks.

+7
source share
5 answers

All this clearly points to variables. In 99.9% of cases, this is a mistake for beginners.

There are things that can happen as a side effect of this calling style:

 my $foo = "1234"; sub bar { $_[0] =~ s/2/two/ } print "Foo is $foo\n"; bar( "$foo" ); print "Foo is $foo\n"; bar( $foo ); print "Foo is $foo\n"; 

Here, the gating created a copy and passed it to the subroutine, bypassing Perl's passage through referential semantics. These are generally considered bad manners for invoking call variables, so you're probably all right.

Here you can also strengthen an object or other meaning. For example, undef builds an empty string. Objects can specify arbitrary code to run during streaming. It is possible to have two-digit scalars that have different numeric and string values. This is a way to indicate that you need a string form.

There is also one stupid thing that can go on. If you are working with an XS code that looks at the flags specified for scalar function arguments, then a strict scalar is a direct way to tell perl: “Make me a good clean new string value” with strict flags only and no numerical flags.

I am sure there are other odd exceptions to the rule of 99.9%. This is a few. Before you remove the quotes, take a second to check this strange shit. If you really use legitimate use, add a comment that identifies the quotation marks as a workable kludge and gives a reason to exist.

+7
source

In this case, double quotes are not needed. Moreover, using them is inefficient, as this leads to copying the source lines.

However, sometimes you can use this style to “reinforce” an object. For example, a URI provides support for:

 my $uri = URI->new("http://www.perl.com"); my $str = "$uri"; 
+6
source

I don’t know why, but this is a template commonly used by newcomers to Perl. This is usually waste (as in the passage you published), but I can think of two goals.


This creates a new line with the same value as the original, and this can be useful in very rare cases.

In the following example, an explicit copy is executed to protect $ x from being modified by the subuser because sub is changing its argument.

 $ perl -E' sub f { $_[0] =~ tr/a/A/; say $_[0]; } my $x = "abc"; f($x); say $x; ' Abc Abc $ perl -E' sub f { $_[0] =~ tr/a/A/; say $_[0]; } my $x = "abc"; f("$x"); say $x; ' Abc abc 

As a result of creating a copy of the string, it builds objects. This can be useful when working with code that changes its behavior based on whether its argument is a link or not.

In the following example, an explicit line is executed because require handles links in @INC differently than lines.

 $ perl -MPath::Class=file -E' BEGIN { $lib = file($0)->dir; } use lib $lib; use DBI; say "ok"; ' Can't locate object method "INC" via package "Path::Class::Dir" at -e line 4. BEGIN failed--compilation aborted at -e line 4. $ perl -MPath::Class=file -E' BEGIN { $lib = file($0)->dir; } use lib "$lib"; use DBI; say "ok"; ' ok 
+6
source

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

However, quoting a variable may someday be necessary: ​​this explicitly causes the structure of the variable's value. Stringification may give a different result for some values ​​if the thoses values ​​are double vars or if they are blessed values ​​with overload ed stringification.

Here is an example with double vars:

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

Output:

 1 2 
+2
source

My theory has always been that people switch from other languages ​​with bad habits. This does not mean that they think: "I will use double quotes all the time," but they just do not think!

I will be honest and say that I fell into this trap because I came to Perl with Java, so the muscle memory was there and just kept shooting.

PerlCritic has finally taken me out of my habit!

This definitely makes your code more efficient, but if you don’t think about whether you want your lines to be interpolated, you are most likely making stupid mistakes, so I will go ahead and say that it is dangerous.

0
source

All Articles