Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

non-barewords and use strict

by Solo (Deacon)
on Sep 16, 2002 at 20:27 UTC ( [id://198363]=perlquestion: print w/replies, xml ) Need Help??

Solo has asked for the wisdom of the Perl Monks concerning the following question:

After a somewhat exhaustive search (please forgive me if I've missed a relevant post here or elsewhere--please direct me to 'em) I've yet to find an answer to this troubling philosophical question:

Why does use strict only check barewords?

Allow me to demonstrate my confusion.

use strict; sub This::Does::Not::Fail { }; This::Does::Not::Fail; # rightly so This::Will::Fail; # as we expect It::Bothers::Me::That::This::Works (); # wassup with THAT?!
I'm confused why subs with an argument list (even an empty one) should cause a non-existent sub to be ignored by strict. Could the great Monks please enlighten me?
--
May the Source be with you.

You said you wanted to be around when I made a mistake; well, this could be it, sweetheart.

Replies are listed 'Best First'.
Re: non-barewords and use strict
by japhy (Canon) on Sep 16, 2002 at 20:35 UTC
    You're asking "how can I detect misspelled function names?", correct? Well, there's no easy way. As for why Perl doesn't do it automatically, that's because Perl lets you define functions at run-time, with eval() or AUTOLOAD().
    eval 'sub foo { print @_ }'; # edited foo(123);
    Edited, thanks to lestrrat.

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a job (NYC-area)
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Re: non-barewords and use strict
by sauoq (Abbot) on Sep 16, 2002 at 20:37 UTC

    When you call it with the argument list, it looks like a sub call, not a bareword and it decides that you must know what you are doing. The compiler isn't smart enough to know whether that sub will exist when that line executes. (You could eval it into existence, for example.) When you call it without the argument list, it just looks like a bareword and it doesn't assume that the bareword is actually a subroutine invocation because there is no real reason to make that assumption.

    -sauoq
    "My two cents aren't worth a dime.";
    
Re: non-barewords and use strict
by clintp (Curate) on Sep 16, 2002 at 22:48 UTC
    Lemme re-comment this for you:
    use strict; sub This::Does::Not::Fail { }; # Rightly so because you've already notified Perl that # this is a subroutine name. It *knows* this identifier # is a sub. Thanks for pre-declaring it! This::Does::Not::Fail; # rightly so # This fails because you've given perl no contextual hints # that this is a subroutine. It might be... a filehandle! # Or something else. But you haven't pre-declared it # either. This is a bareword (an identifier). This::Will::Fail; # as we expect # You haven't pre-declared this thing. # *BUT* you did give perl a contextual hint that this is # indeed a subroutine. This is essentially a *promise* # you've made to strict that at runtime, when this code is # encountered, you'll have a subroutine with this name # all ready to go. It::Bothers::Me::That::This::Works (); # wassup with THAT?!
    For that last point, there are other "promises" you can make and not just with parens. Some of these include:
    $obj->method; # Either $obj class' method will exist or # AUTOLOAD had better take care of it later. $a=foo Bar; # Bar will have a foo()! Or AUTOLOAD...etc. &foo; # Again, you've promised it will exist...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://198363]
Approved by Ovid
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-04-20 01:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found