Mozilla-Prefs-Simple

 view release on metacpan or  search on metacpan

README  view on Meta::CPAN

NAME
    Mozilla::Prefs::Simple - Manipulate Mozilla preferences

SYNOPSIS
      use Mozilla::Prefs::Simple;

      my $p = new Mozilla::Prefs::Simple('prefs.js');

      $p->set_pref("browser.blink_allowed", "true");
      $p->set_pref("general.useragent.locale", "\"en-US\"");

      if ($p->get_pref("mailnews.reply_header_type") == 2) {
        ...
      }

      $p->save_file('prefs.js');

DESCRIPTION
    This is a no-frills module for reading and writing Mozilla preference
    files.

    More details can be found in the module documentation.

AUTHOR
    Robert Rothenberg, `<rrwo at cpan.org>'

lib/Mozilla/Prefs/Simple.pm  view on Meta::CPAN

our $VERSION = '0.01';

=head1 NAME

Mozilla::Prefs::Simple - Manipulate Mozilla preferences

=head1 SYNOPSIS

  use Mozilla::Prefs::Simple;

  my $p = new Mozilla::Prefs::Simple('prefs.js');

  $p->set_pref("browser.blink_allowed", "true");
  $p->set_pref("general.useragent.locale", "\"en-US\"");

  if ($p->get_pref("mailnews.reply_header_type") == 2) {
    ...
  }

  $p->save_file('prefs.js');

=head1 DESCRIPTION

This is a no-frills module for reading and writing Mozilla preference
files.

=begin readme

More details can be found in the module documentation.

lib/Mozilla/Prefs/Simple.pm  view on Meta::CPAN

=over

=cut

=item new

Create a new preferences object.

  my $p = new Mozilla::Prefs::Simple();

  my $p = new Mozilla::Prefs::Simple('prefs.js');

=cut

sub new {
    my $class = shift || __PACKAGE__;

    my $self  = {
	"_strip_comments"  => 1,
	"_backup_original" => 1,
    };

lib/Mozilla/Prefs/Simple.pm  view on Meta::CPAN

=item clear

  $p->clear;

Erase the existing preferences. Called by L</new>  method.

=cut

sub clear {
    my $self = shift;
    delete $self->{_prefs};

    tie my %prefs, 'Tie::Hash::Sorted';
    $self->{_prefs} = \%prefs;
}


sub _parse_line {
    my $self = shift;
    my $line = shift;

    if ($line =~ /\buser_pref($RE{balanced}{-parens=>'()'})/) {
	my $pref = $1;
	if ($pref =~ /^\(\"(.+)\"\s*\,\s*(.*)\)$/) {

lib/Mozilla/Prefs/Simple.pm  view on Meta::CPAN

     }
     else {
 	croak "Preserving comments is unsupported";
     }

    return $data;
}

=item load_file

  $p->load_file('prefs.js');

Loads a preferences file.

If preferences are already set, they will be overwritten or merged with
the ones in the file.

=cut

sub load_file {
    my $self = shift;

lib/Mozilla/Prefs/Simple.pm  view on Meta::CPAN

for you:

  $p->set_pref_q("some.string", "value");

=cut

sub set_pref {
    my $self  = shift;
    my $key   = shift;
    my $value = shift;
    $self->{_prefs}->{$key} = "$value";
}

sub set_pref_q {
    my $self  = shift;
    my $key   = shift;
    my $value = shift;
    $self->{_prefs}->{$key} = "\"$value\"";
}

=item get_pref

  my $val = $p->get_pref("some.pref");

Returns the value of a preference.

=cut

sub get_pref {
    my $self  = shift;
    my $key   = shift;
    return $self->{_prefs}->{$key};
}

=item has_pref

  if ($p->has_pref("some.pref")) {
    ...
  }

Checks for the existence of a preference.

=cut

sub has_pref {
    my $self  = shift;
    my $key   = shift;
    return exists $self->{_prefs}->{$key};
}

=item print_pref

  $p->print_pref("some.pref", $fh);

Prints the JavaScript preference line to C<$fh>.

=cut

sub print_pref {
    my $self  = shift;
    my $key   = shift;
    my $value = $self->get_pref($key);
    my $fh    = shift;
    print $fh "user_pref(\"$key\", $value);\n";
}

=item print_prefs

  $p->print_prefs($fh);

Prints out all of the preferences to the filehandle.
If no filehandle is given, C<STDOUT> is assumed.
=cut

sub print_prefs {
    my $self  = shift;
    my $fh    = shift || \*STDOUT;
    while (my ($key, $value) = each %{$self->{_prefs}}) {
	$self->print_pref($key, $fh);
    }
}

=item save_file

  $p->save_file('prefs.js');

Saves the preferences to the given filename.

If the file exists, a backup copy is made of the original.

=cut

sub save_file {
    my $self = shift;
    my $file = shift;

lib/Mozilla/Prefs/Simple.pm  view on Meta::CPAN

    open($fh, ">$file");

#     print $fh "
# 
# /* Do not edit this file.
#  *
#  * If you make changes to this file while the application is running,
#  * the changes will be overwritten when the application exits.
#  *
#  * To make a manual change to preferences, you can visit the URL about:config
#  * For more information, see http://www.mozilla.org/unix/customizing.html#prefs
#  */
# ";

    print $fh "\n/* Generated by " .
	__PACKAGE__ . " on " . localtime() . " */\n\n"; 

    $self->print_prefs($fh);

    close $fh;
}

=back

=head1 CAVEATS

This module does very little to validate data.  When using it, make sure
that you backup your preferences beforehand.



( run in 0.669 second using v1.01-cache-2.11-cpan-8f98c5d2c55 )