Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: a sub within a sub -- revisiting

by PeterPeiGuo (Hermit)
on Apr 10, 2010 at 05:29 UTC ( #833952=note: print w/ replies, xml ) Need Help??


in reply to a sub within a sub -- revisiting

Good code is usually straight forward, and this code is misleading in couple of ways:

  1. Visually, bar seems to be only available inside foo. But that's not true, bar can actually be called outside of foo.
  2. Make it even worse, when bar is called outside of foo, it does not do what's expected any more.

Peter (Guo) Pei


Comment on Re: a sub within a sub -- revisiting
Re^2: a sub within a sub -- revisiting
by punkish (Priest) on Apr 10, 2010 at 06:13 UTC
    Actually, I disagree. The provided code is actually visually very helpful, as it leads (mind you, leads, not misleads) the reader to think that bar() makes sense in the context of foo().

    From OOP we know that Perl doesn't protect methods and variables. Perl would much rather that you didn't come into its home uninvited than put locks on its doors. The reader of the code should simply not call bar() from outside foo().

    That said, the suggestion to convert bar() into an anonymous sub is a good one. That makes things even clearer.

    --

    when small people start casting long shadows, it is time to go to bed
      The provided code is actually visually very helpful, as it leads (mind you, leads, not misleads) the reader to think that bar() makes sense in the context of foo().

      Maybe that code documents that its author intended such a thing, but to an experienced Perl programmer, I suspect that the code instead reads as if the author doesn't know how lexicals and sub declarations work in Perl.

      The ... code [of the OP] ... leads ... the reader to think that bar() makes sense in the context of foo().

      But does the code lead the reader to think that foo() does not make sense in the context of bar()? (See ikegami's Re: a sub within a sub -- revisiting.) If the code does not alert the reader to the subtle (side?) effect pointed out in that reply, it is not helpful and should either be heavily commented or another, more straightforward approach should be chosen to achieve the same effect. (Of course, this assumes the effect is intended in the first place!)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://833952]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (9)
As of 2014-08-27 09:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (235 votes), past polls