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.bat < prev    next >
Encoding:
DOS Batch File  |  2003-09-17  |  10.2 KB  |  420 lines

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