| 1 | # Perform pdl demos on terminals | ||
| 2 | |||
| 3 | package PDL::Demos::Routines; | ||
| 4 | |||
| 5 | # Copyright (C) 1998 Tuomas J. Lukka. | ||
| 6 | # All rights reserved, except redistribution | ||
| 7 | # with PDL under the PDL License permitted. | ||
| 8 | |||
| 9 | use Carp; | ||
| 10 | use PDL; | ||
| 11 | |||
| 12 | @ISA="Exporter"; | ||
| 13 | @EXPORT = qw/comment act actnw output/; | ||
| 14 | |||
| 15 | $SIG{__DIE__} = sub {die Carp::longmess(@_);}; | ||
| 16 | |||
| 17 | sub home() { | ||
| 18 | if (-e '/usr/bin/tput') { | ||
| 19 | system 'tput clear'; | ||
| 20 | } elsif ( $^O eq 'MSWin32' ) { | ||
| 21 | system 'cls'; | ||
| 22 | } | ||
| 23 | } | ||
| 24 | |||
| 25 | sub comment($) { | ||
| 26 | home(); | ||
| 27 | print "----\n"; | ||
| 28 | print $_[0]; | ||
| 29 | my $prompt = "---- (press enter)"; | ||
| 30 | defined($PERLDL::TERM) ? $PERLDL::TERM->readline($prompt) : ( print $prompt, <> ); | ||
| 31 | } | ||
| 32 | |||
| 33 | sub act($) { | ||
| 34 | home(); | ||
| 35 | my $script = $_[0]; | ||
| 36 | $script =~ s/^(\s*)output/$1print/mg; | ||
| 37 | print "---- Code:"; | ||
| 38 | print $script; | ||
| 39 | print "---- Output:\n"; | ||
| 40 | my $pack = (caller)[0]; | ||
| 41 | # eval "package $pack; use PDL; $_[0]"; | ||
| 42 | eval "package $pack; use PDL; $_[0]"; | ||
| 43 | print "----\nOOPS!!! Something went wrong, please make a bug report!: $@\n----\n" if $@; | ||
| 44 | my $prompt = "---- (press enter)"; | ||
| 45 | defined($PERLDL::TERM) ? $PERLDL::TERM->readline($prompt) : ( print $prompt, <> ); | ||
| 46 | } | ||
| 47 | |||
| 48 | sub actnw($) { | ||
| 49 | home(); | ||
| 50 | my $script = $_[0]; | ||
| 51 | $script =~ s/^(\s*)output/$1print/mg; | ||
| 52 | print "---- Code:"; | ||
| 53 | print $script; | ||
| 54 | print "---- Output:\n"; | ||
| 55 | my $pack = (caller)[0]; | ||
| 56 | # eval "package $pack; use PDL; $_[0]"; | ||
| 57 | eval "package $pack; use PDL; $_[0]"; | ||
| 58 | print "----\n"; | ||
| 59 | print "----\nOOPS!!! Something went wrong, please make a bug report!: $@\n----\n" if $@; | ||
| 60 | } | ||
| 61 | |||
| 62 | sub output {print @_} |