man News::Newsrc () - manage newsrc files
NAME
News::Newsrc - manage newsrc files
SYNOPSIS
use News::Newsrc;
$newsrc = new News::Newsrc; $newsrc = new News::Newsrc $file;
$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);
REQUIRES
Perl 5.6.0, Set::IntSpan 1.07
EXPORTS
Nothing
DESCRIPTION
CWNews::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
- •
- reading and writing newsrc files
- •
- adding and removing newsgroups
- •
- changing the order of newsgroups
- •
- subscribing and unsubscribing from newsgroups
- •
- testing whether groups exist and are subscribed
- •
- marking and unmarking articles
- •
- testing whether articles are marked
- •
- returning lists of newsgroups
- •
- returning lists of articles
NEWSRC FILES
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
- The group is the name of the newsgroup. A group name may not contain colons (:) or exclamation points (!). Group names must be unique within a newsrc file. The group name is required.
- Subscription mark
- The subscription mark is either a colon (:), for subscribed groups, or an exclamation point (!), for unsubscribed groups. The subscription mark is required.
- Article list
- The article list is a comma-separated list of positive integers. The integers must be listed in increasing order. Runs of consecutive integers may be abbreviated a-b, where a is the first integer in the run and b is the last. The article list may be empty.
NEWSGROUP ORDER
CWNews::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 CWwhere key to the method. Recognized locations are: Put the newsgroup first. Put the newsgroup last. Put the newsgroup in alphabetical order. If the other newsgroups are not sorted alphabetically, put the group at an arbitrary location. Put the group immediately before $group. If $group does not exist, put the group last. Put the group immediately after $group. If $group does not exist, put the group last. Put the group at position $n in the group list. Indices are zero-based. Negative indices count backwards from the end of the list.
METHODS
Creates and returns a CWNews::Newsrc object. If $file is specified, CWnew loads the newsgroups in $file into the object. If the load fails, CWnew CWdies. Loads the newsgroups in $file into $newsrc. If $file is omitted, reads $ENV{HOME}/.newsrc. Any existing data in $newsrc is discarded. Returns true on success. If $file can't be opened, CWload discards existing data from $newsrc and returns null. If $file contains invalid lines, CWload will CWdie. When this happens, the state of $newsrc is undefined. Imports the newsgroups in @lines into $newsrc. Any existing data in $newsrc is discarded. Each line in @lines describes a single newsgroup, and must have the format described in NEWSRC FILES. If @lines contains invalid lines, CWimport_rc will CWdie. When this happens, the state of $newsrc is undefined. CWimport_rc accepts either an array or an array reference. Writes the contents of $newsrc back to the file from which it was CWloaded. If CWload has not been called, writes to $ENV{HOME}/.newsrc. In either case, if the destination file exists, it is renamed to fileCW.bak. CWsave will CWdie if there is an error writing the file. Writes the contents of $newsrc to $file. If $file exists, it is renamed to $fileCW.bak. Subsequent calls to CWsave will write to $file. CWsave_as will CWdie if there is an error writing the file. Returns the contents of $newsrc as a list of lines. Each line describes a single newsgroup, and has the format described in NEWSRC FILES. In scalar context, returns an array reference. Adds $group to the list of newsgroups in $newsrc. $group is initially subscribed. The article list for $group is initially empty. 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, CWadd_group does nothing if $group already exists. If the CWreplace => CW1 option is provided, then CWadd_group will delete $group if it exists, and then add it. CWadd_group returns true iff $group was added. Changes the position of $group in $newsrc according to %options. See NEWSGROUP ORDER for details. If $group does not exist, CWmove_group does nothing and returns false. Otherwise, it returns true. If $group exists in $newsrc, CWdel_group removes it and returns true. The article list for $group is lost. If $group does not exist in $newsrc, CWdel_group does nothing and returns false. Subscribes to $group. $group will be created if it does not exist. Its location may be specified in %options; see NEWSGROUP ORDER for details. Unsubscribes from $group. $group will be created if it does not exist. Its location may be specified in %options; see NEWSGROUP ORDER for details. Adds $article to the article list for $group. $group will be created if it does not exist. Its location may be specified in %options; see NEWSGROUP ORDER for details. Adds @articles to the article list for $group. $group will be created if it does not exist. Its location may be specified in %options; see NEWSGROUP ORDER for details. Adds all the articles from $from to $to, inclusive, to the article list for $group. $group will be created if it does not exist. Its location may be specified in %options; see NEWSGROUP ORDER for details. Removes $article from the article list for $group. $group will be created if it does not exist. Its location may be specified in %options; see NEWSGROUP ORDER for details. Removes @articles from the article list for $group. $group will be created if it does not exist. Its location may be specified in %options; see NEWSGROUP ORDER for details. Removes all the articles from $from to $to, inclusive, from the article list for $group. $group will be created if it does not exist. Its location may be specified in %options; see NEWSGROUP ORDER for details. Returns true iff $group exists in $newsrc. Returns true iff $group exists and is subscribed. Returns true iff $group exists and its article list contains $article. Returns the number of groups in $newsrc. Returns the list of groups in $newsrc, in newsrc order. In scalar context, returns an array reference. Returns the list of subscribed groups in $newsrc, in newsrc order. In scalar context, returns an array reference. Returns the list of unsubscribed groups in $newsrc, in newsrc order. In scalar context, returns an array reference. Returns the list of articles in the article list for $group. In scalar context, returns an array reference. $group will be created if it does not exist. Its location may be specified in %options; see NEWSGROUP ORDER for details. Returns the list of articles from $from to $to, inclusive, that do not appear in the article list for $group. In scalar context, returns an array reference. $group will be created if it does not exist. Its location may be specified in %options; see NEWSGROUP ORDER for details. Returns the article list for $group as a string, in the format 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 you plan to do any nontrivial processing on the article list, consider converting it to a CWSet::IntSpan object:
$articles = Set::IntSpan->new($newsrc->get_articles('alt.foo'))Sets the article list for CW$group. Any existing article list is lost. $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, CWset_articles does nothing and returns false. Otherwise, it returns true.
DIAGNOSTICS
- Bad newsrc line
- A line in the newsrc file does not have the format described in NEWSRC FILES.
- Bad article list
- The article list for a newsgroup does not have the format described in NEWSRC FILES.
NOTES
Error Handling
Don't test for errors that you can't handle.
CWload 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, CWsave and CWsave_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. I was going to call these methods CWimport and CWexport, but CWimport turns out not to be a good name for a method, because CWuse also calls CWimport, and expects different semantics.
I added the CW_rc suffix to CWimport to avoid this conflict. It's reasonably short and somewhat mnemonic (the module manages newsrc files). I added the same suffix to CWexport for symmetry.
ACKNOWLEDGMENTS
- •
- Neil Bowers <neilb@cre.canon.co.uk>
- •
- Matthew Darwin <matthew@davin.ottawa.on.ca>
- •
- Philip Hallstrom <philip.hallstrom@cendantsoft.com>
- •
- M. Hedlund <hedlund@best.com>
- •
- Bruce J. Keeler <bruce@gridpoint.com>
- •
- Chris Leach <Chris.Leach@epa.ericsson.se>
- •
- Abhijit Menon-Sen <ams@wiw.org>
- •
- J.B. Nicholson-Owens <jbn@pop-a-wheelie.midwest.net>
- •
- Lars Balker Rasmussen <gnort@daimi.aau.dk>
- •
- Mike Stok <mike@stok.co.uk>
- •
- Bennett Todd <bet@onyx.interactive.net>
- •
- Larry W. Virden <lvirden@cas.org>
- •
- Chris Szurgot <szurgot@itribe.net>
AUTHOR
Steven McDougall, swmcd@world.std.com
SEE ALSO
COPYRIGHT
Copyright 1996-2001 by Steven McDougall. This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.