5 Einträge, 1 Seite |
QuoteNote: Many folks tend to overuse defined, and then are surprised to discover that the number 0 and "" (the zero-length string) are, in fact, defined values. For example, if you say
Code: (dl )"ab" =~ /a(.*)b/;
The pattern match succeeds, and $1 is defined, despite the fact that it matched "nothing". It didn't really fail to match anything. Rather, it matched something that happened to be zero characters long. This is all very above-board and honest. When a function returns an undefined value, it's an admission that it couldn't give you an honest answer. So you should use defined only when you're questioning the integrity of what you're trying to do. At other times, a simple comparison to 0 or "" is what you want.
defined $1 and $1 ne ""
$zeile =~ /^\s+([a-zA-Z0-9]{0,16})\s+([a-zA-Z0-9]{0,8})\s+([a-zA-Z0-9]{0,16})\s+([a-zA-Z0-9]{0,8})/;
1
2
3
4
5
6
7
8
9
10
### FALSCH
$zeile =~ /...(...).../;
if ($1 ne "") {
# hier steht evtl. etwas altes in $1
}
### RICHTIG
if ($zeile =~ /...(...).../) {
# jetzt kann man $1 etc. verwenden
}
5 Einträge, 1 Seite |