Perl FAQ 4.1: What are all these $@%*<> signs and how do I know when to use them?

Perl FAQ 4.1

What are all these $@%*<> signs and how do I know when to use them?

Those are type specifiers:

See the question on arrays of arrays for more about Perl pointers.

While there are a few places where you don't actually need these type specifiers, except for files, you should always use them. Note that <FILE> is NOT the type specifier for files; it's the equivalent of awk's getline function, that is, it reads a line from the handle FILE. When doing open, close, and other operations besides the getline function on files, do NOT use the brackets.

Beware of saying:

    $foo = BAR;
Which wil be interpreted as
    $foo = 'BAR';
and not as
    $foo = ;
If you always quote your strings, you'll avoid this trap.

Normally, files are manipulated something like this (with appropriate error checking added if it were production code):

    open (FILE, ">/tmp/foo.$$");
    print FILE "string\n";
    close FILE;
If instead of a filehandle, you use a normal scalar variable with file manipulation functions, this is considered an indirect reference to a filehandle. For example,
    $foo = "TEST01";
    open($foo, "file");
After the open, these two while loops are equivalent:

    while (<$foo>) {}
    while () {}
as are these two statements:
    close $foo;
    close TEST01;
but NOT to this:
    while (<$TEST01>) {} # error
    ^ note spurious dollar sign

This is another common novice mistake; often it's assumed that
    open($foo, "output.$$");
will fill in the value of $foo, which was previously undefined. This just isn't so -- you must set $foo to be the name of a filehandle before you attempt to open it.

Often people request:

How about changing perl syntax to be more like awk or C? I $$mean @less $-signs = &other *special \%characters?

Larry's answer is:

Then it would be less like the shell. :-)

You'll be pleased to know that I've been trying real hard to get rid of unnecessary punctuation in Perl 5. You'll be displeased to know that I don't think noun markers like $ and @ unnecessary. Not only do they function like case markers do in human language, but they are automatically distinguished within interpolative contexts, and the user doesn't have to worry about different syntactic treatments for variable references within or without such a context.

But the & prefix on verbs is now optional, just as ``do'' is in English. I do hope you do understand what I mean.

For example, you used to have to write this:

    &california || &bust;

It can now be written more cleanly like this:

    california or bust;

Strictly speaking, of course, $ and @ aren't case markers, but number markers. English has mandatory number markers, and people get upset when they doesn't agree.

It were just convenient in Perl (for the shellish interplative reasons mentioned above) to pull the markers out to the front of each noun phrase. Most people seems to like it that way. It certainly seem to make more sense than putting them on the end, like most varieties of BASIC does.

Other resources at this site: