Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Mnemonic Devices for Perl Programming

by dws (Chancellor)
on Nov 16, 2001 at 02:33 UTC ( #125702=perlmeditation: print w/replies, xml ) Need Help??

Many people use mnemonic devices to help remember to take the right action at the right time. For example, "Spring forward, fall back" helps one remember which way to change their clocks at the twice-yearly Daylight Savings Time switch (where DST applies). "Roy G. Biv" helps one remember the colors in the rainbow in correct order. Etc.

I'm starting a collection of mnemonic devices for Perl. The collection is starting small.

Question that Dot Star is a reminder to consider whether one really wants ".*" to be greedy, or whether ".*?" (or a negated character class) is a better option.
Do you have a favorite mnemonic device to contribute? What do you say to yourself while programming to help remember to do the right think at the right time?

(If someone has one that will help me remember the difference between /m and /s, I'll be forever grateful.)

Replies are listed 'Best First'.
Re: Mnemonic Devices for Perl Programming
by blakem (Monsignor) on Nov 16, 2001 at 04:20 UTC
    my mnemonic for /s and /m is:
    /s -- Allows .* to Slurp up numerous lines
    /m -- Allows ^ and $ to match in the Middle of the string


      This is in my book, by the way. Thanks for your information.

      Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
      s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

        And now everyone knows why I got to be mentioned in the same breath as merlyn and Erudil. dotSlurp1 mode indeed... ;-)

        But seriously, go check out japhy's book... Its a really good read and goes into some new regex stuff that I didn't know much about.

        1dotSlurp was coined by dws as a summary of my mnemonic. kind of catchy really...


Re (tilly) 1: Mnemonic Devices for Perl Programming
by tilly (Archbishop) on Nov 16, 2001 at 02:50 UTC
    My favorite is, "Hash-lookup is read, 'of'"

    This helps enormously when figuring out how to name hashes. Try it.

    If you want to create some for /m and /s, you could do worse than, To match many lines, /m and There is a single character "." doesn't match, unless you /s.

Re: Mnemonic Devices for Perl Programming
by halxd2 (Monk) on Nov 16, 2001 at 03:01 UTC
    use strict or die;

    took me a long time to see the wisdom!
Re: Mnemonic Devices for Perl Programming
by brianarn (Chaplain) on Nov 16, 2001 at 06:31 UTC
    Well, one thing I used to forget a lot was to make sure I successfully opened a filehandle properly, cause it'd cause my code to die in a funny way, so I reminded myself to
    open FH, "< blah.txt" or die "Can't open file: $!";
    and I could never remember what the special variable was, until I came up with the thought that, when I cause I program to die like this, I'm taking it out with a bang.

    <rimshot here>

Re: Mnemonic Devices for Perl Programming
by Sweeper (Pilgrim) on Nov 16, 2001 at 02:55 UTC
    The /s modifier Simplifies the Silly Signification of dot:
    "everything except \n", how Silly!
    "everything", how much Simpler!

    The /m modifier allows ^ and $ to Match even in the Middle of a string.

    How this help. It certainly has helped me.

Re: Mnemonic Devices for Perl Programming
by TheDamian (Priest) on Nov 17, 2001 at 07:16 UTC
    Interesting how individualized mnemonic preference is. We've now had several mnemonics for the /s and /m regex modifiers, none of which resonated with me at all.

    I've always preferred the one suggested to me by Uri Guttman:

    /s modifies the behaviour of a Single metacharacter (.)
    /m modifies the behaviour of Multiple metacharacters (^ and $)
Re: Mnemonic Devices for Perl Programming
by Biker (Priest) on Nov 16, 2001 at 13:41 UTC

    Well, I certainly didn't invent this classic, but I do apply it to Perl programming just as often as for many other daily tasks.

    KISS -- Keep It Simple, Stupid!

    f--k the world!!!!
    /dev/world has reached maximal mount count, check forced.

Re: Mnemonic Devices for Perl Programming
by demerphq (Chancellor) on Nov 16, 2001 at 21:01 UTC
    Not real sure if this is a mnemoic, but a rule of thumb that I find useful when explaining the behaviour of chained maps and other list functions is

    Maps make more sense when read right to left.

    OT:I remember learning the color codes for resistors in electronics class in high school. The teacher gave us a contrived mnemoic which frankly I could never keep in my head. I was saved however by an electronics book from years before which had the rather saucy menomic

    Bad Boys Raped Our Young Girls But Violette Gave Willingly

    Which I have never managed to forget. :-)

    Yves / DeMerphq
    Have you registered your Name Space?

Re: Mnemonic Devices for Perl Programming
by George_Sherston (Vicar) on Nov 16, 2001 at 20:56 UTC
    I hadn't thought of this, but it's quite a useful thing to do. I have to look up my Perl 5 pocket reference every time I do seek because I have a mental block abt whether the position comes before or after the whence (somehow it makes more sense that it shd come after, actually)... the trouble is, I can't actually think up a snappy mnenomic for FPW. The best I cd do was appropriate for foulmouthed people who don't like accountants, viz **** Price Waterhouse. The asterisks are fine because I know the filehandle comes first ;-). But perhaps the process of looking for a mnenomic will break my blockage and I'll be able to remember anyhow.

    George Sherston
      If inclined, one could write a reverse acronymer. With Perl and a word list one could find mnemonics like:

      seek frozen panda waffles
      seek fattened pizza witches
      seek fierce psychedelic wives

      I never cared for 'Every good boy does fine'. Maybe I can lobby for 'Extinct goats burn dry footballs'?


      #!/usr/bin/perl use strict; my ($string, $num) = @ARGV; my ($DICT) = '/usr/share/dict/words'; my (%words, @chs, $ch, $line, $i); @chs = split('', $string); for $ch (@chs) { if (!defined($words{$ch})) { $words{$ch} = []; } } if (!open(IN, $DICT)) { print STDERR "\nError:$DICT\n\n"; exit; } while (chomp ($line = <IN>)) { $ch = substr($line, 0, 1); if (defined($words{$ch})) { push (@{$words{$ch}}, $line); } } for ($i = 0; $i < $num; $i++) { for $ch (@chs) { print "$words{$ch}->[rand(@{$words{$ch}})] "; } print "\n"; }

      Hm, "Seek Fortune, Passion, and Wisdom"?

      James Mastros,
      Just Another Perl Scribe

Re: Mnemonic Devices for Perl Programming
by belg4mit (Prior) on Jan 25, 2002 at 06:00 UTC
    See also quest for perl mnemonics. I posted mine there because I came across it first when looking for this thread.

    perl -pe "s/\b;([st])/'\1/mg"

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://125702]
Approved by root
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (11)
As of 2017-09-26 20:59 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (297 votes). Check out past polls.