Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Syntax over my head

by dd-b (Monk)
on Jan 10, 2012 at 23:49 UTC ( [id://947260]=perlquestion: print w/replies, xml ) Need Help??

dd-b has asked for the wisdom of the Perl Monks concerning the following question:

I just fixed a problem in my code where wr $server, $sendprefix; was generating 2012-01-10 17:07:37,773 ERROR kcmdproxy.child(10028):209 Exception while handling connection: Can't locate object method "wr" via package "IO::Socket::INET" at ./kcmdproxy line 149, <GEN9> line 1.. (Exception caught higher up the stack and turned into that log message.)

The fix was to change my code to wr($server, $sendprefix);.

I've been casually thinking there are many ways to call functions and it doesn't much matter which one I use. This situation is challenging that belief. Is there a good place to read up on this and become enlightened rather than more confused?

So far as I know, nothing is being called via package IO::Socket::INET where that error comes from; the "function" is not for example in a member of an object derived from that class, or anything like that. It's not in an object at all.

Replies are listed 'Best First'.
Re: Syntax over my head
by BrowserUk (Patriarch) on Jan 10, 2012 at 23:52 UTC

    You are being bitten by Indirect object syntax.

    Ask for more if that doesn't explain it for you.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

Re: Syntax over my head
by Anonymous Monk on Jan 11, 2012 at 00:06 UTC

    To disambiguate between indirect object syntax, and a function calls, you either need a forward declaration (or exporting), or parenthesis .

    $ perl -le " $f = \*STDOUT; w $f, 6; " Can't locate object method "w" via package "IO::File" at -e line 1. $ perl -le " sub w { warn @_ } $f = \*STDOUT; w $f, 6; " GLOB(0x99a674)6 at -e line 1. $ perl -le " $f = \*STDOUT; w $f, 6; sub w { warn @_; } " Can't locate object method "w" via package "IO::File" at -e line 1. $ perl -le " sub w; $f = \*STDOUT; w $f, 6; sub w { warn @_; } " GLOB(0x99a514)6 at -e line 1.
Re: Syntax over my head
by eyepopslikeamosquito (Archbishop) on Jan 11, 2012 at 10:54 UTC

    Had you been following the advice given in Perl Best Practices, you would not have fallen into this particular hole. The first item in Chapter 9 is: "Call subroutines with parentheses but without a leading &". Luckily, Chapter 9 happens to be the free sample chapter from this book so you can read why Damian offers this advice right now.

Re: Syntax over my head
by perlfan (Vicar) on Jan 11, 2012 at 03:55 UTC
    Basically, what the others say is that if you want to call a subroutine before it's declared, you have to use parenthesis for the parameters.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2024-03-28 23:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found