home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / xampp / xampp-perl-addon-1.4.9-installer.exe / ysh < prev    next >
Encoding:
Text File  |  2003-09-17  |  9.7 KB  |  404 lines

  1. #!perl -w
  2.  
  3. $VERSION = '0.35';
  4. $perl_version = '5.8.0';
  5.  
  6.  
  7. =head1 NAME
  8.  
  9. ysh - The YAML Test Shell
  10.  
  11. =head1 SYNOPSIS
  12.  
  13.  ysh [options]
  14.  
  15. =head1 DESCRIPTION
  16.  
  17. This program is designed to let you play with the YAML.pm module in
  18. an interactive way. When you to type in Perl, you get back YAML. And
  19. vice versa.
  20.  
  21. By default, every line you type is a one line Perl program, the return
  22. value of which will be displayed as YAML.
  23.  
  24. To enter multi-line Perl code start the first line with ';' and use as
  25. many lines as needed. Terminate with a line containing just ';'.
  26.  
  27. To enter YAML text, start with a valid YAML separator/header line
  28. which is typically '---'. Use '===' to indicate that there is no YAML
  29. header. Enter as many lines as needed. Terminate with a line
  30. containing just '...'.
  31.  
  32. To read in and process an external YAML file, enter '< filename'. The
  33. ysh will also work as a standalone filter. It will read anything on
  34. STDIN as a YAML stream and write the Perl output to STDOUT. You can say
  35. (on most Unix systems):
  36.  
  37.     cat yaml.file | ysh | less
  38.  
  39. =head1 COMMAND LINE OPTIONS
  40.  
  41. =over 4
  42.  
  43. =item -l
  44.  
  45. Keep a log of all ysh activity in './ysh.log'. If the log file already
  46. exists, new content will be concatenated to it.
  47.  
  48. =item -L
  49.  
  50. Keep a log of all ysh activity in './ysh.log'. If the log file already
  51. exists, it will be deleted first.
  52.  
  53. =item -r
  54.  
  55. Test roundtripping. Every piece of Perl code entered will be Dumped,
  56. Loaded, and Dumped again. If the two stores do not match, an error
  57. message will be reported.
  58.  
  59. =item -R
  60.  
  61. Same as above, except that a B<confirmation> message will be printed
  62. when the roundtrip succeeds.
  63.  
  64. =item -i<number>
  65.  
  66. Specify the number of characters to indent each level. This is the same
  67. as setting $YAML::Indent.
  68.  
  69. =item -ub
  70.  
  71. Shortcut for setting '$YAML::UseBlock = 1'. Force multiline scalars to
  72. use 'block' style.
  73.  
  74. =item -uf
  75.  
  76. Shortcut for setting '$YAML::UseFold = 1'. Force multiline scalars to
  77. use 'folded' style.
  78.  
  79. =item -uc
  80.  
  81. Shortcut for setting '$YAML::UseCode = 1'. Allows subroutine references
  82. to be processed.
  83.  
  84. =item -nh
  85.  
  86. Shortcut for setting '$YAML::UseHeader = 0'.
  87.  
  88. =item -nv
  89.  
  90. Shortcut for setting '$YAML::UseVersion = 0'.
  91.  
  92. =item -v
  93.  
  94. Print the versions of ysh and YAML.pm.
  95.  
  96. =item -V
  97.  
  98. In addition to the -v info, print the versions of YAML related modules.
  99.  
  100. =item -h
  101.  
  102. Print a help message.
  103.  
  104. =back
  105.  
  106. =head2 YSH_OPT
  107.  
  108. If you don't want to enter your favorite options every time you enter
  109. ysh, you can put the options into the C<YSH_OPT> environment variable.
  110. Do something like this:
  111.  
  112.     export YSH_OPT='-i3 -uc -L'
  113.  
  114. =head1 SEE ALSO
  115.  
  116. L<YAML>
  117.  
  118. =head1 AUTHOR
  119.  
  120. Brian Ingerson <ingy@cpan.org>
  121.  
  122. =head1 COPYRIGHT
  123.  
  124. Copyright (c) 2001, 2002. Brian Ingerson. All rights reserved.
  125.  
  126. This program is free software; you can redistribute it and/or modify it
  127. under the same terms as Perl itself.
  128.  
  129. See L<http://www.perl.com/perl/misc/Artistic.html>
  130.  
  131. =cut
  132.  
  133. use strict;
  134.  
  135. use Term::ReadLine;
  136. sub Term::ReadLine::Perl::Tie::FIRSTKEY {undef}
  137. use YAML;
  138. use Data::Dumper;
  139. $Data::Dumper::Indent = 1;
  140. use vars qw($prompt);
  141. $prompt = 'ysh > ';
  142. my $round_trip = 0;
  143. my $force = 0;
  144. my $log = 0;
  145. $| = 1;
  146. my @env_args = split /\s+/, ($ENV{YSH_OPT} || '');
  147. my @args = (@env_args, @ARGV);
  148. my $stream = -t STDIN ? '' : join('', <STDIN>);
  149.  
  150. while (my $arg = shift @args) {
  151.     handle_help(), exit if $arg eq '-h';
  152.     handle_version(), exit if $arg eq '-v';
  153.     handle_Version(), exit if $arg eq '-V';
  154.     $YAML::Indent = $1, next if $arg =~ /^-i(\d+)$/;
  155.     $YAML::UseFold = 1, next if $arg eq '-uf';
  156.     $YAML::UseBlock = 1, next if $arg eq '-ub';
  157.     $YAML::UseCode = 1, next if $arg eq '-uc';
  158.     $YAML::UseHeader = 0, next if $arg eq '-nh';
  159.     $YAML::UseVersion = 0, next if $arg eq '-nv';
  160.     $round_trip = 1, next if $arg eq '-r';
  161.     $round_trip = 2, next if $arg eq '-R';
  162.     $log = 1, next if $arg eq '-l';
  163.     $log = 2, next if $arg eq '-L';
  164.     $force = 1, next if $arg eq '-F';
  165.     warn(<<END), exit; 
  166. Unknown YAML Shell argument: '$arg'.
  167. For help, try: perldoc ysh
  168. END
  169. }
  170.  
  171. check_install() unless $force;
  172.  
  173. if ($log) {
  174.     if ($log == 2) {
  175.         open LOGFILE, "> ./ysh.log" or die $!;
  176.     }
  177.     else {
  178.         open LOGFILE, ">> ./ysh.log" or die $!;
  179.     }
  180.     print LOGFILE "\nYAML.pm Version $YAML::VERSION\n";
  181.     print LOGFILE "Begin logging at ", scalar localtime, "\n\n";
  182. }
  183.  
  184. sub Print {
  185.     print @_;
  186.     print LOGFILE @_ if $log;
  187. }
  188. local $SIG{__WARN__} = sub { Print @_ };
  189.  
  190. if (not length($stream)) {
  191.     Print <<END;
  192. Welcome to the YAML Test Shell. Type ':help' for more information.
  193.  
  194. END
  195. }
  196.  
  197. {
  198.     my $sh;
  199.     {
  200.         local @ENV{qw(HOME EDITOR)};
  201.         local $^W;
  202.         $sh = Term::ReadLine::->new('The YAML Shell');
  203.     }
  204.  
  205.     sub my_readline {
  206.         print LOGFILE $prompt if $log;
  207.     my $input = $sh->readline($prompt);
  208.     if (not defined $input) {
  209.         $input = ':exit';
  210.         Print "\n";
  211.     }
  212.     $input .= "\n";
  213.     }
  214. }
  215.  
  216. if (length($stream)) {
  217.     my @objects;
  218.     eval { @objects = YAML::Load($stream) };
  219.     if ($@) {
  220.         print STDERR $@;
  221.     }
  222.     else {
  223.         print STDOUT Data::Dumper::Dumper(@objects);
  224.     }
  225.     exit 0;
  226. }
  227.  
  228. while ($_ = my_readline()) {
  229.     print LOGFILE $_ if $log;
  230.     next if /^\s*$/;
  231.     exec('ysh', @ARGV) if /^\/$/;
  232.     handle_command($_),next if /^:/;
  233.     handle_file($1),next if /^<\s*(\S+)\s*$/;
  234.     handle_yaml($_),next if /^--\S/;
  235.     handle_yaml(''),next if /^===$/;
  236.     handle_perl($_,1),next if /^;/;
  237.     handle_perl($_,0),next;
  238.     Print "Unknown command. Type ':help' for instructions.\n";
  239. }
  240.  
  241. sub handle_file {
  242.     my ($file) = @_;
  243.     my @objects;
  244.     eval { @objects = YAML::LoadFile($file) };
  245.     if ($@) {
  246.         Print $@;
  247.     }
  248.     else {
  249.         Print Data::Dumper::Dumper(@objects);
  250.     }
  251. }
  252.     
  253. sub handle_perl {
  254.     my ($perl, $multi) = @_;
  255.     my (@objects, $yaml, $yaml2);
  256.     local $prompt = 'perl> ';
  257.     my $line = '';
  258.     if ($multi) {
  259.         while ($line !~ /^;$/) {
  260.             $line = my_readline();
  261.             print LOGFILE $line if $log;
  262.             $perl .= $line;
  263.         }
  264.     }
  265.     @objects = eval "no strict;$perl";
  266.     Print("Bad Perl expression:\n$@"), return if $@;
  267.     eval { $yaml = Dump(@objects) };
  268.     $@ =~ s/^ at.*\Z//sm if $@;
  269.     Print("Dump failed:\n$@"), return if $@;
  270.     Print $yaml;
  271.     if ($round_trip) {
  272.         {
  273.             local $SIG{__WARN__} = sub {};
  274.             eval { $yaml2 = Dump(Load($yaml)) };
  275.         }
  276.         $@ =~ s/^ at.*\Z//sm if $@;
  277.         Print("Load failed:\n$@"), return if $@;
  278.         if ($yaml eq $yaml2) {
  279.             if ($round_trip > 1) {
  280.                 Print "\nData roundtripped OK!!!\n";
  281.             }
  282.         }
  283.         else {
  284.             Print "================\n";
  285.             Print "after roundtrip:\n";
  286.             Print "================\n";
  287.             # $yaml2 =~ s/ /_/g;  #
  288.             # $yaml2 =~ s/\n/+/g; #
  289.             # Print $yaml2, "\n"; #
  290.             Print $yaml2;
  291.             Print "=========================\n";
  292.             Print "Data did NOT roundtrip...\n";
  293.         }
  294.     }
  295. }
  296.  
  297. sub handle_yaml {
  298.     my $yaml = shift;
  299.     my $line = $yaml;
  300.     my (@objects);
  301.     local $prompt = 'yaml> ';
  302.     $line = my_readline();
  303.     print LOGFILE $line if $log;
  304.     $line = '' unless defined $line;
  305.     while ($line !~ /^\.{3}$/) {
  306.         $yaml .= $line;
  307.         $line = my_readline();
  308.         print LOGFILE $line if $log;
  309.         last unless defined $line;
  310.     }
  311.     $yaml =~ s/\^{2,8}/\t/g;
  312.     eval { @objects = Load($yaml) };
  313.     $@ =~ s/^ at.*\Z//sm if $@;
  314.     $@ =~ s/^/  /gm if $@;
  315.     Print("YAML Load Failed:\n$@"), return if $@;
  316.     Print Data::Dumper::Dumper(@objects);
  317. }
  318.  
  319. sub handle_command {
  320.     my $line = shift;
  321.     chomp $line;
  322.     my ($cmd, $args);
  323.     if ($line =~ /^:(\w+)\s*(.*)$/) {
  324.         $cmd = $1;
  325.         $args = $2;
  326.         exit if $cmd =~ /^(exit|q(uit)?)$/;
  327.         handle_help(),return if $cmd eq 'help';
  328.         print `clear`,return if $cmd =~ /^c(lear)?$/;
  329.     }
  330.     Print "Invalid command\n";
  331. }
  332.  
  333. sub handle_help {
  334.     Print <<END;
  335.                       Welcome to the YAML Test Shell.
  336.  
  337.    When you to type in Perl, you get back YAML. And vice versa.
  338.  
  339.    By default, every line you type is a one line Perl program, the
  340.    return value of which will be displayed as YAML.
  341.  
  342.    To enter multi-line Perl code start the first line with ';' and use
  343.    as many lines as needed. Terminate with a line containing just ';'.
  344.  
  345.    To enter YAML text, start with a valid YAML separator/header line
  346.    which is typically '---'. Use '===' to indicate that there is no YAML
  347.    header. Enter as many lines as needed. Terminate with a line
  348.    containing just '...'.
  349.  
  350.    Shell Commands:             (Begin with ':')
  351.       :exit or :q(uit) - leave the shell
  352.       :help - get this help screen
  353.  
  354. END
  355. }
  356.  
  357. sub check_install {
  358.     if (-f "./YAML.pm" && -f "./pm_to_blib" &&
  359.         -M "./YAML.pm" <  -M "./pm_to_blib"
  360.        ) {
  361.         die "You need to 'make install'!\n";
  362.     }
  363. }
  364.  
  365. sub handle_version {
  366.     print STDERR <<END;
  367.  
  368. ysh: '$main::VERSION'
  369. YAML: '$YAML::VERSION'
  370.  
  371. END
  372. }
  373.  
  374. sub handle_Version {
  375.     my $TRP = get_version('Term::ReadLine::Perl');
  376.     my $TRG = get_version('Term::ReadLine::Gnu');
  377.     my $POE = get_version('POE');
  378.     my $TO = get_version('Time::Object');
  379.  
  380.     print STDERR <<END;
  381.  
  382. ysh: '$main::VERSION'
  383. YAML: '$YAML::VERSION'
  384. perl: '$main::perl_version'
  385. Data::Dumper: '$Data::Dumper::VERSION'
  386. Term::ReadLine::Perl: '$TRP'
  387. Term::ReadLine::Gnu: '$TRG'
  388. POE: '$POE'
  389. Time::Object: '$TO'
  390.  
  391. END
  392. }
  393.  
  394. sub get_version {
  395.     my ($module) = @_;
  396.     my $version;
  397.     eval "no strict; use $module; \$version = \$${module}::VERSION";
  398.     #$version = "$@" if $@;
  399.     $version = "not installed" if $@;
  400.     return $version;
  401. }
  402.  
  403. 1;
  404.