dakedesu has asked for the wisdom of the Perl Monks concerning the following question:

I would like to appologise, I do not know which perldoc document to ask for this but, what is allowed in naming a perl variable? More specifically: What characters are allowed for the first character? Is it alphanumerica, or are numbers not allowed for the first character?

I am sorry, but the book I originally learn perl from was a library book, and I cannot remember what the starting character allows. I cannot remember which perldoc manpage to check--as `perldoc perlvar` did something silly like contain info on predefined variables. A quick `perldoc -q variables` return two FAQ questions that did not tell me what I wanted. `perldoc -q naming` returned nothing as did `perldoc -q names`

Even giving a name of entry in perldoc would be nice.

Thank you for your time

  • Comment on Very quick question about names in perl

Replies are listed 'Best First'.
Re: Very quick question about names in perl
by Abigail-II (Bishop) on Dec 15, 2003 at 12:45 UTC
    For variables names that can be made lexical (that is, a 'my' can be put in front of it), names start with letters or underscore. Can't start with digits.

    Package variables can consist of anything, as long as you write them in the form: ${"..."}. Otherwise, they have to start with letters or underscore (just like lexicals), *or* consist of a single punctuation character, *or* consist of numbers only. That is, $117 is a legal variable (but it's read-only), @117 is legal, but $17foo is not.

    Abigail

      Thanks, but this raises more questions. What I plan on using this with, is to call functions that are part of an object, that will be read via $AUTOLOAD--would I be able to do something silly like $client->12('blah'), or would that be doing evil things?

        This is getting dangerously close to a philosphical discussion, but if you keep things simple, you'll have fewer things to worry about, and are therefore less likely to make mistakes. Speaking personally, I would limit myself to variables starting with letters and containing only letters, underscores and numbers. It makes the code easier to read (in theory), and you will spend less time later being confused. Assuming, of course, you are as easily confused as I am.

        --
        tbone1
        Ain't enough 'O's in 'stoopid' to describe that guy.
        - Dave "the King" Wilson

        $client -> "12" ('blah');
        ought to work, but I don't have time right now to check.

        Abigail

      There are also some special package variables like $^X. All of the ones that I know of, though, have established internal meanings.
        Those are usually considered to be punctuation variables as well.

        Abigail

Re: Very quick question about names in perl
by Ninthwave (Chaplain) on Dec 15, 2003 at 12:50 UTC

    Update:I mis cut and pasted the main link. Perl Documentation:perlvar
    and the quote from it but it sums itself up well.

    From: Perl Documentation: perldata

    Names

    Names that start with a digit may contain only more digits. Names that do not start with a letter, underscore, digit or a caret (i.e. a control character) are limited to one character, e.g., $% or $$. (Most of these one character names have a predefined significance to Perl. For instance, $$ is the current process id.)

    From: Perl Documentation: perl

    Bugs

    While none of the built-in data types have any arbitrary size limits (apart from memory size), there are still a few arbitrary limits: a given variable name may not be longer than 251

    "No matter where you go, there you are." BB

      Unless you're looking up names symbolically, in which case you can put anything in a variable name, including control characters:

      # 007 (octal) is ASCII bell perl -e '${"\00717foo"} = 1; print ${"\00717foo"}'

      Though this should usually not be confused with a good idea.

      ----
      I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
      -- Schemer

      : () { :|:& };:

      Note: All code is untested, unless otherwise stated

      Thanks--I guess they updated it since 5.6.1 (which I haven't bother to update from the default FreeBSD install)

        If possible, I would upgrade to version 5.8.x -- that's the current version, and one that most folk would assume you are using (unless you say otherwise). The most recent release is here and many other places around and about.

        --t. alex
        Life is short: get busy!