Y is not defined and but it equates to a null string. How does the Perl concepts compare to a language like C

The closest thing in C to an undefined value is when a variable is not initialised. For example:
int x; printf("%d",x);
will give you whatever junk is in memory, it is unlikely that the value will be zero. In Perl we don't have that problem, every scalar has the value undef until you assign it otherwise. This can be flagged as an error or warning should you want it (that's one reason for use warnings and use strict).

Worse, an unitialised string in C can cause your program to crash:
char name[42]; printf("%s",name);
could cause printf to run into an address which is illegal and crash the program. It might not - it might hit a zero delimiter by coincidence, but then you get junk displayed. No such problems in Perl.

A "null string" is, I assume, an empty string, 0x00 in C. One difference with C is that even a null string will require an array of at least one byte. char *pName = NULL; is not an empty string in C, it is a pointer with a value of zero. That might be semantics, but in C (not in C++) NULL is a pointer value.

Take a look at a Perl scalar string variable using Devel::Peek::Dump. Dump the variable as you change its size, and look at the amount of memory used (LEN). Now undef the value and see the difference:
use strict; use warnings; use Devel::Peek; my $str = ""; Dump($str); $str = "Fred Bloggs"; Dump($str); $str = "This is a much, much longer string"; Dump($str); $str = ""; Dump($str); undef $str; Dump($str);
See the difference between setting a string to be empty and using undef. Would it have made a difference if you assigned undef to the string instead of using undef as a function?

In reply to Re: defined vs null string by cdarke
in thread defined vs null string by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.