strict irony

J

J Krugman

use strict;

print "Ain't it ironic?\n" if strict;
print "The things you learn...\n" if BAREWORD;
print "What's going on here?\n" if bewildered;

# but if this line is uncommented, script won't compile:
# my $x = BAREWORD;

# ...will wonders ever cease?
#
# jill
 
M

Matt Garrish

J Krugman said:
use strict;

print "Ain't it ironic?\n" if strict;
print "The things you learn...\n" if BAREWORD;
print "What's going on here?\n" if bewildered;

# but if this line is uncommented, script won't compile:
# my $x = BAREWORD;

# ...will wonders ever cease?

What wonders? That you're using strictures but not warnings?

Matt
 
J

Jay Tilton

: use strict;
:
: print "Ain't it ironic?\n" if strict;
: print "The things you learn...\n" if BAREWORD;
: print "What's going on here?\n" if bewildered;
:
: # but if this line is uncommented, script won't compile:
: # my $x = BAREWORD;

The 'if' modifiers are being optimized away. Why the optimizing phase
would turn a blind eye to barewords under strict subs, I don't know.

Compounding the mystery, adding "use warnings;" causes the missing
"Bareword not allowed" compilation errors to surface. Changing 'if' to
'unless' causes compilation errors as well.

A most curious discovery.
 
J

Jeff 'japhy' Pinyan

: use strict;
:
: print "Ain't it ironic?\n" if strict;
: print "The things you learn...\n" if BAREWORD;
: print "What's going on here?\n" if bewildered;

The 'if' modifiers are being optimized away. Why the optimizing phase
would turn a blind eye to barewords under strict subs, I don't know.

I'd have to look at the source to see why, but I'm guessing that the "if
BAREWORD" construction *is* being optimized away.
Compounding the mystery, adding "use warnings;" causes the missing
"Bareword not allowed" compilation errors to surface. Changing 'if' to
'unless' causes compilation errors as well.

That's because 'unless' is merely 'if not ...', so there is no longer an
"if BAREWORD" construct, there is an "if not BAREWORD", which is
apparently not covered by this optimization.

This optimization is, of course, compile-time, so don't be surprised that

use strict;
print "ok\n" if FALSE;
use constant FALSE => 0;

prints "ok". The removal of the conditional happens before the 'use
constant' line is reached.
 
M

Matt Garrish

Jay Tilton said:
: use strict;
:
: print "Ain't it ironic?\n" if strict;
: print "The things you learn...\n" if BAREWORD;
: print "What's going on here?\n" if bewildered;
:
: # but if this line is uncommented, script won't compile:
: # my $x = BAREWORD;

The 'if' modifiers are being optimized away. Why the optimizing phase
would turn a blind eye to barewords under strict subs, I don't know.

Unless I'm missing something obvious, I don't see the issue here. Until you
attempt to assign a bareword to a variable, why would strictures complain?

Matt
 
J

Jeff 'japhy' Pinyan

Unless I'm missing something obvious, I don't see the issue here. Until you
attempt to assign a bareword to a variable, why would strictures complain?

Because 'strict "subs"' is supposed to behave this way:

"strict subs"
This disables the poetry optimization, generating a compile-time
error if you try to use a bareword identifier that's not a sub-
routine, unless it is a simple identifier (no colons) and that it
appears in curly braces or on the left hand side of the "=>" sym-
bol.

and

if (foo) { ... }

is in direct opposition to the rules stated.
 
J

Joe Smith

Purl said:
Thanks, Joe. I was able to recreate this oddity
only when I used a print command.

I tested it with warn() in place of print(), and
by using a function that takes arguments.

use strict;
my $a="";
sub foo { $a .= "@_"; }
foo "Ain't it ironic?\n" if strict;
foo "The things you learn...\n" if BAREWORD;
foo "What's going on here?\n" if bewildered;
print "At end: $a";

I admit I did not try any other syntax, so I am
guilty of over-generalizing when I said "any statement".
-Joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,147
Messages
2,570,835
Members
47,382
Latest member
MichaleStr

Latest Threads

Top