Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

A feature of use

by ambrus (Abbot)
on Aug 19, 2008 at 19:57 UTC ( [id://705321]=obfuscated: print w/replies, xml ) Need Help??

Look at the following snippet.

perl -e 'open strict, ">&", STDOUT or die; use strict; print strict fo +obar, "\n";'

This prints foobar so clearly foobar is interpretted as a bareword. However, we use strict, so that should disallow barewords. Why is this? (The title of the post can give a hint.)

Replies are listed 'Best First'.
Re: A feature of use
by ambrus (Abbot) on Aug 20, 2008 at 19:57 UTC

    In fact, even this works.

    perl -e 'if (0) { open strict; } use strict; print STDOUT foobar, "\n" +;'
Re: A feature of use
by jdalbec (Deacon) on Aug 20, 2008 at 22:45 UTC
      Ah, you gave me enough clues...
      use strict is turned into require strict; which loads the module, followed by strict->import;. But Perl now knows strict as a filehandle, so this syntax is treated as an object method call to the filehandle object associated with the filehandle with the name "strict".

      Since either there is a method for the package IO::Handle (or else, import has a special status so perl won't complain if it can't find it), it works but actually doesn't do much. What it definitely doesn't do is call import for the package strict. That's why strict checks are still off.

      So it's all caused by the ambiguous syntax of word->method, which may represent either a class method call, or an object method call.

      It certainly seems so, we can even verify this like this.

      perl -we 'use IO::Handle; { package IO::Handle; sub import { warn "imp +ort called on handle $_[0]"; } } if (0) { open strict; } BEGIN { warn + "about to use strict"; } use strict; BEGIN { warn "after use strict" +; } use warnings;'

      However, I don't understand why this happens even before the open is called at all. I originally wrote the snippet with open in a BEGIN, because I thought that was the only way to trigger this. In fact, as open $foo, ... should autovivify a new glob with filehandle each time it's executed, I don't really know why open strict should do anything in compile time.

      Indeed, notice how my $h = *strict; open $h; triggers the feature if it's ran in a BEGIN block before use strict;, but not if it's just compiled before it.

Re: A feature of use
by ambrus (Abbot) on Jul 18, 2016 at 09:34 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: obfuscated [id://705321]
Approved by Joost
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-04-18 04:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found