What will happen if...

I use all of $a, @a and $a[2] variables in the same program?

The scalar variable $a will be unrelated to the array variable @a.

$a[2] will denote the third element in the @a array, and will be unrelated to $a.

I use a variable to which no value(s) have been previously assigned?

The variable will come into existence with a default initial value.

A scalar variable will have the undef value. Inside a numerical operation it will be regarded as 0, and inside a string operation it will be regarded as an empty string "".

An array variable will become an empty list, ().

I assign a scalar value into an array variable?

The scalar value will become the single element of the array.



@array = 5;
# same as @array = (5);

$n = 7;
@array = $n;  
# same as @array = ($n);

I assign an array variable into a scalar variable?

The scalar variable will contain the number of elements in the array.


@array = ("Dafna", "Michal", "Rina");
$var = @array;

# $var is now 3;

Find more "Ifs" in Chapter 3 of the "Learning Perl" book...or try them yourself!

Table of Contents.
Next | Next.