Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Using perl to teach programming

by raptor (Sexton)
on Feb 05, 2001 at 13:03 UTC ( [id://56390]=note: print w/replies, xml ) Need Help??


in reply to Using perl to teach programming

hi Falkkin, all,
I read your script ...cool :") and here are some extentioon I've written, plus some docs (perldoc tutorial.pl).
And here is the rewriten code ... enjoy...
All changes are marked with comment #*changed AND all new sub's with #*added
#!/usr/bin/perl use strict; use Term::ReadLine; my $term = new Term::ReadLine 'Perl Tutor'; my $OUT = $term->OUT || *STDOUT; print "Welcome to the Perl Tutor!\n\n"; print_commands(); my $undo_level = 250; my @undo_data; my @redo_data; # It will be cool if someone extend this so we can create interactive # tutorials...:") ..I don't have much time at the moment and I'm lazy + :") #... f.e. www.perl.com had 6 articles for the beginers... # before a couple of weeks .. # if tutorial mode extract next item from .tut file # and do what the teacher say.. ;') # expect user to do something, correct him if wrong...etc.. # IDEAS ?!?! what about including MS Office wizards :") he he LINE: while (1) { $_ = $term->readline("> "); chomp; help(), next LINE if /^h$/i || /^help/i; vars(), next LINE if /^v$/i || /^vars/i; block(), next LINE if /^b$/i || /^block/i; load(), next LINE if /^l$/i || /^load/i; clear(), next LINE if /^c$/i || /^clear/i; undo(), next LINE if /^u$/i || /^undo/i; re_do(), next LINE if /^r$/i || /^redo/i; #*added printBlock($1), next LINE if /^bp.*$/i; evalBlock($1), next LINE if /^be.*$/i; editLineInBlock($1), next LINE if /^eb.*$/i; delBlock($1), next LINE if /^bd.*$/i; loadBlock($1), next LINE if /^bl.*$/i; execOnFile($1), next LINE if /^e\|.*$/i; exit() if /^q$/i || /^quit/i; evaluate($_); } sub print_commands { print <<'DONE'; Special commands are: (H)elp Get online help. (L)oad Load and evaluate the contents of the file. (V)ars Look at the present values of variables. (C)lear Clears the values of all variables and removes all undo dat +a. (B)lock Type in more than one line of code at a time. (bp$ID)Block print -=> print block with ID OR all (be$ID)Block evaluate -=> evaluate block with ID (bd$ID)Block delete -=> delete block with ID (bl$ID)Block load -=> load file into block (eb$ID|$line)Edit Block -=> Edit block ID - line $line (E|$fname)xec on File -=> execute the commands that follow on file - +$fname (U)ndo Undo last Perl command. (R)edo Redo the last `undo' command. (Q)uit Quit the Perl Tutor. Anything which is not a special command will be evaluated as Perl code +. DONE } #*chnaged sub load { my $inBlock = shift; my $filename = $term->readline("Enter a filename: "); open (FILE, $filename) || return print "Could not load file $filenam +e: $!\n"; my @data = <FILE>; close(FILE); my $data = join "\n", @data; print "File $filename loaded successfully.\n"; if ($inBlock) { $Blocks::{"id".$inBlock} = $data } else { evaluate($ +data) }; } #*changed sub block { print "Entering Block Mode.\nEnter in as many lines of code as you w +ant; press Ctrl-D or Ctrl-Z when done.\n\n"; my @input = <STDIN>; my $input = join "\n", @input; $Blocks::count++; $Blocks::{"id".$Blocks::count} = join "", @input;; evaluate($input); } #*added sub printBlock { (my $id) = $_ =~ /(\d+)/; if ($id) { if ($Blocks::{"id".$id}) { print qq{$Blocks::{"id".$id}\n} } else { print "Block with ID : $id doesn't exists\n" }; return }; for my $k (sort grep {/^id/} keys %Blocks:: ) { print qq{Block ID : $k\n$Blocks::{$k}\n#=======----------|\n +} }; }; #*added sub evalBlock { (my $id) = $_ =~ /(\d+)/; if ( $id && $Blocks::{"id".$id}) { evaluate($Blocks::{"id".$id}) ; print "Block $id evaluated successfully\n" } else { print "Nothing evaluated, may be block with that ID doe +sn't exists\n" }; }; #*added sub delBlock { (my $id) = $_ =~ /(\d+)/; delete $Blocks::{"id".$id} if $id; }; #*added sub loadBlock { (my $id) = $_ =~ /(\d+)/; load($id) if $id; }; #*added, someone to write the EDITOR :") or we have to use temporary f +ile !!! # think also for Windows :"( sub editLineInBlock { (my ($id,$line)) = $_ =~ /eb(\d+?)\|(\d+)/; return unless $id; my @block = split "\n", $Blocks::{"id".$id}; print "Block $id old Line number $line:\n$block[$line]\n"; my $newline = $term->readline("New line content : "); $block[$line] = $newline; $Blocks::{"id".$id} = join "\n", @block; print "Line updated...\n"; }; #*added sub execOnFile() { no strict; (my ($fname,$rest)) = $_ =~ /^e\|([^\s]+?)\s+?(.*)$/; unless ($fname || $rest ) {print "No filename/action specified !!! +\n"; return}; # I'm not sure where is better to store the filename in MAIN or i +n WORKSPACE ?!! # 'cause WorkSpace is cleared... but until U deicde let it be ... +. if ($fname =~ /^\$(.*)/) { $fname = ${$WorkSpace::{$1}}; unless ($fname) {print "Variable \$$1 which is supossed to con +tain the filename is not defined\n"; return} }; my $action = eval "sub { $rest }"; save_state(); package WorkSpace; open FILE, "<$fname"; while (<FILE>) { &$action($_) }; close FILE; print "\n"; package main; }; sub clear { %WorkSpace:: = (); @undo_data = (); print "Successfully cleared all variables and removed all undo data. +\n" } sub help { print "Eventually, a nice help message will go here.\n\n"; print_commands(); } sub quote_strings { my @return_val; foreach (@_) { my $scalar = $_; $scalar = "\"$_\"" unless $scalar =~ /^[0-9.]+$/; push @return_val, $scalar; } return @return_val if wantarray; return $return_val[0]; } sub undo { save_state(); my $current_state = pop @undo_data; my $undo_state = pop @undo_data; return print "Could not undo, because there is no undo data availabl +e.\n" unless $undo_state; push @redo_data, $current_state; my %state = %{ $undo_state }; %WorkSpace:: = %state; print "Successfully executed `undo' command.\n"; } sub re_do { my $state_ref = pop @redo_data; return print "Could not redo, because there is no redo data availabl +e.\n" unless $state_ref; push @undo_data, $state_ref; shift @undo_data if @undo_data > $undo_level; my %state = %{ $state_ref }; %WorkSpace:: = %state; print "Successfully executed `redo' command.\n"; } sub save_state { my %saved_data; foreach my $key (keys %WorkSpace::) { $saved_data{$key} = $WorkSpace::{$key}; } push @undo_data, \%saved_data; shift @undo_data if @undo_data > $undo_level; } no strict; sub evaluate { save_state(); package WorkSpace; $_ = shift; eval($_); print "Unrecognized command or syntax error. (Type `help' for help.) +" if $@; print "\n"; package main; } sub vars { package WorkSpace; my $varcount = 0; foreach my $symname (sort keys %WorkSpace::) { local *sym = $WorkSpace::{$symname}; if (defined $sym) { $varcount++; my $scalar = main::quote_strings($sym); print "\$$symname = $scalar\n"; } if (defined @sym) { $varcount++; my @array = main::quote_strings(@sym); print "\@$symname = ("; print join(", ", @array); print ")\n"; } if (defined %sym) { $varcount++; print "\%$symname = ("; my $output; foreach my $key (sort keys %sym) { my $value = main::quote_strings($sym{$key}); $output .= "$key => $value, "; } chop $output; chop $output; print "$output)\n"; } if (defined &sym) { $varcount++; print "sub $symname { ... }\n"; } } print "No variables are currently defined.\n" unless $varcount; package main; } #*added to remembering U that it exists :") BEGIN { $WorkSpace::f = "block.html"; }; __END__ =head1 DESCRIPTION ... very cool idea briliant realization (that's why Perl ROCKS!! :"), I have also to teach some people on PERL, so ....blah, blah .... write something here :") .... Hey, lazy people use BEGIN{} block to add code that you want to be executed every time, config data etc... =head2 EXAMPLES e|c:\test.txt print -=> print c:\test.txt line by line. The "e|filename actions" command excutes actions U specified on every line of the file. [ the same like : while(<FILE>) {...actions.. }. In the $_ as U may expect :") is current line ] e|$filename s/<.*?>/g;print -=> strip HTML tags from the file pointed by the var $filename (if U are lazy like me, don't type the filename every time :") ) bp -=> every time when U execute "b"/"block" command or load file into block with "bl" this block of code is stored in Blocks package as string. So with "bp" all blocks are printed to STDOUT. bp3 -> print block number 3. The block numbers begin with 1 and are automaticaly incremented after creating new block. bl -=> load file into block. The file is not evaluated as this happen when U load file with "l"/"load" command. -> bl5 - load the file into block number 5 bd7 -=> delete block number 7. be2 -=> once U have block loaded/created U can evaluate it. eb2|4 -=> replace the line 4 from block 2 with the text U enter. =cut

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://56390]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-04-19 05:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found