]> git.donarmstrong.com Git - perltidy.git/blobdiff - examples/ex_mp.pl
Imported Upstream version 20130922
[perltidy.git] / examples / ex_mp.pl
index 75abc74096d413677d4157268bdababf83d96883..2bee266a9d133d8d6f52a7a5d30bc3bafe845a32 100644 (file)
@@ -1,20 +1,43 @@
-# This example is from the Perl::Tidy man page
-use Perl::Tidy;
+#!/usr/bin/perl -w
+
+# example call to perltidy from man page documentation of Perl::Tidy
 
-# some messy source code to format
-my $source = <<'EOM';
 use strict;
-my @editors=('Emacs', 'Vi   '); my $rand = rand();
-print "A poll of 10 random programmers gave these results:\n";
-foreach(0..10) {
-my $i=int ($rand+rand());
-print " $editors[$i] users are from Venus" . ", " . 
-"$editors[1-$i] users are from Mars" . 
-"\n";
+use Perl::Tidy;
+
+my $source_string = <<'EOT';
+my$error=Perl::Tidy::perltidy(argv=>$argv,source=>\$source_string,
+    destination=>\$dest_string,stderr=>\$stderr_string,
+errorfile=>\$errorfile_string,);
+EOT
+
+my $dest_string;
+my $stderr_string;
+my $errorfile_string;
+my $argv = "-npro";   # Ignore any .perltidyrc at this site
+$argv .= " -pbp";     # Format according to perl best practices
+$argv .= " -nst";     # Must turn off -st in case -pbp is specified
+$argv .= " -se";      # -se appends the errorfile to stderr
+## $argv .= " --spell-check";  # uncomment to trigger an error
+
+print "<<RAW SOURCE>>\n$source_string\n";
+
+my $error = Perl::Tidy::perltidy(
+    argv        => $argv,
+    source      => \$source_string,
+    destination => \$dest_string,
+    stderr      => \$stderr_string,
+    errorfile   => \$errorfile_string,    # not used when -se flag is set
+    ##phasers     => 'stun',                # uncomment to trigger an error
+);
+
+if ($error) {
+
+    # serious error in input parameters, no tidied output
+    print "<<STDERR>>\n$stderr_string\n";
+    die "Exiting because of serious errors\n";
 }
-EOM
 
-# We'll pass it as ref to SCALAR and receive it in a ref to ARRAY
-my @dest;
-perltidy( source => \$source, destination => \@dest );
-foreach (@dest) {print}
+if ($dest_string)      { print "<<TIDIED SOURCE>>\n$dest_string\n" }
+if ($stderr_string)    { print "<<STDERR>>\n$stderr_string\n" }
+if ($errorfile_string) { print "<<.ERR file>>\n$errorfile_string\n" }