in reply to New sections and move one
If you are looking for general-purpose subroutines
that are ready to run,
the right place to look is CPAN, not PerlMonks.
Re: Re (tilly) 1: New sections and move one
by chipmunk (Parson) on Aug 12, 2001 at 19:23 UTC
|
Err... Then what are the Monastery's Snippets, Code Catacombs, Craft, and Cool Uses For Perl sections for?
| [reply] [Watch: Dir/Any] |
|
| [reply] [Watch: Dir/Any] |
Re: Re (tilly) 1: New sections and move one
by aquacade (Scribe) on Aug 12, 2001 at 19:48 UTC
|
I'm using ActiveState for (yuk!) Windows cause that's my client platform. I have not been able to successfully install a CPAN module (as yet) unless ActiveState had it in it's PPM repository already. (I heard Brian Ingerson is on a leave of absence from ActiveState, when I called to complain that installing from the respository is throwing errors like never before, like missing dependent modules).
Please excuse me quickly throwing together some examples of what I mean. They were quickly tested, but I'm sure someone could make them better! What I'm suggesting is like the following examples. Someone new to Perl could use these right away on faith and learn what they mean after they "Saved the Day!" with their boss or client.
Update: Revised rtrim to use sexeger as suggested below!
sub rtrim {
# Right trim spaces
my @out=@_;
for (@out) {
$_ = reverse $_;
s/^\s+//;
$_ = reverse $_;
}
return wantarray ? @out : $out[0];
}
sub ltrim {
# Left trim spaces
my @out=@_;
for (@out) {
s/^\s+//;
}
return wantarray ? @out : $out[0];
}
sub trim {
# Trim extra spaces from both left and right
my @out=@_;
for (@out) {
s/^\s+//;
s/\s+$//;
}
return wantarray ? @out : $out[0];
}
sub trimall {
# Trim all extra spaces from both left and right and middle
my @out=@_;
for (@out) {
$_= join ' ',split;
}
return wantarray ? @out : $out[0];
}
Again, this could be the start of the "String related" category of the Perl Monks' "Subroutine Library." I don't think these kind of onezie subroutines would find a welcome home in CPAN as one routine per submission. If they were in a module, then I have the same 'discovery' and 'install' problems I have today. As onezies they would fail CPANTS for sure not being modules!
..:::::: aquacade ::::::.. | [reply] [Watch: Dir/Any] [d/l] |
|
I remain unconvinced. For pure Perl modules on CPAN I have had good luck under Windows just downloading, untarring, and then copying them manually. Other than the need to find something that will handle .tar.gz files on Windows, I don't see what is gained by having such modules on PerlMonks.
As for the examples that you give, your routines make perfect sense if you are working in a language like VB with poor string handling capability. But in Perl I, and I think most experienced Perl developers, find it much easier and more reasonable to just inline the regular expressions in question where needed.
A better example might be something like:
# Runs a system command with basic error handling.
sub run_system {
if ($verbose) {
my ($cmd, @args) = @_;
print "Running '$cmd'\n";
if (@args) {
print " with arguments '@args'\n";
}
}
if (system(@_)) {
my $cmd = shift;
my $arg_msg = "";
if (@_) {
$arg_msg = " with args ['@_']";
}
confess("Running '$cmd'$arg_msg failed on return code ($?)\n");
}
}
But how many truly useful ones are there? The fact is that the built-ins in Perl combined with its flexibility make a lot of the basic utility functions you might write in a different language (VB, C, etc) pretty much useless. I cannot think of enough for me to want a new section. Instead I think that they belong in Snippets. True, that needs improvement (eg making it searchable). Perhaps it should have categories, etc. But I think that it is the right place for this. | [reply] [Watch: Dir/Any] [d/l] |
|
| [reply] [Watch: Dir/Any] |
|
|
|
|
On a slightly off note,
I think your rtrim example could be improved using sexegers.elusion :
| [reply] [Watch: Dir/Any] [d/l] |
|
|