Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
Perl: the Markov chain saw
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

Perl allows to use arithmetics on string

Here's the code:
#!/usr/local/bin/perl -w use strict; sub step_over_desert # () I won't repeat this mistake anymore!!! { print "Tsock...\n"; } sub cover_the_distance # () I won't repeat this mistake anymore!!! { my ($me, $desert_width, $max_spit_distance, $num_legs, $last_name, + $day_phone, $days_without_water) = @_; my $num_steps=0; while($desert_width) { my $todo = $desert_width * $num_legs; print "$desert_width miles remaining; $todo steps yet to do.\n" +; step_over_desert(); $desert_width--; $num_steps += $num_legs; } print "Steps made: $num_steps\n"; } #cover_the_distance("strong animal", 5, 15, 4, "Mabooka", "(123)-456-7 +890", -1); cover_the_distance("strong animal", "five thousand miles", 15, 4, "Mab +ooka", "(123)-456-7890", -1); 1;

Don't try it at home

Well, I recommend at the very least to redirect the output to a file:-).

Why people do this?

Again, the main reason: because it allows you. One may expect either a compile-time error or a run-time exception. But Perl allows you to shoot your own foot.
Actually, this is an obfuscated (starting to love the word) code from a real-life app. The real application would send a query over the wire to the server,
get a responce line --
-- that should look smth. like this:
200 OK 578
-- meaning " there're 578 lines yet to read from the socket,
parse this line and create a loop:
while($numlines) { get_next_one(...); $numlines--; ... }
Well guess what? Once in 10000 times the server would get tired and send a garbled status line -- smth. like:
200 OK whatever,man
and
(while "whatever,man"--) {....}
will start the almost infinite loop. (Try the code above).

Solution

Be aware.
use int($n).
Ask your new hires during the inteview: "What is the result of the following operation:
"Larry"--;
-?.

In reply to Re^2: Example 2: incrementing "something" by Mabooka-Mabooka
in thread Common Beginner Mistakes by chromatic

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    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-04-16 11:39 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      April first is:







      Results (424 votes), past polls