2018 twenty-four merry days of Perl Feed

Christmas Quoting

B::perlstring - 2018-12-04

"Today," Buddy Snowbeard explained to his Elf class, "I'm going to change your world."

"Not by teaching you anything earth shattering. Oh no, because becoming an effective programmer isn't about making giant leaps, from awesome framework to awesome framework. It's about learning the little things each day, that make your life easier."

"Today", Buddy intoned with undue seriousness, "I'm going to teach you how to print out a variable!"

The groans from the class were audible. The class knew this. It'd been covered in Perl Programming 101 back in Elf kindergarten. They were sixth graders for Santa's sake, and this was like teaching granny elf to suck candy.

"Now I know what you're all thinking", Buddy continued, "but listen to me for a second."

He tapped the board: "I see a lot of elves out there printing variables out like this:"

say STDERR 'DEBUG: The next $childname is '.$childname;

"And you're saying to yourselves what's wrong with that? Plenty! Plenty is wrong with that."

"You in the front, Noelle Toffeecane, what is the problem here?"

"Er," Noelle stammered, "you can't tell where the thing you're printing out starts and ends?"

"Very good Miss Toffeecane. I guess we could change it to something like this:"

say STDERR 'DEBUG: The next $childname is "'.$childname.'"';

"Of course this still has problems. What might they be, Cookie Flippyfeast?"

"The childname could have like a double quote in it? You know, it could, like, um mess it up like? So you'll be like, I have no idea where this even starts and ends like?"

"Eloquently put Mr Flippyfeast. And why might $childname have a double quote in it? Because it could like have anything in it like. After all that's why you're printing it out. To debug it. Skittle Dreamwish, what else could be in the string that'd mess up our output?"

"A newline?"

"Good. Another Fruity Flippywarmth?"

"A unicode character"

"Yes! And you'd be trusting your terminal to render the sequence out - you'd have no way of telling exactly what character sequence was actually in the string. What other character might our terminal get confused with, Tiny Plumhat?"

"A control character, sir. You could end up turning the whole terminal red on green. Though that would look festive, sir."

"Indeed, but it wouldn't help with your debugging. In fact, let's create a truly naughty string to print out."

use Term::ANSIColor;
my $childname = color('red on_green');
$childname .= "Larry the \N{SNOWMAN}";
$childname .= '\n"';

"What do we need to do before we print it out - you in the back - Stripes Dreamfeast isn't it?"

"You need to escape it, sir. I'd use Data::Dumper"

"And how would that look, Mr Dreamfest? Let's write it up on the board."

use Data::Dumper;
say STDERR 'DEBUG: The next $childname is '. Dumper($childname)

"But even with a nice string that'll print out something like this"

    DEBUG: The next $childname is $VAR1 = 'Larry Wall';

"Not exactly what we want. We can do better? Any ideas, Dash Sleepyhat?"

"There are options for Data::Dumper, sir. We can pretty much make it do whatever we want"

"Yes you can. But can you remember them off of the top of your head? I certainly can't. And I don't want to write three lines of options in front of every debug statement I use. What's another module we could use? Anyone?"

The class was silent. Eventually a small elf called Mistle Ivybell raised his hand at the back. "Data::Printer, sir?"

"Data::Printer is a good choice Mr Ivybell, but it wasn't exactly what I was looking for. I'm thinking of something instead that's always installed and ships with perl? Anyone?"

No hands went up.

"Oh surely some of you must have heard of the B module? It allows you to do internal like things with Perl. And one of the things it can do is get a Perl string representation of any string. Let's see how this works:"

use B qw(perlstring);
say STDERR 'DEBUG: The next $childname is '. perlstring($childname)

"And that prints out something - if not nice, since we didn't gave it such a naughty string to start with - at least unambiguously understandable"

    DEBUG: The next $childname is "\x{1b}[31;42mLarry the \x{2603}\""

Just then the bell rang.

"That's all for today class! Don't forget your test scripts are due by the end of the week!"

Gravatar Image This article contributed by: Mark Fowler <mark@twoshortplanks.com>