1

I know of Perl compiler back-end that allows you to convert any one-liner into script on following matter:

perl -MO=Deparse -pe 's/(\d+)/localtime($1)/e'

Which would give the following output

LINE: while (defined($_ = <ARGV>)) {
s/(\d+)/localtime($1);/e;
}
continue {
    print $_;
}

Is there possibly a reverse tool, usable from command-line, which provided full script will generate one-liner version of it?

Note: The above example was taken from https://stackoverflow.com/a/2822721/4313369.

Community
  • 1
  • 1
Reillusion
  • 15
  • 4
  • 4
    Not really - you can do a lot of complicated stuff in a script, so 'reducing' it isn't always possible. Also: Why do you want to do this? – Sobrique Jul 15 '15 at 11:45
  • Every now and then I'm editing a script which I'm using on few remote servers to which I have access through command-line. Figured it would be easiest to maintain it as full script then dynamically convert it to one-liner and send through the console. – Reillusion Jul 15 '15 at 11:51
  • Can do that as a HEREDOC easily enough, rather than a one liner. – Sobrique Jul 15 '15 at 11:55

1 Answers1

1

Perl is a free-form syntax language with clear statement and block separators, so there is nothing preventing you from simply folding a normal script up into a single line.

To use your example in reverse, you could write it as:

$ perl -e 'LINE: while (defined($_ = <ARGV>)) { s/(\d+)/localtime($1);/e; } continue { print $_; }'

This is a rather contrived example, since there is a shorter and clearer way to write it. Presumably you're starting with scripts that are already as short and clear as they should be.

Any use statements in your program can be turned into -M flags.

Obviously you have to be careful about quoting and other characters that are special to the shell. You mention running this on a remote system, by which I assume you mean SSH, which means you now have two levels of shell to sneak any escaping through. It can be tricky to work out the proper sequence of escapes, but it's always doable.

This method may work for automatically translating a Perl script on disk into a one-liner:

$ perl -e "$(tr '\n' ' ' < myscript.pl)"

The Perl script can't have comments in it, since that would comment out the entire rest of the script. If that's a problem, a bit of egrep -v '\w+#' type hackery could solve the problem.

Warren Young
  • 40,875
  • 8
  • 85
  • 101