Perl Language
Vero e falso
Ricerca…
Sintassi
- undef # False
- '' # Definito, Falso
- 0 # definito, ha lunghezza, falso
- '0' # Definito, Ha Lunghezza, Falso
Osservazioni
Perl non ha un tipo di dati booleani, né ha parole chiave true e false come molte altre lingue. Tuttavia, ogni valore scalare verrà valutato come vero o falso quando valutato in un contesto booleano (la condizione in un'istruzione if o in un ciclo while, ad esempio).
I seguenti valori sono considerati falsi:
-
'', la stringa vuota. Questo è ciò che restituiscono gli operatori di confronto integrati (ad es.0 == 1) -
0, il numero 0, anche se lo scrivi come 000 o 0.0 -
'0', la stringa che contiene una singola cifra 0 -
undef, il valore non definito - Oggetti che utilizzano l' overloading per numerare / stringificare in valori falsi, come
JSON::false
Tutti gli altri valori sono veri:
- qualsiasi numero diverso da zero come
1,3.14,'NaN'o'Inf' - qualsiasi stringa numericamente 0 ma non letteralmente la stringa
'0', come'00','0e0',"0\n"e"abc".
Se si restituisce intenzionalmente un valore 0 numericamente vero, si preferisca'0E0'(usato da moduli ben noti) o'0 but true'(usato dalle funzioni Perl) - qualsiasi altra stringa che non sia vuota, come
' ','false' - tutti i riferimenti, anche se fanno riferimento a valori falsi, come
\'',[]o{} - una matrice o un hash di valori falsi
I seguenti operatori sono comunemente trattati per restituire un valore booleano in un contesto scalare:
@arestituisce se l'array è vuoto o meno%hrestituisce se l'hash è vuoto o menogreprestituisce se sono stati trovati o meno elementi corrispondenti@a = LISTe(LIST) = LISTrestituiscono se la LISTA del lato destro ha prodotto o meno scalari
Elenco di valori veri e falsi
use feature qw( say );
# Numbers are true if they're not equal to 0.
say 0 ? 'true' : 'false'; # false
say 1 ? 'true' : 'false'; # true
say 2 ? 'true' : 'false'; # true
say -1 ? 'true' : 'false'; # true
say 1-1 ? 'true' : 'false'; # false
say 0e7 ? 'true' : 'false'; # false
say -0.00 ? 'true' : 'false'; # false
# Strings are true if they're not empty.
say 'a' ? 'true' : 'false'; # true
say 'false' ? 'true' : 'false'; # true
say '' ? 'true' : 'false'; # false
# Even if a string would be treated as 0 in numeric context, it's true if nonempty.
# The only exception is the string "0", which is false.
# To force numeric context add 0 to the string
say '0' ? 'true' : 'false'; # false
say '0.0' ? 'true' : 'false'; # true
say '0e0' ? 'true' : 'false'; # true
say '0 but true' ? 'true' : 'false'; # true
say '0 whargarbl' ? 'true' : 'false'; # true
say 0+'0 argarbl' ? 'true' : 'false'; # false
# Things that become numbers in scalar context are treated as numbers.
my @c = ();
my @d = (0);
say @c ? 'true' : 'false'; # false
say @d ? 'true' : 'false'; # true
# Anything undefined is false.
say undef ? 'true' : 'false'; # false
# References are always true, even if they point at something false
my @c = ();
my $d = 0;
say \@c ? 'true' : 'false'; # true
say \$d ? 'true' : 'false'; # true
say \0 ? 'true' : 'false'; # true
say \'' ? 'true' : 'false'; # true
Modified text is an extract of the original Stack Overflow Documentation
Autorizzato sotto CC BY-SA 3.0
Non affiliato con Stack Overflow