Skip to content

Latest commit

 

History

History
216 lines (145 loc) · 6.37 KB

barewords.pod

File metadata and controls

216 lines (145 loc) · 6.37 KB

Barewords

Perl is a malleable language. You can write programs in the most creative, maintainable, obfuscated, or bizarre fashion you prefer. Maintainability is a concern of good programmers, but Perl doesn't presume to dictate what you consider maintainable.

Perl's parser understands Perl's builtins and operators. It uses sigils to identify variables and other punctuation to recognize function and method calls. Yet sometimes the parser has to guess what you mean, especially when you use a bareword--an identifier without a sigil or other syntactically significant punctuation.

Good Uses of Barewords

Though the strict pragma (pragmas) rightly forbids ambiguous barewords, some barewords are acceptable.

Bareword hash keys

Hash keys in Perl 5 are usually not ambiguous because the parser can identify them as string keys; pinball in $games{pinball} is obviously a string.

Occasionally this interpretation is not what you want, especially when you intend to evaluate a builtin or a function to produce the hash key. In this case, disambiguate by providing arguments, using function argument parentheses, or prepending unary plus to force the evaluation of the builtin:

Bareword package names

Package names in Perl 5 are also barewords. If you hew to naming conventions where package names have initial capitals and functions do not, you'll rarely encounter naming collisions, but the Perl 5 parser must determine how to parse Package->method(). Does it mean "call a function named Package() and call method() on its return value?" or does it mean "Call a method named method() in the Package namespace?" The answer varies depending on what code the parser has already encountered in the current namespace.

Force the parser to treat Package as a package name by appending the package separator (::)Even among people who understand why this works, very few people do it.:

Bareword named code blocks

The special named code blocks AUTOLOAD, BEGIN, CHECK, DESTROY, END, INIT, and UNITCHECK are barewords which declare functions without the sub builtin. You've seen this before (code_generation):

While you can elide sub from AUTOLOAD() declarations, few people do.

Bareword constants

Constants declared with the constant pragma are usable as barewords:

Note that these constants do not interpolate in double-quoted strings.

Constants are a special case of prototyped functions (prototypes). When you predeclare a function with a prototype, the parser knows how to treat that function and will warn about ambiguous parsing errors. All other drawbacks of prototypes still apply.

Ill-Advised Uses of Barewords

No matter how cautiously you code, barewords still produce ambiguous code. You can avoid most uses, but you will encounter several types of barewords in legacy code.

Bareword function calls

Code written without strict 'subs' may use bareword function names. Adding parentheses makes the code pass strictures. Use perl -MO=Deparse,-p (see perldoc B::Deparse) to discover how Perl parses them, then parenthesize accordingly.

Bareword hash values

Some old code may not take pains to quote the values of hash pairs:

When neither the Floyd() nor Annette() functions exist, Perl will interpret these barewords as strings. strict 'subs' will produce an error in this situation.

Bareword filehandles

Prior to lexical filehandles (lexical_filehandles), all file and directory handles used barewords. You can almost always safely rewrite this code to use lexical filehandles; the exceptions are STDIN, STDOUT, and STDERR. Fortunately, Perl's parser recognizes these.

Bareword sort functions

Finally, the sort builtin can take as its second argument the name of a function to use for sorting. While this is rarely ambiguous to the parser, it can confuse human readers. The alternative of providing a function reference in a scalar is little better:

The second option avoids the use of a bareword, but the result is one line longer. Unfortunately, Perl 5's parser does not understand the single-line version due to the special parsing of sort; you cannot use an arbitrary expression (such as taking a reference to a named function) where a block or a scalar might otherwise go.

In both cases, the way sort invokes the function and provides arguments can be confusing (see perldoc -f sort for the details). Where possible, consider using the block form of sort instead. If you must use either function form, consider adding an explanatory comment.

POD ERRORS

Hey! The above document had some coding errors, which are explained below:

Around line 3:

A non-empty Z<>

Around line 66:

Deleting unknown formatting code N<>