News::Newsrc - manage newsrc files |
News::Newsrc - manage newsrc files
use News::Newsrc;
$newsrc = new News::Newsrc;
$ok = $newsrc->load; $ok = $newsrc->load ($file); $newsrc->import_rc ( @lines); $newsrc->import_rc (\@lines);
$newsrc->save; $newsrc->save_as ($file); @lines = $newsrc->export_rc;
$ok = $newsrc-> add_group ($group, %options); $ok = $newsrc->move_group ($group, %options); $ok = $newsrc-> del_group ($group);
$newsrc-> subscribe ($group, %options); $newsrc->unsubscribe ($group, %options);
$newsrc->mark ($group, $article , %options); $newsrc->mark_list ($group, \@articles, %options); $newsrc->mark_range ($group, $from, $to, %options);
$newsrc->unmark ($group, $article , %options); $newsrc->unmark_list ($group, \@articles, %options); $newsrc->unmark_range ($group, $from, $to, %options);
... if $newsrc->exists ($group); ... if $newsrc->subscribed ($group); ... if $newsrc->marked ($group, $article);
$n = $newsrc-> num_groups; @groups = $newsrc-> groups; @groups = $newsrc-> sub_groups; @groups = $newsrc->unsub_groups;
@articles = $newsrc-> marked_articles($group, %options); @articles = $newsrc->unmarked_articles($group, $from, $to, %options);
$articles = $newsrc->get_articles ($group, %options); $ok = $newsrc->set_articles ($group, $articles, %options);
Perl 5.004, Set::IntSpan 1.07
Nothing
News::Newsrc
manages newsrc files, of the style
alt.foo: 1-21,28,31-34 alt.bar! 3,5,9-2900,2902
Methods are provided for
A newsrc file is an ASCII file that lists newsgroups and article numbers. Each line of a newsrc file describes a single newsgroup. Each line is divided into three fields: a group, a subscription mark and an article list.
Lines containing only whitespace are ignored. Whitespace within a line is ignored.
Group names must be unique within a newsrc file. The group name is required.
News::Newsrc
preserves the order of newsgroups in a newsrc file:
if a file is loaded and then saved,
the newsgroup order will be unchanged.
Methods that add or move newsgroups affect the newsgroup order.
By default,
these methods put newsgroups at the end of the newsrc file.
Other locations may be specified by passing an %options hash
with a where
key to the method.
Recognized locations are:
where
=> 'first'
where
=> 'last'
where
=> 'alpha'
If the other newsgroups are not sorted alphabetically, put the group at an arbitrary location.
where
=> [ before
=> $group ]If $group does not exist, put the group last.
where
=> [ after
=> $group ]If $group does not exist, put the group last.
where
=> [ number
=> $n ]
new
News::Newsrc
News::Newsrc
object.
The object contains no newsgroups.
load
load
($file)If $file can't be opened,
load
discards existing data from $newsrc and returns null.
If $file contains invalid lines, load
will die
.
When this happens, the state of $newsrc is undefined.
import_rc
(@lines)import_rc
([@lines])Each line in @lines describes a single newsgroup,
and must have the format described in NEWSRC FILES.
If @lines contains invalid lines, import_rc
will die
.
When this happens, the state of $newsrc is undefined.
import_rc
accepts either an array or an array reference.
save
load
ed.
If load
has not been called, writes to $ENV{HOME}/.newsrc.
In either case, if the destination file exists,
it is renamed to file.bak
.
save_as
($file).bak
.
Subsequent calls to save
will write to $file.
export_rc
In scalar context, returns an array reference.
add_group
($group, %options)By default, $group is added to the end of the list of newsgroups. Other locations may be specified in %options; see NEWSGROUP ORDER for details.
By default,
add_group
does nothing if $group already exists.
If the replace
=> 1
option is provided,
then add_group
will delete $group if it exists,
and then add it.
add_group
returns true iff $group was added.
move_group
($group, %options)If $group does not exist,
move_group
does nothing and returns false.
Otherwise, it returns true.
del_group
($group)del_group
removes it and returns true.
The article list for $group is lost.
If $group does not exist in $newsrc,
del_group
does nothing and returns false.
subscribe
($group, %options)$group will be created if it does not exist. Its location may be specified in %options; see NEWSGROUP ORDER for details.
unsubscribe
($group, %options)$group will be created if it does not exist. Its location may be specified in %options; see NEWSGROUP ORDER for details.
mark
($group, $article, %options)$group will be created if it does not exist. Its location may be specified in %options; see NEWSGROUP ORDER for details.
mark_list
($group, \@articles, %options)$group will be created if it does not exist. Its location may be specified in %options; see NEWSGROUP ORDER for details.
mark_range
($group, $from, $to, %options)$group will be created if it does not exist. Its location may be specified in %options; see NEWSGROUP ORDER for details.
unmark
($group, $article, %options)$group will be created if it does not exist. Its location may be specified in %options; see NEWSGROUP ORDER for details.
unmark_list
($group, \@articles, %options)$group will be created if it does not exist. Its location may be specified in %options; see NEWSGROUP ORDER for details.
unmark_range
($group, $from, $to, %options)$group will be created if it does not exist. Its location may be specified in %options; see NEWSGROUP ORDER for details.
exists
($group)subscribed
($group)marked
($group, $article)num_groups
groups
sub_groups
unsub_groups
marked_articles
($group)$group will be created if it does not exist. Its location may be specified in %options; see NEWSGROUP ORDER for details.
unmarked_articles
($group, $from, $to, %options)$group will be created if it does not exist. Its location may be specified in %options; see NEWSGROUP ORDER for details.
get_articles
($group, %options)$group will be created if it does not exist. Its location may be specified in %options; see NEWSGROUP ORDER for details.
If you plan to do any nontrivial processing on the article list,
consider converting it to a Set::IntSpan
object:
$articles = Set::IntSpan->new($newsrc->get_articles('alt.foo'))
set_articles
($group, $articles, %options)$articles is a string, as described in NEWSRC FILES.
$group will be created if it does not exist. Its location may be specified in %options; see NEWSGROUP ORDER for details.
If $articles does not have the format described in NEWSRC FILES,
set_articles
does nothing and returns false.
Otherwise, it returns true.
``Don't test for errors that you can't handle.''
load
returns null if it can't open the newsrc file,
and dies if the newsrc file contains invalid data.
This isn't as schizophrenic as it seems.
There are several ways a program could handle an open failure on the newsrc file. It could prompt the user to reenter the file name. It could assume that the user doesn't have a newsrc file yet. If it doesn't want to handle the error, it could go ahead and die.
On the other hand, it is very difficult for a program to do anything sensible if the newsrc file opens successfully and then turns out to contain invalid data. Was there a disk error? Is the file corrupt? Did the user accidentally specify his kill file instead of his newsrc file? And what are you going to do about it?
Rather than try to handle an error like this,
it's probably better to die and let the user sort things out.
By the same rational,
save
and save_as
die on failure.
Programs that must retain control can use eval{...} to protect calls that may die. For example, Perl/Tk runs all callbacks inside an eval{...}. If a callback dies, Perl/Tk regains control and displays $@ in a dialog box. The user can then decide whether to continue or quit from the program.
import_rc
/export_rc
I was going to call these methods import
and export
,
but import
turns out not to be a good name for a method,
because use
also calls import
,
and expects different semantics.
I added the _rc
suffix to import
to avoid this conflict.
It's reasonably short and somewhat mnemonic
(the module manages newsrc files).
I added the same suffix to export
for symmetry.
Steven McDougall, swmcd@world.std.com
perl(1), Set::IntSpan
Copyright 1996-1998 by Steven McDougall. This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
News::Newsrc - manage newsrc files |