# Made by Steven Rubin steven@ssrubin.com (April 2002) package Text::Shortcuts; $VERSION = 0.02; use strict; use Text::xSV; sub new { my $invocant = shift; my $class = ref($invocant) || $invocant; my $self = { SF => shift, OF => shift }; bless($self, $class); unless (-s $self->{SF}) { my $file = $self->{SF}; open(SF, ">>$file"); print SF "shortcut,output\n"; close(SF); } return $self; } sub set_shortcut { my $self = shift; my $is_taken=0; my $char = shift; my $text = shift; my $csv = new Text::xSV; $csv->open_file($self->{SF}); $csv->bind_header(); while ($csv->get_row()) { my ($shortcut) = $csv->extract(qw(shortcut)); if ($char eq $shortcut) { warn "Shortcut character already used!"; $is_taken=1; last; } } unless ($is_taken) { my $file = $self->{SF}; open(SF, ">>$file"); print SF "$char,$text\n"; close(SF); } } sub get_shortcuts { my $self = shift; my $csv = new Text::xSV; $csv->open_file($self->{SF}); $csv->bind_header(); while ($csv->get_row()) { my ($shortcut, $output) = $csv->extract(qw(shortcut output)); print "\[$shortcut\] produces $output\n"; } } sub new_doc { my $self = shift; my $file = $self->{OF}; open(OF, ">>$file"); while (my $line = <STDIN>) { last if $line =~ /^(end)$/; my %shortcuts; my $csv = new Text::xSV; $csv->open_file($self->{SF}); $csv->bind_header(); while ($csv->get_row()) { my ($shortcut, $output) = $csv->extract(qw(shortcut output +)); $shortcuts{$shortcut} = $output; } $line =~ s/\[(\w)\]+/$shortcuts{$1}/g; print OF $line; } close(OF); } sub get_doc { my $self = shift; my $file = $self->{OF}; open(OF, "<$file"); while (<OF>) { print; } close(OF); } 1; __END__ =head1 NAME Text::Shortcuts - A shortcut creation & usage engine =head1 SYNOPSIS use Text::Shortcuts; my $sc = Text::Shortcuts->new($shortcut_file, $output_file); while(1) { print "Choose option:\n \t1. Set Shortcut\n \t2. Get Shortcuts\n \t3. Start Note\n \t4. Read Note\n"; print "Choice: "; chomp(my $choice = <STDIN>); unless ($choice == 1 || $choice == 2 || $choice == 3 || #--Quick Gettaway $choice == 4) { print "Invalid Choice: $choice"; redo; } if ($choice == 1) { print "What letter do you want to make this shortcut? "; chomp (my $letter = <STDIN>); print "What do you want \[$letter\] to produce?\n"; chomp (my $produce = <STDIN>); $sc->set_shortcut($letter, $produce); } elsif ($choice == 2) { $sc->get_shortcuts; } elsif ($choice == 3) { print "type 'end' to end\n"; $sc->new_doc; } elsif ($choice == 4) { $sc->get_doc; } } =head1 DESCRIPTION This module is for use in creating a shortcuts engine. Shortcuts engin +e? you ask. Text::Shortcuts lets you define shortcuts (such as [j] or [R] +) and then lets you write documents using your shortcuts to stand for longer pieces of text. For example, you might set [y] to equal Yoonsdock, Mars. When you crea +te a new document using C<new_doc>, it will change all occurences of [y] in that document to Yoonsdock, Mars. This modules allows for creating and viewing of shortcuts, and creatin +g and viewing of documents. When a document is typed using C<$sc->new_doc>, +the module sends the I<real> text to the output file, not the shortcut. =head1 USAGE =over 4 =item C<new($shortcut_file, $output_file)> This is the constructor method. It must have the args for the file tha +t contains the shortcut definitions, and the file that the output will g +o to. =item C<set_shortcut($shortcut_letter, $shortcut_expanded)> This method sets shortcuts, so [$shortcut_letter] will acess $shortcut +_expanded. =item C<get_shortcuts> This method print out a list of every shortcut, and what it produces. =item C<new_doc> This method allows you to type to your output file using shortcuts you've defined earlier. =item C<get_doc> A method for people who are too lazy to C<open> their output file. It simply opens the output file and prints what's in it. =back =head1 BUGS None known yet. Email me at steven@ssrubin.com if any are found. =head1 AUTHOR Steven S. Rubin (steven@ssrubin.com) =head1 COPYRIGHT Copyright 2002. This may be modified and distributed on the same terms as Perl.

In reply to Shortcuts Engine: Packaged Version by munchie

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.