Re: confused about a regex

On 3/8/07, oryann9 <oryann9@xxxxxxxxx> wrote:
Why is $_=abc.eeeee.i short for
$_ = 'abc' . 'eeeee' . 'i';

from "Programming Perl 3rd Edition":
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

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

use strict;
use warnings;

sub abc { "abc." }
sub eeeee { "eeeee." }
sub i { "i" }


print "$_\n";

*** Single Unix Specification, or what used to be known as POSIX

Relevant Pages

  • Re: code review - JSON parsing and data structures
    ... Didn't that give some bareword diagnostic under use strict, ... a function or a string. ... perl can go to really strange places: ...
  • Re: Look At This Package
    ... Bareword "fname" not allowed while "strict subs" in use at utest line 12. ... my $ui = new UserInfo(); ... sub full_name ...
  • Re: strict irony
    ... Why the optimizing phase ... would turn a blind eye to barewords under strict subs, ... "Bareword not allowed" compilation errors to surface. ...
  • Bareword errors?
    ... getting an inconsistant bareword error ... strict accepts the first use of the bareword(?) but not later - ... (not under strict one runs - the other gives error. ... read(STDIN, $buffer, $ENV); ...
  • RE: Bareword and strict subs
    ... A bareword is an unquoted string appearing in Perl source. ... Depending on context it is treated as a sub call or a string. ...