To understand the difference, you must understand the distinction between an empty string and being undefined. An empty string, is a string defined to be empty. It is defined. Creating a scalar variable without assigning it anything or assigning it the special symbol undef are two ways of saying "I haven't chosen a value yet. Act accordingly." Thus:

The difference between $a and $a=undef depends on whether or not $a has already been declared. my $a; and my $a=undef; two ways of writing the same thing. However, if you've already declared $a somewhere earlier in the script, $a; is an expression that evaluates to the value of $a. So my $a=1; $a; returns true. On the other hand, $a=undef; clears away whatever definition you gave to $a and makes it undefined again, so my $a=1; $a=undef; returns false.

There is no difference between $a=''; and $a="". Both assign $a an empty string. However, there is quite a difference if you put something between '...' and "...". For non-empty strings, single quotes are "non-interpolating" and double quotes are "interpolating". That means that between single quotes what-you-see-is-what-you-get: '$a' is just the two characters $ and a. But with double ("interpolating") quotes, Perl evaluates any variables found in between "..." so my $a=1; my $x="$a"; sets $x to "1" since that is the value of $a. For more information, see perlop.

Best, beth


In reply to Re: difference b/w undef and empty string by ELISHEVA
in thread difference b/w undef and empty string by saranperl

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.