Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: How am i doing?

by cavac (Prior)
on Jul 21, 2025 at 13:24 UTC ( [id://11165740]=note: print w/replies, xml ) Need Help??


in reply to How am i doing?

Overall, a good start. The main some problems that i see:

  • You are developing for an ancient version of Perl. Since then, subs (=functions) can now be declared to directly take function arguments.
  • You are using recursion. Unless you really know what you are doing (and set strict recursion limits), that's something that can backfire quite easily. (And this isn't a problem that requires recursion).
  • You are using global variables when you don't have to. That makes code re-use much harder.

Here is a version i quickly "modernized" and "cavacized". I'm note sure if the result is correct, though, since i don't have the description in the book available (and i had only a few minutes during my afternoon break to write this).

#!/usr/bin/perl # Latest perl version with the newest features and bugfixes use v5.40; # Do all kinds of automatic checking of the program code use strict; use warnings; # Great debugging tool to quickly print a complex structure (array, ha +sh, scalar, whatever) to the console use Data::Dumper; # Call the calculation main routine my $max = 100; # 4_000_000; my @baseframe = (1, 2, 3); my $result = fib($max, \@baseframe); # Call with a REFERENCE to @basef +rame, so subs can modify it # Print the final result print 'Final: ', $result, "\n"; # For debugging, print the final state of @baseframe print 'Final @baseframe: ', Dumper(\@baseframe), "\n"; # And end scene... exit(0); # This does ONE round of the calculation, no deep recursion sub fib_one_round($frame) { $frame->[0] = $frame->[0] + $frame->[1]; $frame->[1] = $frame->[2]; $frame->[3] = $frame->[2]; if(($frame->[0] %2) != 0) { return $frame->[0]; } return 0; } # This does the main loop. We are working on an array *reference*, so +fib_one_round can modify the array sub fib($max, $frame = [1, 2, 3]) { my $result = 0; # Loop until the max value has been reached while($result < $max) { $result += fib_one_round($frame); # Print the intermediate results for debugging print $result, "\n"; } return $result; }

Basically, i replaced the recursion with a while loop, added modern sub-signatures, made sure the "frame" we are working on is passed through the function calls (instead of global vars). And removed/inlined the count() function because it's a single if-statement that doesn't warrant the performance overhead of a funtion call. And, oh, added an example of Data::Dumper to show how you can easily look at complex data structures for debugging.

I also moved the main code above the subs, because that makes it easier if you come back at a later time to the code. The high level stuff is at the beginning (showing/explaining what the script does), the nitty-gritty details that you might never have to touch again are below that.

Hope that helps.

PerlMonks XP is useless? Not anymore: XPD - Do more with your PerlMonks XP
Also check out my sisters artwork and my weekly webcomics

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2025-12-12 16:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your view on AI coding assistants?





    Results (92 votes). Check out past polls.

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.