On 07/04/2018 10:16 AM, Rick T wrote:
The following line works, even though I forgot to double quote the
variable.
my $student_directory = '/data/students/' . $student_id;
When I noticed this, I thought this was convenient: perl is trying to
“do the right thing.” But I worry that leaving them out may be bad
coding practice; if so, or you see other worthwhile improvements,
please let me know.
you don't need quotes on single scalar variables. in fact it is bad
practice and can lead to bugs when you do that. what you did is not
interpolation but ordinary concatenation with the . operator.
interpolation is replacing scalars (and array) variables with their
values inside double quotes.
More importantly, I wonder how perl knows to do this. Perhaps context
provided by the assignment operator or the concatenation operator? If
there is a general rule on this, it would help me to know more widely
when I can omit double quotes.
the dot operator provides string context to both its arguments. if you
are always double quoting single scalars, you should stop it as it is
also slower due to the extra copying of the string value.
that line is better written as:
my $student_directory = "/data/students/$student_id";
i avoid using . when i can. it just reads better when you have the whole
string in double quotes and you skip all the extra quote chars and .
operators.
uri