in reply to Re^2: unix to perl questions
in thread unix to perl questions

defined does pretty much exactly what the English word suggests: It tests whether the variable has a defined value, which is to say, whether any value has ever been assigned to the variable.

In this particular case, if(defined getpwnam($nUsername)) checks to see whether getpwnam returns a value at all:
- If it finds the user, it will return something and "something" is always defined, so the test evaluates as true.
- If it doesn't find the user, it will return nothing (not just an empty value or a false value, but the absence of a value - in Perl, this not-a-value is called undef, for "undefined"), and the test evaluates as false.

Replies are listed 'Best First'.
Re^4: unix to perl questions
by Anonymous Monk on Mar 12, 2015 at 16:41 UTC
    Just to add to that: The case where the function returns a "defined but false" value is the user "root", who has a UID of 0.
Re^4: unix to perl questions
by deyaneria (Acolyte) on Mar 12, 2015 at 16:56 UTC
    Is "undefined" similar to null in Perl? I have yet to see null in Perl.
      Yes. See undef.
      لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ