Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Perl Storybook, Part I

by metaperl (Curate)
on Feb 10, 2005 at 14:45 UTC ( #429742=perlmeditation: print w/replies, xml ) Need Help??

=for story Sally was walking along a path when she came to a very large hole. She filled the hole with sand =cut $hole = 'sand'; =for story As she walked across the hole, she realized she might get lost if she didn't put down some stones to find her way back. She put down a red stone, a blue stone and a white stone: =cut @stone = qw(red blue white); =for story No sooner had she put down the stones than she arrived at a 3-way intersection. Here is the Perl, you tell the story: =cut %directions = ( left => 'Yorkshire', right => 'Birmingham', straight => 'Nottamun' ); %distance = ( Yorkshire => '5 miles', Birmingham => '1.5 miles', Nattamun => '2.2 miles' ); =for story Did you tell the story? I'm sure you figured it out. However, let's have Perl tell it for us: =cut while (my ($direction, $location) = each %directions) { print "if she takes the $direction path, she will walk for $distance{$location} before arriving at $location.\n"; } =for story Ok, now we have ended the first lesson in our Perl storybook. We have learned many things in just one lesson: HOLES: We learned how to fill in holes. Whenever we want to fill in a hole, we just put something in it. The hole is anything that you want to hold a value. Here is one more example: Today is Saturday $today = 'Saturday'; BEADS: When Sally put down the stones on the path, she was storing a series of things to help her later. Whenever you have a list of things you want to keep together, you put them down together, just like putting beads on a necklace. Let's create a list of grocery items: my @shopping_list = qw(oranges apples bananas); DICTIONARIES: You know what a dictionary is right, you look up a key and you get a value. Let's create a dictionary of magic terms: %magic = ( spell => 'something you cast on someone', rabbit => 'something you pull out of a hat', wand => 'the thing you wave to cast a spell' ) Now let's get something out of our dictionary: =cut print $magic{wand}

Replies are listed 'Best First'.
Re: Perl Storybook, Part I
by BrowserUk (Patriarch) on Feb 10, 2005 at 14:59 UTC

    What! No pictures?

    If you're gonna patronise me, at least do it wiv piccies :)


    Examine what is said, not who speaks.
    Silence betokens consent.
    Love the truth but pardon error.
Re: Perl Storybook, Part I
by Ytrew (Pilgrim) on Feb 11, 2005 at 06:40 UTC
    Ignore the naysayers: I very much enjoyed your "story".

    Would that all programs were as engaging! I'm not sure the metaphors used scale to more advanced programming features, but they're simple, clearly drawn, and quite easy to follow. My compliments for your creativity!

    Do I have your permission to print out a copy? I'd like to show it to people offline, if that's okay with you.

    I'm looking foward to reading Part II.

    --
    Ytrew Q. Uiop

      Sure go ahead and print it out and share it. Thank you for your compliments!
Re: Perl Storybook, Part I
by dragonchild (Archbishop) on Feb 10, 2005 at 14:58 UTC
    <crochety and flippant response>

    And your point is ... ?

    </crochety and flippant response>

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

      And your point is ... ?

      Ah, come on... I think all the story needs is some chunky bacon :-)

      (Update: Just kidding, I think the story is fine).

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2023-12-08 20:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?











    Results (37 votes). Check out past polls.

    Notices?