If $alice and $bob were not assigned values, they are undef. Unlike a language such as C where using a variable that has never been assigned can result in undefined behavior, in Perl the behavior is predictable, and documented. undef is treated like an out-of-band value; it's neither a string nor a number, it's just undef. The undef value has several features:

Here are some examples:

use strict; use warnings; use Devel::Peek; { my $foo; print '$foo is ', (defined($foo) ? 'defined' : 'undef'), "\n"; Dump($foo); } print "\n\n"; { my $foo; print '$foo eq $foo: $foo is stringwise ', ($foo eq $foo ? 'equal' + : 'unequal'), "\n"; Dump($foo); } print "\n\n"; { my $foo; print '$foo == $foo: $foo is numerically ', ($foo == $foo ? 'equal +' : 'unequal'), "\n"; Dump($foo); }

The output will look like this:

$foo is undef SV = NULL(0x0) at 0x1275f40 REFCNT = 1 FLAGS = () Use of uninitialized value $foo in string eq at mytest.pl line 17. Use of uninitialized value $foo in string eq at mytest.pl line 17. $foo eq $foo: $foo is stringwise equal SV = PV(0x1252e70) at 0x12a0470 REFCNT = 1 FLAGS = () PV = 0 Use of uninitialized value $foo in numeric eq (==) at mytest.pl line 2 +4. Use of uninitialized value $foo in numeric eq (==) at mytest.pl line 2 +4. $foo == $foo: $foo is numerically equal SV = PVNV(0x12510f0) at 0x12a0608 REFCNT = 1 FLAGS = () IV = 0 NV = 0 PV = 0

The first block tests for definedness, and then shows what's inside the scalar. Essentially it's empty, just contains undef.

The second block tests the scalar for string equality. This will cause a PV (string value) to be added to the scalar's internal structure, but the PV is =; it's empty. A future test for definedness would still be true. Perl happily just treated undef values as empty strings and compared them. Notice that warnings were generated.

The third block tests the scalar for numeric equality. This has caused an IV and NV (integer and numeric value) field to show up in the scalar struct. They are also empty. The PV field showed up again too, and is also empty. The scalar will still report to be undef if tested with defined. But internally its structure has been altered a little. Notice the warnings again, for doing a numeric comparison on uninitialized value.

All this is hidden in the internals. Mostly you just have to keep in mind that if it hasn't been initialized it will be undef, and that Perl is willing to let you do string and numeric operations and comparisons on undef, upgrading to the appropriate datatype as needed.


Dave


In reply to Re: Learning Programming, desperately need to know what information is contained in scalar variables by davido
in thread Learning Programming, desperately need to know what information is contained in scalar variables 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.