in reply to defined vs null string

You asked about how it compares with C, so here's a C version:

#include <stdio.h> #include <string.h> char *undef; char *def_empty = ""; char *def = "foo bar"; void check(char *t) { if (t) { if (strlen(t)) { printf("Defined loc=%p, val='%s'\n", t, t); } else { printf("Empty string: loc=%p\n", t); } } else { printf("Undefined (null ptr)\n"); } } int main(int, char **) { check(undef); check(def_empty); check(def); return 0; }

It gives us:

$ ./a.exe Undefined (null ptr) Empty string: loc=0x4020e5 Defined loc=0x4020e6, val='foo bar'

...roboticus

When your only tool is a hammer, all problems look like your thumb.

Replies are listed 'Best First'.
Re^2: defined vs null string
by ikegami (Patriarch) on Aug 30, 2011 at 18:54 UTC

    That would be closer to

    sub SvPVX { unpack 'J', pack 'p', $_[0] } if (defined $y) { if (length($y)) { printf("Defined loc=%X, val='%s'\n", SvPVX($y), $y); } else { printf("Empty string: loc=%X\n", SvPVX($y)); } } else { printf("Undefined (null ptr)\n"); }

    So you completely removed the case the OP was asking about.

    The C equivalent would be closer to the following (memory leak aside):

    Scalar* stringify(Scalar* sv) { if (scalar_type(sv) == undefined) { fprintf(STDERR, "Use of unitialised value\n"); return NewEmptyStringScalar(); } else if (scalar_type(sv) == string) { return sv; } else if (scalar_type(sv) == ...) { ... } else ... } int eq(Scalar* sv1, Scalar* sv2) { return strcmp( stringify(sv1)->string_buffer, stringify(sv2)->string_buffer ) == 0; }