thran has asked for the wisdom of the Perl Monks concerning the following question:
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
(tye)Re: Out of memory
by tye (Sage) on Oct 02, 2001 at 01:59 UTC | |
You have implemented an "infinite-stack state machine". (: You have several cases of, you want to "do_this and then do_that" so you write code that says: which means that you won't return from do_this() until you have finished do_that() but do_that() does the same thing so each time you do something new you tie up a new slot on the stack for a subroutine that will never return. You really need to replace at least one set of these calls with a loop. It would also help to indent your code and read up on "here doc"s (in "perldoc perldata") for large blocks of text. And please use &mysub(); instead of &mysub; (see (tye)Re: A question of style for more on that). - tye (but my friends call me "Tye") | [reply] [d/l] [select] |
|
Re: Out of memory
by perrin (Chancellor) on Oct 02, 2001 at 02:01 UTC | |
| [reply] |
|
(jeffa) Re: Out of memory
by jeffa (Bishop) on Oct 03, 2001 at 00:32 UTC | |
Try #1Simply encapsultate all your print statements in a subroutine - since you are printing to a socket ($client) you should pass it as an arg to the sub - notice my use of whitespace to INDENT the code inside the BRACES:But printing inside a sub like this is not very modular, imagine if you wanted to store the ASCII art in a variable for latter use - this would always print! So, lets use a scalar instead:
Try #3WAIT FOR IT!!!
Try #2Now we are getting somewhere - we can now use this subroutine (notice that the name changed to reflect it's PURPOSE) 2 ways: Much better. But that looks like Java code - ick. Perl is smarter, and doesn't restrict us to having to break strings across multiple lines, we can do this: Perl also provides the q() operator, which is just like single quotes: The cool thing about q() is you can use other chars as delimiters - q{cool} q/cool/ q#cool# q{cool}. Since bunny does not contain any braces in it's ASCII art, we can safely use it for . . . Try #3Here i use the q() (AKA, q{}) operator to get the ASCII art into $bunny, then i use the s/// operator to add leading space (notice the x operator where $spaces is defined) as well as append \r to each line (\n is already there!). i also removed all backslashes that were being used to escape meta-characters - as a result, the bunny was harmed:Be sure and read up on q() and it's cousins, qq(), qr(), and qx() at perlop. jeffa | [reply] [d/l] [select] |