Why is the true sdf value defined in this Perl example?

I tried this example in Perl. Can someone explain why this is true?

  if (defined sdf)  {   print "true";  }

He is typing true.

sdf can be any name.

Also, if the sdf function is defined and it returns 0, then it does not print anything.

print (sdf); does not print the sdf line, but

if (sdf eq "sdf")
{
 print "true";
}

outputs true.

A related question remains if sdf is a string. What does not print?

+3
source share
5 answers

sdfis bareword .

perl -Mstrict -e "print qq{defined\n} if defined sdf"
Bareword "sdf" not allowed while "strict subs" in use at -e line 1.
Execution of -e aborted due to compilation errors.

For more fun, try

perl -Mstrict -e "print sdf => qq{\n}"

. :

" " . Perl ( , , , ), , :

@daynames = (sun, mon, tue, wed, thu, fri, sat);

, :

@monthnames = (jan, feb, mar, apr, may, jun,
               jul, aug, sep, oct, nov, dec);

? , 10- 'oct', oct(), $_, .

: ( @ysth)

E:\Home> perl -we "print sdf"
Unquoted string "sdf" may clash with future reserved word at -e line 1.
Name "main::sdf" used only once: possible typo at -e line 1.
print() on unopened filehandle sdf at -e line 1.

print , . , print $_ filehandle sdf. sdf , . , . :

E:\Home> perl -MO=Deparse -e "print sdf"
print sdf $_;

. :

E:\Home> perl -e "print asdfg, sadjkfsh"
No comma allowed after filehandle at -e line 1.
E:\Home> perl -e "print asdfg => sadjkfsh"
asdfgsadjkfsh

, => LHS, "word", .

, . use strict .

+10

bareword". , "sdf" undefined.

+5

:

telemachus ~ $ perl -e 'if (defined sdf) { print "True\n" };'
True
telemachus ~ $ perl -e 'if (defined abc) { print "True\n" };'
True
telemachus ~ $ perl -e 'if (defined ccc) { print "True\n" };'
True
telemachus ~ $ perl -e 'if (defined 8) { print "True\n" };'
True

undef, defined .

, Perl: ?

+4

defined true, , undefined.

+3

a certain function returns true if the value passed in the argument is undefined. This is useful for extracting a variable containing 0 or "" from a variable that just winked.

0
source

Source: https://habr.com/ru/post/1721445/


All Articles