Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Shortcuts Engine: Packaged Version

by munchie (Monk)
on Apr 06, 2002 at 21:42 UTC ( #157183=sourcecode: print w/replies, xml ) Need Help??
Category: Text Processing
Author/Contact Info Steven Rubin (munchie)
Description: This is the shortcuts engine I made put into packaged format. It is now more portable, and offers more flexibility. I have never submitted anything to CPAN, so I want fellow monks' opinion on wheter or not this module is ready for submission to CPAN.

This module requires Text::xSV by tilly. If you have any suggestions on making this better, please speak up.

UPDATE 1: Took out the 3Arg open statements for slightly longer 2Args, to make sure that older versions of Perl will like my program. (thanks to crazyinsomniac)

UPDATE 2: I just uploaded this on PAUSE, so very soon you'll all be able to get it on CPAN! (My first module!)

# Made by Steven Rubin (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";
    return $self;
sub set_shortcut {
    my $self = shift;
    my $is_taken=0;
    my $char = shift;
    my $text = shift;
    my $csv = new Text::xSV;
    while ($csv->get_row()) {
        my ($shortcut) = $csv->extract(qw(shortcut));
        if ($char eq $shortcut) {
            warn "Shortcut character already used!";
    unless ($is_taken) {
                my $file = $self->{SF};
        open(SF, ">>$file");
        print SF "$char,$text\n";
sub get_shortcuts {
    my $self = shift;

    my $csv = new Text::xSV;
    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;
        while ($csv->get_row()) {
            my ($shortcut, $output) = $csv->extract(qw(shortcut output
            $shortcuts{$shortcut} = $output;
        $line =~ s/\[(\w)\]+/$shortcuts{$1}/g;
        print OF $line;
sub get_doc {
    my $self = shift;

        my $file = $self->{OF};
    open(OF, "<$file");
    while (<OF>) {


=head1 NAME

Text::Shortcuts - A shortcut creation & usage engine


    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";
        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) {
        } elsif ($choice == 3) {
            print "type 'end' to end\n";
        } elsif ($choice == 4) {


This module is for use in creating a shortcuts engine. Shortcuts engin
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
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>, 
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
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

=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.


=head1 BUGS

None known yet. Email me at if any are found.

=head1 AUTHOR

Steven S. Rubin (


Copyright 2002.  This may be modified and distributed on the same
terms as Perl.
Replies are listed 'Best First'.
C-h i g (xemacs)Abbrevs
by Fletch (Chancellor) on Apr 07, 2002 at 22:05 UTC

    Both x?emacs and vim offer similar `abbrev' modes that operate inline (i.e. when you type it it's expanded immediately). Check the info file for x?emacs (in xemacs's the node is (xemacs)Abbrevs, and vim's :help map.txt for its abbrevs functions.

      That's very cool, but even if I had known about, I still wouldv'e made this module.

    • 1: It's part of a note taking suite I'm making for my dad.
    • 2: I don't use emacs or vim, and I'm sure there are many others like me that may be able to benefit from this module.

      > munchie, the number munchin newb
      Llama: The other other white meat!
      (you had to be there :-P)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://157183]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2020-10-27 14:02 GMT
Find Nodes?
    Voting Booth?
    My favourite web site is:

    Results (256 votes). Check out past polls.