Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Assigning Null to a variable

by Ombongi.Moraa (Novice)
on Jul 02, 2012 at 13:14 UTC ( #979427=perlquestion: print w/ replies, xml ) Need Help??
Ombongi.Moraa has asked for the wisdom of the Perl Monks concerning the following question:

What happens when you assign space to a variable? I have this piece of code that goes;
my $alphanumber = ''; for (1..$length) { my $alphanumber .= $chars[int rand @chars]; }
Funny enough, when i append the result of $chars[int rand @chars]; to $alphanumber, the space disappears; What exactly is the meaning of assigning '' to a variable?

Comment on Assigning Null to a variable
Select or Download Code
Re: Assigning Null to a variable
by Corion (Pope) on Jul 02, 2012 at 13:16 UTC

    You don't assign a space to a string by assigning it the empty string. Compare the two following statements:

    #1234567890123 my $foo = ''; # Empty string my $bar = ' '; # String with a space

    Neither of them is the "null" value in Perl. The thing that comes closest to a "null" value is undef.

Re: Assigning Null to a variable
by Eliya (Vicar) on Jul 02, 2012 at 13:31 UTC
    for (1..$length) { my $alphanumber .= $chars[int rand @chars]; } ^^

    Most likely, your real problem is the above my ...

Re: Assigning Null to a variable
by frozenwithjoy (Curate) on Jul 02, 2012 at 14:31 UTC

    This isn't related to what you are asking about, but for future questions, it makes it a lot easier to read if you put tags around your code and add other appropriate formatting. Check out Markup in the Monastery.

    Also, if you look at the code you posted, the square brackets vanished and turned the text inside to a link because outside of code tags, square brackets are used for creating links (checkout What shortcuts can I use for linking to other information? for more info.)

Re: Assigning Null to a variable
by Anonymous Monk on Jul 02, 2012 at 15:02 UTC
    Perl does not have a value to correspond to the SQL notion of NULL. It only has undef. It does not have a built-in concept of "I know what the value is, and the value is: that it is known that it has no value at all."

      You could still use it that way in arrays.

      For hashes there is "exists" vs. "defined", see perldoc -f exists. If haven't tested it now (working from memory and POD), but this looks like it fits the bill.

      Sorry for any bad spelling, broken formatting and missing code examples. During a slight disagreement with my bicycle (which i lost), i broke my left forearm near the elbow. I'm doing the best i can here...

      I don't know enough about SQL to know how its  NULL compares to Perl's undef, but it strikes me that "I know what the value is, and the value is: that it is known that it has no value at all" is actually a pretty good (Update: well, on second thought, perhaps just a bit wordy) explanation of our friend undef!

        "I know what the value is, and the value is: that it is known that it has no value at all" is actually a pretty good explanation of our friend undef!

        ... and that's why DBI maps SQL's NULL to undef and vice versa.

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re: Assigning Null to a variable
by Anonymous Monk on Jul 03, 2012 at 02:23 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://979427]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (8)
As of 2014-08-20 05:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (105 votes), past polls