Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

chomp/defined question

Status
Not open for further replies.

SgtB

IS-IT--Management
Oct 3, 2002
447
US
Just got the Llama book, and learning perl!!!
Got a quick question though....
Here's the script:

chomp ($var = <STDIN>);
if (defined($var)) {
print &quot;Input was $var\n&quot;;
} else {
print &quot;no input\n&quot;;

At user input, if I just hit Enter, doesn't that just create a newline? Shouldn't the chomp command make the $var = undef then?

If I do that though, I just get &quot;Input was &quot;

Might be a simple question, but I thought that chomp always got rid of the first newline. ________________________________________
Check out
 
take away the defined() part. If the variable has been defined at all (even it it is null value) it will still count as defined. And here is a learning-curve-shortener:

$var = undef;
undef $var;

These two lines are different - that is - they do different things. The first line makes it so that $var is defined to have the null value. In this case, defined($var) returns true. The second line makes it like $var never existed. It is removed from the symbol table (I believe). And defined($var) returns false.

--jim * * * Merry Christmas and Happy New Year! * * *

( Don't touch that Red Flag link you crazy freak, it's just a holiday greeting! )
 
What I like to do is this...

Code:
 if (defined $var && $var =~ /\S/) {...}

Then it first checks if it's defined, and if it is, then it checks that it contains non-space characters.
Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
There is a real difference between being 'empty' and 'undefined'. A variable can exist and be empty and defined. 'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top