Questions tagged [undef]

EXAMPLES:

undef $foo;
my $bar = undef;

SEE ALSO:
perldoc -f undef

57 questions
39
votes
3 answers

When do we need #ifdef before #undef?

In many of the C and C++ files I have seen macros like this: #ifdef X #undef X // no statements in between #endif I feel that, it's adequate to simply write: #undef X If the macro X wasn't defined, then the #undef should have no effect. Is it ok…
iammilind
  • 68,093
  • 33
  • 169
  • 336
20
votes
1 answer

Why does Perl's string multiplication not warn on undef?

Under use strict; use warnings; my $foo = undef; the expressions $foo . '' and "$foo" both produce Use of uninitialized value $foo in ... at ... but the following expression gives the empty string without warning: $foo x 1 Anyone knows why? I…
Stefan Majewsky
  • 5,427
  • 2
  • 28
  • 51
19
votes
1 answer

How do I remove all undefs from array?

While reading from a configuration file in Perl there might be cases when a line is invalid and it does not need to get added to my array of valid lines. Since I'm using a for loop here, even the invalid lines create an undef entry. How can I remove…
flohei
  • 5,248
  • 10
  • 36
  • 61
16
votes
1 answer

How do I remove a Tcl procedure?

How do I remove a tcl procedure? One can unset a variable, override an alias with interp alias {} myproc {} otherproc, override a proc with one defined inside another namespace with namespace import -force. But I did not find a way to make a…
cfi
  • 10,915
  • 8
  • 57
  • 103
15
votes
6 answers

How can you get Perl to stop when referencing an undef value?

How do you get Perl to stop and give a stack trace when you reference an undef value, rather than merely warning? It seems that use strict; isn't sufficient for this purpose.
Neil
  • 24,551
  • 15
  • 60
  • 81
9
votes
1 answer

Why does opening an undef not fail?

This code dies as I expect it to: use strict; use warnings; open my $fh, "<", "" or die $!; But this does not: use strict; use warnings; open my $fh, "<", undef or die $!; What is going on here?
Chas. Owens
  • 64,182
  • 22
  • 135
  • 226
9
votes
2 answers

What values should a boolean function in Perl return?

SHORT QUESTION What are the best ways to represent true and false consistently in libraries of Perl code? 1 / 0? 1 / the special empty string that Perl's native boolean operators return? undef? () (i.e. the empty list)? QUESTION BACKGROUND We all…
Krazy Glew
  • 7,210
  • 2
  • 49
  • 62
8
votes
5 answers

In Perl, is there graceful way to convert undef to 0 manually?

I have a fragment in this form: my $a = $some_href->{$code}{'A'}; # a number or undef my $b = $some_href->{$code}{'B'}; # a number or undef $a = 0 unless defined($a); $b = 0 unless defined($b); my $total = $a + $b; The reality is even more messy,…
Anon Gordon
  • 2,469
  • 4
  • 28
  • 34
7
votes
3 answers

Perl: mapping to lists' first element

Task: to build hash using map, where keys are the elements of the given array @a, and values are the first elements of the list returned by some function f($element_of_a): my @a = (1, 2, 3); my %h = map {$_ => (f($_))[0]} @a; All the okay until f()…
indexless
  • 389
  • 3
  • 11
7
votes
9 answers

Check for value definedness in C++

I'm working in C++ and I need to know if a scalar value (for instance a double) is "defined" or not. I also need to be able to "undef" it if needed: class Foo { public: double get_bar(); private: double bar; void calculate_bar() { …
tunnuz
  • 23,338
  • 31
  • 90
  • 128
7
votes
1 answer

The poison value and undefined value in LLVM

LLVM introduces the concept of "poison value", which I never feel sure to understand. For example, for the statement %add = add nsw i32 %x, 1 If %x+1 is strictly larger than the largest i32 integer, an arbitrary value is to be assigned to %add.…
zell
  • 9,830
  • 10
  • 62
  • 115
6
votes
3 answers

Warnings in Perl Eval

I need to hide warnings within eval but the rest of the code should continue to throw warning messages. Here is what I have - eval "\$value = $hash->{key}"; now value of $hash->{key} could be a function call, like: $hash->{key} =…
user589672
  • 61
  • 1
  • 2
6
votes
1 answer

Perl `defined' and `undef' subroutine scope

Please take a look at the following code: use strict; use warnings; print "subroutine is defined\n" if defined &myf; myf(); sub myf { print "called myf\n"; } undef &myf; #myf(); print "now subroutine is defined\n" if defined &myf; The…
password636
  • 981
  • 1
  • 7
  • 18
5
votes
1 answer

Some string values ("0", "", and " ") are defined, but (==undef)

In my perl script, I used if ($a == undef) condition, I thought it is the same as if (not defined $a), where $a is a string as read from a csv file. However, I noticed that several string values ("", " ", "0", "-0", "+0", "@", "a", and many other…
Douglas
  • 71
  • 1
5
votes
3 answers

scope of #undef C++

I have a question about using #undef to redefine macros. I have a file global.h which contains a number of #define-d macros. In the code that uses these macros, I find that the values that the macros hold are not generic enough. I want to redefine…
Sriram
  • 10,298
  • 21
  • 83
  • 136
1
2 3 4