On 3/8/07, oryann9 wrote:
snip
Why is $_=abc.eeeee.i short for
$_ = 'abc' . 'eeeee' . 'i';
snip
from "Programming Perl 3rd Edition":
bareword
A word sufficient ambigious to be deemed
illegal under use strict 'subs'. In the
absence of that stricture, a bareword is treated
as if quotes were around it.
Basically a bareword is anything that matches /[_a-zA-Z][\w\d]*/ and
does not have enough context around it for the perl interpreter to
decide what it is. For instance, the FILE in the code below matches
the bareword pattern, but the context of open and print let the perl
interpreter know it is actually a file handle*.
open FILE, '>', $file or die "could not open $file: $!";
print FILE "FILE is not a bareword";
But in the case we are talking about the interpreter does not know
what to do with abc, eeeee, or i. They aren't file handles. They
aren't functions** being called with no arguments (because the
interpreter hasn't seen a definition for abc, eeeee, or i yet). So it
just throws quotes around each of abc, eeeee, and i and moves on.
Now all of this begs the question "why do barewords exist at all?".
The short answer is they don't if you use the strict pragma like you
are supposed to. The longer answer is that Perl is multipurpose
language. It supports styles and usages that are outside of the
normal programming scope. One of its many uses is as a commandline
tool. I use it everyday instead of the common SUS*** utilities grep,
cut, sort, etc. I don't sit down and write a file to run through the
interpreter; instead I say things like
cdparanoia -Q 2>&1 | perl -ane 'print $F[2] if $F[0] eq TOTAL'
In that environment I often want to cut down the number of characters
I have to type to get the job done and barewords are one of the things
Perl lets me use to achieve that goal. Barewords also have uses that
the strict pragma doesn't ban. Specifically the ones involving
hashes:
my %hash = (
foo => 'foo is a bareword'
);
$hash{bar} = 'if the key is a bareword I don't have to quote it';
$hash{'not this'} = 'this key contains a non-bareword character, so it
must be quoted';
I hope this has been helpful to you.
* but you should be using bareword like file handles anymore either.
Use IO::File and the "open my $fh, '>', $file" idiom instead.
** This is sort of how use constant works, try this at home
#!/usr/bin/perl
use strict;
use warnings;
sub abc { "abc." }
sub eeeee { "eeeee." }
sub i { "i" }
$_=abc.eeeee.i;
print "$_\n";
*** Single Unix Specification, or what used to be known as POSIX