📄 ch15_20.htm
字号:
<HTML><HEAD><TITLE>Recipe 15.19. Program: tkshufflepod (Perl Cookbook)</TITLE><METANAME="DC.title"CONTENT="Perl Cookbook"><METANAME="DC.creator"CONTENT="Tom Christiansen & Nathan Torkington"><METANAME="DC.publisher"CONTENT="O'Reilly & Associates, Inc."><METANAME="DC.date"CONTENT="1999-07-02T01:43:33Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-243-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="ch15_01.htm"TITLE="15. User Interfaces"><LINKREL="prev"HREF="ch15_19.htm"TITLE="15.18. Program: Small termcap program"><LINKREL="next"HREF="ch16_01.htm"TITLE="16. Process Management and Communication"></HEAD><BODYBGCOLOR="#FFFFFF"><img alt="Book Home" border="0" src="gifs/smbanner.gif" usemap="#banner-map" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Perl Cookbook"><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><p><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch15_19.htm"TITLE="15.18. Program: Small termcap program"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 15.18. Program: Small termcap program"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1"><ACLASS="chapter"REL="up"HREF="ch15_01.htm"TITLE="15. User Interfaces"></A></FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="chapter"HREF="ch16_01.htm"TITLE="16. Process Management and Communication"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 16. Process Management and Communication"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch15-25429">15.19. Program: tkshufflepod</A></H2><PCLASS="para">This short program uses <ACLASS="indexterm"NAME="ch15-idx-1000005219-0"></A><ACLASS="indexterm"NAME="ch15-idx-1000005219-1"></A><ACLASS="indexterm"NAME="ch15-idx-1000005219-2"></A><ACLASS="indexterm"NAME="ch15-idx-1000005219-3"></A><ACLASS="indexterm"NAME="ch15-idx-1000005219-4"></A>Tk to list the <CODECLASS="literal">=head1</CODE> sections in the file using the Listbox widget, and it lets you drag the sections around to reorder them. When you're done, press <CODECLASS="literal">"s"</CODE> or <CODECLASS="literal">"q"</CODE> to save or quit. You can even double-click on a section to view it with the Pod widget. It writes the section text to a temporary file in <EMCLASS="emphasis">/tmp</EM> and removes the file when the Pod widget is destroyed.</P><PCLASS="para">Call it with the name of the Pod file to view:</P><PRECLASS="programlisting">% tkshufflepod chap15.pod</PRE><PCLASS="para">We used this a lot when we wrote this book.</P><PCLASS="para">The program text is shown in <ACLASS="xref"HREF="ch15_20.htm#ch15-41772"TITLE="tkshufflepod">Example 15.10</A>.</P><DIVCLASS="example"><H4CLASS="example"><ACLASS="title"NAME="ch15-41772">Example 15.10: tkshufflepod</A></H4><PRECLASS="programlisting">#!/usr/bin/perl -w# <ACLASS="indexterm"NAME="ch15-idx-1000005016-0"></A>tkshufflepod - reorder =head1 sections in a pod fileuse Tk;use strict;# declare variablesmy $podfile; # name of the file to openmy $m; # main windowmy $l; # listboxmy ($up, $down); # positions to movemy @sections; # list of pod sectionsmy $all_pod; # text of pod file (used when reading)# read the pod file into memory, and split it into sections.$podfile = shift || "-";undef $/;open(F, "< $podfile") or die "Can't open $podfile : $!\n";$all_pod = <F>;close(F);@sections = split(/(?==head1)/, $all_pod);# turn @sections into an array of anonymous arrays. The first element# in each of these arrays is the original text of the message, while# the second element is the text following =head1 (the section title).foreach (@sections) { /(.*)/; $_ = [ $_, $1 ];}# fire up Tk and display the list of sections.$m = MainWindow->new();$l = $m->Listbox('-width' => 60)->pack('-expand' => 1, '-fill' => 'both');foreach my $section (@sections) { $l->insert("end", $section->[1]);}# permit dragging by binding to the Listbox widget.$l->bind( '<Any-Button>' => \&down );$l->bind( '<Any-ButtonRelease>' => \&up );# permit viewing by binding double-click$l->bind( '<Double-Button>' => \&view );# 'q' quits and 's' saves$m->bind( '<q>' => sub { exit } );$m->bind( '<s>' => \&save );MainLoop;# down(widget): called when the user clicks on an item in the Listbox.sub down { my $self = shift; $down = $self->curselection;;}# up(widget): called when the user releases the mouse button in the# Listbox.sub up { my $self = shift; my $elt; $up = $self->curselection;; return if $down == $up; # change selection list $elt = $sections[$down]; splice(@sections, $down, 1); splice(@sections, $up, 0, $elt); $self->delete($down); $self->insert($up, $sections[$up]->[1]);}# save(widget): called to save the list of sections.sub save { my $self = shift; open(F, "> $podfile") or die "Can't open $podfile for writing: $!"; print F map { $_->[0] } @sections; close F; exit;}# view(widget): called to display the widget. Uses the Pod widget.sub view { my $self = shift; my $temporary = "/tmp/$$-section.pod"; my $popup; open(F, "> $temporary") or warn ("Can't open $temporary : $!\n"), return; print F $sections[$down]->[0]; close(F); $popup = $m->Pod('-file' => $temporary); $popup->bind('<Destroy>' => sub { unlink $temporary } );<ACLASS="indexterm"NAME="ch15-idx-1000005214-0"></A><ACLASS="indexterm"NAME="ch15-idx-1000005214-1"></A><ACLASS="indexterm"NAME="ch15-idx-1000005214-2"></A><ACLASS="indexterm"NAME="ch15-idx-1000005214-3"></A><ACLASS="indexterm"NAME="ch15-idx-1000005214-4"></A>}<ACLASS="indexterm"NAME="ch15-idx-1000004995-0"></A><ACLASS="indexterm"NAME="ch15-idx-1000004995-1"></A></PRE></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch15_19.htm"TITLE="15.18. Program: Small termcap program"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 15.18. Program: Small termcap program"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="book"HREF="index.htm"TITLE="Perl Cookbook"><IMGSRC="../gifs/txthome.gif"ALT="Perl Cookbook"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="chapter"HREF="ch16_01.htm"TITLE="16. Process Management and Communication"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 16. Process Management and Communication"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">15.18. Program: Small termcap program</TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="index"HREF="index/index.htm"TITLE="Book Index"><IMGSRC="../gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228">16. Process Management and Communication</TD></TR></TABLE><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><FONTSIZE="-1"></DIV<!-- LIBRARY NAV BAR --> <img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p> <a href="copyrght.htm">Copyright © 2002</a> O'Reilly & Associates. All rights reserved.</font> </p> <map name="library-map"> <area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map> </BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -