I tried this example in Perl. Can someone explain why is it true?
if (defined sdf) { print "true"; }
It prints true
.
sdf could be any name.
In addition, if there is sdf function defined and it returns 0, then it does not print anything.
print (sdf);
does not print sdf string but
if (sdf eq "sdf")
{
print "true";
}
prints true.
The related question remains if sdf is a string. What is it not printed by print?
sdf
is a bareword.For more fun, try
See Strictly speaking about use strict:
Corrected: (thanks @ysth)
If a bareword is supplied to
print
in the indirect object slot, it is taken as a filehandle to print to. Since no other arguments are supplied,print
defaults to printing$_
to filehandlesdf
. Sincesdf
has not been opened, it fails. If you run this without warnings, you do not see any output. Note also:as confirmation of this observation. Note also:
The latter prints both strings because
=>
automatically quotes strings on the LHS if they consist solely of 'word' characters, removing the filehandle interpretation of the first argument.All of these examples show that using barewords leads to many surprises. You should
use strict
to avoid such cases.