Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Storable 2 Text - An editor for data files created by Storable.pm

by kingman (Scribe)
on Aug 08, 2002 at 13:57 UTC ( #188588=sourcecode: print w/ replies, xml ) Need Help??

Category: Utility Scripts
Author/Contact Info Kingsley Gordon - kingman@ncf.ca
Description: Opens a file created by Storable::lock_store and dumps the data structure to an ascii file for viewing/editing.

Also creates a file via Storable::lock_store that contains an empty hash if you create a symlink to the script called ts (touch store).

#!/usr/bin/perl -w
use strict;
use Data::Dump qw(dump);
use Storable qw(lock_retrieve lock_store);
my %usage;
 
(my $script_name = $0) =~ s#.*/##;  # $0 = full path to script
my $file = $ARGV[0] || &usage($script_name);
 
s2t($file) if $script_name eq 's2t';
ts($file) if $script_name eq 'ts';
 
### Subs ###
 
sub s2t { # s2t (Storable 2 Text) expects a file created by Storable::
+lock_store.
    my $file = shift;
    my $data_struct = lock_retrieve($file) or die "Can't open $file: $
+!";
    my $data = dump($data_struct);
    open(FH, ">$file.txt");                     # Create new file by a
+ppending '.txt' to the original filename.
    print FH "use Storable qw(lock_store);\n";  # Make it easy to upda
+te original file created by Storable::lock_store.
    print FH '$x =';
    print FH "$data";
    print FH ";\n";
    print FH "lock_store (\$x, \'$file\');\n";
    close FH;
    exit;
};
 
sub ts { # ts (Touch Storable) creates a file with Storable::lock_stor
+e that contains an EMPTY hash reference.
    my $file = shift;
    my $x = {};
    lock_store $x, $file or die "Can't create $file: $!";
    exit;
};
 
sub usage  {
my $script_name = shift;
my %s;
$s{s2t} = <<END;
 
Opens a file created by Storable::lock_store and dumps the data struct
+ure to an
ascii file for viewing/editing.
 
Usage: $script_name [file]
 
END
 
$s{ts} = <<END;
 
Creates a file with Storable::lock_store that contains an EMPTY hash r
+eference.
 
Usage: $script_name [file]
 
END
 
print (exists $s{$script_name} ? $s{$script_name} : "You must rename t
+his script to either s2t or ts.\n"); 
exit;
}
 

=pod
=head1 NAME
    s2t or ts (One is symlinked to the other.)
 
=head1 SYNOPSIS
 
s2t [file] B<or> ts  [file]
 
=head1 DESCRIPTION
 
A command-line script to work with Perl data structures created by Sto
+rable.pm
 
This file should be called B<s2t> and a file called B<ts> should be sy
+mlinked
to it like so:
 
    ln -s s2t ts
 
The script consists of two subroutines, one of which is called dependi
+ng on the
scriptname.
 
=head1 s2t (Storable 2 Text)
 
Opens a file created by Storable::lock_store and dumps the data struct
+ure to an
ascii file for viewing/editing.  The extension '.txt' is appended to t
+he end of
the new file to indicate that it's editable. (Storable.pm creates bina
+ry files
which load quickly, but aren't editable.)
 
When you've finished editing the .txt file, you should run it through 
+perl to
re-create the original Storable file.  There are a couple of different
+ ways to
do this:
 
=over 4
 
=item * B<From Vi>
 
 :%!perl
 
=item * B<From the command line>
 
 perl foobar.txt
 
=back
 
=head1 ts (Touch Storable)
 
    Creates a file with Storable::lock_store that contains an EMPTY ha
+sh reference.
 
    Some scripts try to open files created by Storable::lock_store. Th
+is creates those files in the right format.
 
=head1 AUTHOR
 
Kingsley Gordon -  E<lt>kingman@ncf.caE<gt>
 
 last modified: Thu Aug 8 2002
 
=cut

Comment on Storable 2 Text - An editor for data files created by Storable.pm
Download Code
Replies are listed 'Best First'.
Re: Storable 2 Text - An editor for data files created by Storable.pm
by PodMaster (Abbot) on Aug 09, 2002 at 05:43 UTC
    I'll pretend I did not see
    ### POD ###
    I'd like to introduce you to
    =pod
    I trust you'll make the change accordingly.

    Seeing as you're on the right track ( pod ), but are still have a little trouble( sub usage ... <<END; ??? nooo ), i'd like to introduce you to a new friend, Pod::Usage. It'll make the redundant sub usage go away.

    I hope you'll embrace it, and to help you do so, here's a gem from ybic The Dynamic Duo --or-- Holy Getopt::Long, Pod::UsageMan!

    Ooops, I almost forgot, ;D

    ____________________________________________________
    ** The Third rule of perl club is a statement of fact: pod is sexy.

      Ok, I'm trying to fix up the Pod as you suggested. But I'm not sure how to get the same functionality as my &usage hack. The script is designed to be symlinked:
      (my $script_name = $0) =~ s#.*/##; # $0 = full path to script my $file = $ARGV[0] || &usage($script_name);
      And the usage message is related to the name of the script. Is there anyway to do this with Pod::Usage? Is the symlink design of this utility bad?
        It didn't catch my eye the first time, but don't trust $0. It's a bad shell scripting habit. use __FILE__ instead. Sure you can't embed it in string unless you use "@{[__FILE__]}" but it's just the same, it can't be changed, while $0 can.

        Now, Pod::Usage's pod2usage function does look at $0 if you don't pass the -input option, but that's cause __FILE__ is local to each file, and it wouldn't work otherwise, and you can always say pod2usage( -input => __FILE__ );.

        If you symlink a file as 'FOO', __FILE__ will contain that value, although i'm not sure how portable that is (i'm willing to venture a guess that it is).

        Now to your new question, yes, the symlink design of the utility is bad IMHO. Either create 2 separate scripts, or just make each Sub an option (with Getopt)

        And while i'm at it, you shouldn't exit from a subroutine. Subs should return. You shouldn't generally use exit to terminate your program normally.

        update: oh, btw, each pod token should be surrounded by lone newlines, as in perl -e " print qq{die;\n\n=pod\n\nhi there\n\n=cut\n\n}; "

        ____________________________________________________
        ** The Third rule of perl club is a statement of fact: pod is sexy.

Back to Code Catacombs

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (12)
As of 2015-07-28 07:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (253 votes), past polls