http://www.perlmonks.org), the user known as ton asked about parsing a Perl-style double-quoted string, as part of a project to construct a safe Data::Dumper parser that would take output and interpret it rather than handing the result directly to eval. The work in progress for their Undumper was posted, and I commented that there was probably a simpler way to do some of the things and noted that it didn’t handle blessed references.

" />
x
Loading
 Loading
Hello, Guest | Login | Register

Safe Undumping

Recently on the Perl Monastery (http://www.perlmonks.org), the user known as ton asked about parsing a Perl-style double-quoted string, as part of a project to construct a safe Data::Dumper parser that would take output and interpret it rather than handing the result directly to eval. The work in progress for their Undumper was posted, and I commented that there was probably a simpler way to do some of the things and noted that it didn’t handle blessed references.

Recently on the Perl Monastery (http://www.perlmonks.org), the user known as ton asked about parsing a Perl-style double-quoted string, as part of a project to construct a safe Data::Dumper parser that would take output and interpret it rather than handing the result directly to eval. The work in progress for their Undumper was posted, and I commented that there was probably a simpler way to do some of the things and noted that it didn’t handle blessed references.

Well, me and my big mouth…Or, maybe it was just an obsession to fiddle around with Damian Conway’s excellent Parse::RecDescent module? I’m not sure, but I found myself over the next dozen hours staring at Data::Dumper source code, output, test cases, and Parse::RecDescent traces and documentation. I also pounded my head on the desk for a day, trying to figure out how to break a left-recursion loop, and came up with an obvious (now!) solution.

The goal is to be able to take the output of Data::Dumper and reconstruct the original data, but not open ourselves to the possibility of being fed dangerous constructs like backticks or symbol-table-manipulating code. Sure, you could do this with the Safe module, but I was committed to finishing this version before I thought of that.

Anyway, this brings us to the program in Listing One. Most of this program is input to Parse::RecDescent. I’ll set this aside and describe the Perl support structure first.

Lines 1 through 3 start nearly every program…

Please log in to view this content.

Not Yet a Member?

Register with LinuxMagazine.com and get free access to the entire archive, including:

  • Hands-on Content
  • White Papers
  • Community Features
  • And more.
Already a Member?
Log in!
Username

Password

Remember me

Forgotten your password?
Forgotten your username?
Read More
  1. Helpful Tools for Software Developers
  2. The Github Hall of Fame
  3. Book'em, Github.
  4. This Week on Github: Stupid Ruby Tricks
  5. A Veritable Scatter Shot!
Follow Linux Magazine
Rackspace