Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: How not to pass undef variables

by count0 (Friar)
on Jan 10, 2002 at 22:02 UTC ( #137787=note: print w/ replies, xml ) Need Help??


in reply to How not to pass undef variables

From the very first assignment to @emails, you have no idea how many, if any, elements it has.

A good general rule to follow is that when you don't know if there is a value, test it! ;)

The @emails = grep... statement doesn't need any input, so you're safe there.
The foreach... doesn't need @emails to have anything in it, so again, that's ok.

When you get down to the split(), you have a problem. If @newemails has no elements in it.. perl will complain, and $email and $domain will be undefined.

The simplest way to deal with this situation is to test if @newemails has any elements. Wrap any code that depends on it containing something, into that test:

if (scalar @newemails) { my ($email, $domain) = split(/\@/, $newemails[0]); mail($newemails[0], $domain); }


Comment on Re: How not to pass undef variables
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (15)
As of 2015-07-30 19:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (273 votes), past polls