Main Content

Perl for breakfast

Archive - Originally posted on "The Horse's Mouth" - 2004-08-25 06:53:10 - Graham Ellis


@breakfast = ("sausage","bacon","eggs");

# List Context
@b = @breakfast;
print @b," ... a list \n";

# Scalar Context
$c = @breakfast;
print $c," ... a scalar \n";

# Double Quote Context
$d = "@breakfast";
print $d," ... in double quotes \n";

__END__

Perl doesn't have arrays - it has lists which can do all the good things an array can do, and
much much more. This piece of code is a reminder of how a Perl list can be referred to in
different contexts ... giving you the contents, the contents with a space between each element,
and a count of the number of elements in the list with the most subtle of syntax changes.

Results when run:

earth-wind-and-fire:~/aug04 grahamellis$ perl nugget
sausagebaconeggs ... a list
3 ... a scalar
sausage bacon eggs ... in double quotes
earth-wind-and-fire:~/aug04 grahamellis$