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

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
package INI::Library ; =head1 NAME INI::Library - A module for managing simple libraries stored in INI-like files. =head1 SYNOPSIS use INI::Library ; my $sql = new INI::Library { lib => 'sql.lib' } ; ## Ask for a library entry by name... my $query = $sql->retr( 'some_sql_query' ) ; ## Add or update an entry... $sql->set( 'yet_another_query', <<'END' ) ; SELECT foo FROM bar WHERE zoot = 1 END ## Remove an entry from the library... $sql->drop( 'one_more_query' ) ; ## List the entries in the library... print join( ' : ', $sql->elements ), "\n" ; ## Dump the contents of the library to a string... my $lib_str = $sql->ini_out ; ## Write the INI library to disk... $sql->write ; =head1 LIBRARY FILE FORMAT The format for the library files looks a little like an INI file (henc +e the name.) However, unlike an INI file, it does not handle key=value +pairs which are divided into sections. Library entry names are on a line by themselves, enclosed in square brackets. Whatever occurs until the next title tag is the value of the library entry. Blank lines, pound signs (#) and C++ style comments (//) are all discarded. A sample library file might look like this: ## A sample library file [get_survey_questions] select question_no, question_text from question where survey_id = ? order by question_no [get_survey_info] select title, date_format( open_date, '%Y%m%d' ) as open_date, date_format( close_date, '%Y%m%d' ) as close_date, template_file from survey where survey_id = ? =cut use strict ; use warnings ; =head1 OBJECT METHODS =over 4 =item PACKAGE-E<gt>new( HASHREF ) Create a new library handle. Currently, the only argument supported i +n the hashref is C<lib>, which refers to the file containing the INI library. =cut sub new { my $proto = shift ; my $options = shift ; my $self = { 'options' => $options, 'contents' => undef } ; { my $curr_name = '' ; open INI, $self->{'options'}->{'lib'} or die "Cannot open $self->{'options'}->{'lib'}: $!" ; while ( <INI> ) { next if m{^\s*$} ; next if m{^\s*#} ; next if m{^\s*//} ; if ( m{^\[([^\]]+)\]} ) { $curr_name = $1 ; next ; } if ( $curr_name ) { $self->{'contents'}->{$curr_name} .= $_ ; } } } bless $self, $proto ; return $self ; } =item $OBJ-E<gt>retr( NAME ) Returns the library entry referenced by NAME. =cut sub retr { my ( $self, $entity_name ) = @_ ; return $self->{'contents'}->{$entity_name} ; } =item $OBJ-E<gt>set( NAME, VALUE ) Sets the library entry NAME to VALUE. This is used both to create new library entries and to update existing ones. =cut sub set { my ( $self, $entity_name, $entity ) = @_ ; $self->{'contents'}->{$entity_name} = $entity ; return $self ; } =item $OBJ-E<gt>drop( NAME ) Drops entry NAME form the library. =cut sub drop { my ( $self, $entity_name ) = @_ ; delete $self->{'contents'}->{$entity_name} ; return $self ; } =item $OBJ-E<gt>elements Returns a list of all entry names in the library. =cut sub elements { my $self = shift ; return sort keys %{$self->{'contents'}} ; } =item $OBJ-E<gt>ini_out Returns a string containing the library contents in the same INI format that the module reads from. =cut sub ini_out { my $self = shift ; my $output = '' ; foreach ( sort keys %{$self->{'contents'}} ) { $output .= sprintf "[%s]\n%s\n", $_, $self->{'contents'}->{$_} + ; } return $output ; } =item $OBJ-E<gt>write Writes the library to the file named in C<lib>. =cut sub write { my $self = shift ; open OUT, ">$self->{'options'}->{'lib'}" or die "Cannot open $self->{'options'}->{'lib'}: $!" ; print OUT $self->ini_out ; close OUT ; } =back =head1 AUTHOR Doug Gorley E<lt>douggorley@shaw.caE<gt> =cut 1 ; __END__

In reply to INI::Library by DamnDirtyApe

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, 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, 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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    [Cosmic37]: I'm wondering whether there is a specific/(or at least "usual") command or does one take a copy before undefining and then copy it back after slurping file into a string?
    [BarApp]: I need help accessing perl files. I need permission!!!
    [Cosmic37]: I wish thee a peachy life and express gratitude for considering my pathetic questions
    [erix]: record separator
    [Cosmic37]: Permissions are interesting earthlings. Did nature determine who gives permission and who asks permission. Who was the first to get permission? Are you not related to them as one big earthling family?

    How do I use this? | Other CB clients
    Other Users?
    Others musing on the Monastery: (9)
    As of 2017-06-29 16:35 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      How many monitors do you use while coding?















      Results (672 votes). Check out past polls.