Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Just Another Pricey Holiday

by higle (Chaplain)
on Feb 14, 2002 at 22:41 UTC ( #145566=poem: print w/ replies, xml ) Need Help??

#!/usr/bin/perl # Just Another Pricey Holiday # # My beloved's pricey holiday # Pushing for expensive things always # But I bought those gifts # In double shifts # So tonight I'll guarantee some play... # # -higle my $beloveds_pricey_holiday = "Valentine's Day"; until ( $some_play ) { push @expensive_things, buy(qw{those gifts}); $some_play = 1; } sub buy() { push @shopping_cart, shift; push @shopping_cart, shift; }


  higle

Comment on Just Another Pricey Holiday
Download Code
Re: Just Another Pricey Holiday
by thor (Priest) on Feb 15, 2002 at 13:26 UTC
    Your code is a bit flawed. You are not guaranteed to have $some_play=1 after you buy expensive things. Allow me to alter your code a bit:
    #!/usr/bin/perl # Just Another Pricey Holiday # # My beloved's pricey holiday # Pushing for expensive things always # But I bought those gifts # In double shifts # So tonight I'll guarantee some play... # # -higle my $beloveds_pricey_holiday = "Valentine's Day"; until ( $some_play ) { push @expensive_things, buy(qw{those gifts}); $some_play = 1 if &whim_of_significant_other; } sub buy() { push @shopping_cart, shift; push @shopping_cart, shift; } sub whim_of_significant_other { if (rand > 0.9) { return 1; } else { return 0; } }
    Of course, your mileage may vary. ;)

      I think your code is closer, but perhaps still a bit optimistic. Because the assignment of $some_play is in an until loop, it will continuously loop through and eventually get set (assuming that your randomizer has been seeded sufficiently so that it is possible for the whim to change).

      However, I've found that often buy() can somehow get stuck in an infinite loop until everything crashes. Once you are stuck in the loop, control is never passed back so that $some_play can even be considered. This can crash in two ways. Either you'll get a heap overflow from the recursion, or the credit card will fail.

      sub buy () { { $::not_a_good_night ||= rand; if ( $credit_card_balance > $credit_card_limit ) { die ( "Dog house tonight" ); } push @shopping_cart, shift(), shift(); buy(qw( more things )) if ( $::not_a_good_night > 0.3 ); }

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2014-07-10 05:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (199 votes), past polls