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

Re: Re: Problem with tie *STDIN, 'IO::Scalar', \$text;

by Clownburner (Monk)
on Apr 30, 2001 at 01:14 UTC ( #76509=note: print w/replies, xml ) Need Help??

in reply to Re: Problem with tie *STDIN, 'IO::Scalar', \$text;
in thread Problem with tie *STDIN, 'IO::Scalar', \$text;

Sorry this is sorta off on a tangent, but why the hostile attitude towards subroutines?

Obviously, implicit modification of globals CAN be a Bad Thing™ if it's done in such a way that it's hard to follow, but if you're writing a small script,and pressed for time, and you're careful to document what you're doing, it's a timesaver. What other problems does it cause that I don't know about?

I've always thought of subroutines as just that - named blocks - and to me, that makes them a handy way to document or re-use code, even if they don't take args or return anything explicitly.

Have I overlooked some major diabolical evil that subroutines are capable of without knowing it? Or is my perl style just lacking for want of experience?
"All generalizations are incorrect, including this one." Anonymous
  • Comment on Re: Re: Problem with tie *STDIN, 'IO::Scalar', \$text;

Replies are listed 'Best First'.
Re: Re: Re: Problem with tie *STDIN, 'IO::Scalar', \$text;
by LunaticLeo (Scribe) on Apr 30, 2001 at 20:36 UTC
    I think you have overlooked some major diabolical evil :).

    Named blocks, as opposed to functions, HIDE the external effects of the block from the reader. Using the name of the block as some kind of documentation feature is a poor choice.

    Even if you want your subroutine to affect globals, you can pass globals in and return new values for globals. This "documents" your named block even better; explicitly telling the reader what this chunk of code depends on and what it changes.

    $GLOBAL1 = do_stuff($GLOBAL0, $GLOBAL1);

    is MUCH better than:

    ... set globals ... ... do_stuff; ... ... use modified globals

    I have had to read code like this (and it is very hard):

    &read_input; &calculate_this; &calculate_that; &write_output; # some to files others to databases # all in one function. sub calculate_this { ... } sub read_input { ... } sub write_output { ... } sub calculate_that { ... }

    This was horrible. I was tempted to just remove the "sub" lines, but then I noticed the subs were not declared in the order they were used. As I paid more attention, some variables inside the subs were my()'ed but had the same name as globals.

    We had to audit this code to make sure it was calculating money dispusements correctly. How can you audit this code?

    It was painfull to read, impossible to audit, and such a rats nest it was difficult to modify. I lay most of the blame at the fact that it was using globals, locals, and named blocks. Writing functions force a certain amount of structure. Even if the structure is lame it is emminently more readable and auditable.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2020-09-20 12:06 GMT
Find Nodes?
    Voting Booth?
    If at first I donít succeed, I Ö

    Results (121 votes). Check out past polls.