say What?

Now that I’ve got Snow Leopard (finally) installed on my Mac, the default perl binary is now 5.10.0. This means many things: The given keyword and smart matching, the defined-or operator, the wonderful additions to the regex engine, and other things I’m bound to blog about later when I get round to enthusing about them.

What I wanted to talk about today is the simpliest change that’ll be making the most difference to me on a day to day basis: The “say” keyword. More or less say is exactly the same as print but two characters shorter and automatically adds a newline at the end. This is most useful when you’re writing one liners. This quick calculation:

bash$ perl -e 'print 235*1.15, "\n"'

Becomes just:

bash$ perl -E 'say 235*1.15'

(Note the use of -E instead of -e to automatically turn on the 5.10 keywords like say without having to add use 5.10 or use feature 'say'.)

This saves us a grand total of nine keypresses (including having to hit shift one less time.) More importantly it saves us having to use double quotes at all. This is really useful when you’re already using the quotes for something else. For example, running a Perl one-liner remotely with ssh:

bash$ ssh me@remote "perl -MSomeModule -e 'print SomeModule->VERSION, qq{\n}"'

With 5.10 on the remote machine this becomes just:

bash$ ssh me@remote "perl -MSomeModule -E 'say SomeModule->VERSION'"

This has not only the advantage of saving me a bunch of keystrokes, but also doens’t make me think as much. And the less I have to think, the less chance I’m going to do something stupid and make a mistake.

Posted in 1

Permalink 10 Comments

10 responses to “say What?

  1. Why in the world weren’t you saying

    ssh me@remote "perl -MSomeModule -le 'print SomeModule->VERSION'"

    Or even better yet

    ssh me@remote perl -MSomeModule=99999999

    That will get you an error message like

    Carp version 99999999 required--this is only version 1.04 at /usr/share/perl/5.8/Exporter/ line 107.
    BEGIN failed--compilation aborted.

    Which has the version number in it (in this case Carp is version 1.04)

  2. Jakub Narębski

    Simply use ‘-l’ switch:

    bash$ perl -le 'print 235*1.15'

  3. Too bad you didn’t know about the -l option, which turns causes “print” to append a newline. :)

    I.e. “perl -MSomeModule -le ‘print SomeModule->VERSION'”

    The “say” version is still an improvement though, key stroke-wise.

  4. Rhomboid

    Seriously? You could have just used

    perl -le ‘print 235*1.15’

    …with perl 5.8.

  5. Too bad that apple shipped perl 5.10.0 with the unknown error bug…

  6. Sol

    I really scoffed at “say” when I first heard about it, but after nine months of Perl 6 programming I can say I was completely wrong about that. say $value; really is a lot nicer than print "$value\n";. And it’s even more drastic if you are calculating something you’d like to print without saving: say @values.sort.perl; is much cleaner than print "{@values.sort.perl}\n";. (Is there even a way to do that in one statement in Perl 5?)

  7. wallace

    say what one more time. i dare you!

  8. perl -e ‘print 235*1.15, “\n”‘

    Did you really use that? Did no-one ever introduce you to the -l command line option?

    perl -le ‘print 235*1.15’

    Still not as short as the 5.10 version, but you’ve been wasting a lot of keystrokes under 5.8.8 :-)

  9. 2shortplanks

    I’ve always disliked -l because it means that *everything* I print has a newline by default. Realistically, most times I tend to do several prints with a post if, and then a final say to avoid having to use a tertiary operator too much.

    Of course, in the convoluted blog entry that wasn’t clear…

  10. 2shortplanks

    @wallace: What?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Get every new post delivered to your Inbox.

%d bloggers like this: