Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Special cases make special community

by kyle (Abbot)
on Feb 01, 2009 at 06:36 UTC ( [id://740513]=perlmeditation: print w/replies, xml ) Need Help??

Perl has a lot of features that are not immediately obvious to the casual observer. By "casual observer," I mean someone who sees the language "in the wild" but hasn't formally learned it. Think of the programmer who learns it by opening up some working Perl and modifying it.

Consider, as an example, split. Here's a typical invocation:

my @fields = split /:/, $record;

To the casual observer, there are three things that go with split: a record, a field separator, and someplace to put the separated fields. One could see a lot of Perl without ever seeing any variation on this usage.

Nevertheless, there are a lot of possible variations.

  • The $record is optional and defaults to $_.
  • The field separator is also optional and defaults to a special form of splitting on white space (leading white space is ignored).
  • The field destination is optional too. If split is used in a scalar (or void) context, fields go into @_ (clobbering your sub arguments, if you have any). If you have warnings on, you may also learn that, "Use of implicit split to @_ is deprecated."
  • There's an optional third argument (a number) that limits how many fields split is allowed to return.
  • If the field separator contains capturing parentheses, whatever is captured in them will be returned as a field.
  • If the field separator is a single space (' '), it invokes the special white space splitting feature that you get with no arguments.

Those are just the ones I know off the top of my head. A look at the split documentation shows other interesting special cases that I'm pretty sure I've never run into. I know at least some of the features I've listed above were present in Perl 4, and yet they still feel to me like afterthoughts.

(A better example of an "afterthought feature" might be the (?xxx) style features in modern regular expressions. Why do they always start with an open parenthesis and a question mark? I've always assumed it's because that would have been a syntax error in earlier versions of Perl. No earlier code could have been relying on (?xxx) to do anything meaningful, so it's safe to have later code use it for features the earlier Perl didn't have. Now we have lots of different things introduced by an open parenthesis and a question mark.)

I like these extra features. It's fun, like discovering a pocket you didn't know you had in a garment you've worn for years. Still, I wonder sometimes if Perl's casual observers are put off by these things. My guess is that it depends on one's attitude. Some are (as I am) delighted by discovering something new in something old. Others may be dismayed that they did not know the language as well as they thought and may even despair that they'll never master it as they'd like.

I think this is why the Perl community is so important. The monks welcome newcomers and guide them through the language. Experts can show the inexperienced what's in the dark corners and tell them when and why to go there. There are pitfalls, but at the Monastery, there's usually someone hanging around to direct the unwary away from them. In these and other efforts, the monks demonstrate an attitude that delights in discovery so that those without that attitude may witness it, learn of it, and perhaps take it as their own.

Replies are listed 'Best First'.
Re: Special cases make special community
by ikegami (Patriarch) on Feb 01, 2009 at 08:37 UTC

    Those are just the ones I know off the top of my head

    split removes blank trailing elements when the third arg isn't specified. Specify -1 if you want no limit without removing blank trailing elements.

    Why do they always start with an open parenthesis and a question mark?

    Backwards compatibility. They picked a syntax that was unlikely to break any existing patterns, allowing code from earlier versions of Perl to continue to work in newer versions.

Re: Special cases make special community
by moritz (Cardinal) on Feb 01, 2009 at 20:54 UTC
    Let me generalize your statement: "If your system can be explored, it makes a special community".

    It's not only the special cases, it's that Perl is like a castle that you can enter through the main entry; you can stay in the large audience room for years, or you can explore all the dark corners, "slay dragons and rescue maidens" (to paraphrase Abigail-II; no offense meant to our Breathers of Fire).

    The multi-paradigm paradigm plays an important role here - you can write procedural, object-orientated, functional or declarative code (through regexes), and probably many other flavours, in just one language. Much space to explore. TIMTOWTDI.

Re: Special cases make special community
by Porculus (Hermit) on Feb 01, 2009 at 15:49 UTC

    The "split" special case I always think of is its behaviour with an empty regex, which is completely different to the regular behaviour of an empty regex, which is in turn a special case that does something I would never have guessed in a million years unless I'd read the documentation. :)

    (I find it quite useful in split, but I don't think I've ever seen it used otherwise.)

      For the unaware, the general behavior of an empty regex is to function as the last successfully matched regex, if I am not misremembering.

      for(split(" ","tsuJ rehtonA lreP rekcaH")){print reverse . " "}print "\b.\n";
Re: Special cases make special community
by JavaFan (Canon) on Feb 01, 2009 at 18:01 UTC
    One special case of split you haven't mentioned yet is split in void (or scalar) context, which puts the result into @_.

    Also, split with the empty pattern splits into separate characters.

    As for 'afterthought features' in modern regular expressions, since 5.10 we also have (*...) as 'new syntax'. Before 5.10, (* could not be valid (unless escaped). (+ is up for grabs for the next wave of new features. ;-)

Re: Special cases make special community
by slacker (Friar) on Feb 01, 2009 at 19:24 UTC
    As a new user to Perl, this kind of information is good to see. I'm hoping all the nuances of the language, such as this, are enough to keep me interested in using it for the long term.
Re: Special cases make special community
by sundialsvc4 (Abbot) on Feb 02, 2009 at 13:48 UTC

    In spite of what you see over cappucino in a bookstore, all programming languages are difficult to understand, especially at first. They are “human languages,” after all, and so they carry a lot of idiomatic meaning.

    As “native speakers,” we need to try to remember the “Perl As a Second Language” folks. (After all, plenty of times, they are us!) That's why comments are so important. And also, test plans. It does not hurt the Perl compiler in the slightest to pass-over any number of comments and PODs.

Re: Special cases make special community
by artist (Parson) on Feb 02, 2009 at 22:29 UTC
    Community is build around anything that is difficult to understand and/or it has multiple usage.
    The knowledge is built over the period of time by multiple people. We individually capture the small portion of overall knowledge and interact with it. If you really are into exploring, you can explore anything. Putting the knowledge in one's head slowly is like taking a spoonful water out of the ocean, one at a time. But we may have to do it. We are knowledge workers. We then like the knowledge that entertain us. Let it be exploring and tweaking the knowledge we gained. It could be in the form of new learning or poetry or obfuscated code. Mind in general has millions of information of this sort and we keep on adding to continue get entertained.
    --Artist
Re: Special cases make special community
by deprecated (Priest) on Feb 03, 2009 at 19:36 UTC
      I like these extra features. It's fun, like discovering a pocket you didn't know you had in a garment you've worn for years. Still, I wonder sometimes if Perl's casual observers are put off by these things. My guess is that it depends on one's attitude. Some are (as I am) delighted by discovering something new in something old. Others may be dismayed that they did not know the language as well as they thought and may even despair that they'll never master it as they'd like.
    I don't recall the last time I ever "discovered" a feature in a function I use in perl, as you described the default to $_ above. Rather, I go about things the opposite way (if you could call that). I use the internal functions the way I'd expect them to, and I'm pleasantly surprised when they allow me to use them that way.

    This is probably what leads to people thinking perl is hard to read, as you alluded to. My personal opinion on the matter is that people are trying to read perl without taking into consideration the style of the individual programmer. My feeling is that's the reader's fault, not the programmer. But this is why we have the little "+" and "-" buttons.

    --
    Tilly is my hero.

Re: Special cases make special community
by mtve (Deacon) on Sep 16, 2009 at 12:15 UTC

    If split is used in a scalar (or void) context, fields go into @_ (clobbering your sub arguments, if you have any)

    ... thus you can get the number of fields in a scalar and have the fields in @_ at once.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-04-23 11:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found