[23912] in Perl-Users-Digest
Perl-Users Digest, Issue: 6114 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Feb 11 11:10:41 2004
Date: Wed, 11 Feb 2004 08:10:09 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Wed, 11 Feb 2004 Volume: 10 Number: 6114
Today's topics:
Re: RFC: utils.pm <tore@aursand.no>
Re: RFC: utils.pm <usenet@morrow.me.uk>
Re: Search and replace string (ajay)
Re: Search and replace string (Jon Landenburer)
Re: Software Quality & Fault Measurement <nospam_steved94@comcast.net>
Re: UNIX Find on Windows <bik.mido@tiscalinet.it>
Re: UNIX Find on Windows <bik.mido@tiscalinet.it>
Re: Why references?? <usenet@morrow.me.uk>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Wed, 11 Feb 2004 16:10:26 +0100
From: Tore Aursand <tore@aursand.no>
Subject: Re: RFC: utils.pm
Message-Id: <pan.2004.02.11.15.10.25.589573@aursand.no>
On Wed, 11 Feb 2004 14:02:48 +0000, Tassilo v. Parseval wrote:
>> [...]
>>
>> The main question is: Although I know that the Perl core should stick
>> to a minimum, why isn't a module like this included? It covers most of
>> the traps that newbies encounters, and it offers experienced
>> programmers to be even more lazy when programming.
> I think it defeats a little bit the purpose of a Perl distribution as an
> all-purpose means. Some of the functions you suggest would be totally
> meaningless to me. Mostly the date-related functions. Within the past
> three years I didn't have to deal with dates more than, say, three times
> or so. I'd be slightly taken aback to find them in the Perl core while
> at the same time not finding the functionality that I use on a daily
> base and for which I had to write my own code/use a module (such as
> playing back MP3s).
I guess we all want modules which solves the current task we're working
on, right? :)
> Also, I'd find the availability of such micro-functions in the core
> offending. Maybe it's because I suffer more from hubris than others, but
> I really like to reinvent these small wheels in my programs each time.
Really? It's the complete opposite for me: I don't want to reinvent the
wheel each time, and I certainly don't want to include heaps of modules
for doing small, repetetive things.
I am sure that there are modules out there which, combined, does
everything that I listed, but I don't want to use _many_ modules to do
such small things. I want common functions in one module.
I understand that "common functions" might be subjective, but I also think
that most of the functions I listed is something that most of the Perl
programmers do "by hand" - or reinvent - almost every day.
> Not to forget that I think that these utility functions would have a bad
> effect on beginners.
In one way, yes. In another way, no; A lot of the questions asked in
this particular newsgroup are questions that already answered in the FAQ.
I think the major reason why all these questions pops up now and then (far
too often, if you ask me), is that the "unavailability" of simple
functions to solve the problems.
Why do actually beginners - or experienced programmers, for that sake -
need to know how to do everything (...) manually? I don't care how trim()
works, as long as it works. I don't care how the index() function in Perl
is implemented, as long as it works.
> The real reasons though for not including them is indeed the size
> argument. A recent Perl distribution is really big and the amount of
> code in it (that has to be maintained) scary.
I agree on this one. The core size of Perl should always be a minimum,
but I don't think that a module like this one would have much impact on
the size. :)
> Finally, this would make Perl look so PHPish. It hurts on the eyes.
Haha. :)
--
Tore Aursand <tore@aursand.no>
"When you love someone, all your saved-up wishes start coming out." --
Elizabeth Bowen
------------------------------
Date: Wed, 11 Feb 2004 15:23:37 +0000 (UTC)
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: RFC: utils.pm
Message-Id: <c0dhdp$ned$1@wisteria.csv.warwick.ac.uk>
Tore Aursand <tore@aursand.no> wrote:
> Over the time as a Perl programmer, I've gathered small bits of code here
> and there. The last few weeks I've tried to structure all this code and
> put in a module which I - so far - call 'utils.pm'. Thus, in my scripts,
> I tend to begin like this:
>
> #!/usr/bin/perl
> #
> use strict;
> use warnings;
> use utils;
>
> The name could - of course - have been more propriate (if possible), but
> to me 'utils' seems like a good one. :)
Lowercase top-level names are reserved for pragmata. Call it Utils. It
is usually considered good practice not to export stuff unless asked.
> The main question is: Although I know that the Perl core should stick to
> a minimum, why isn't a module like this included? It covers most of the
> traps that newbies encounters, and it offers experienced programmers to be
> even more lazy when programming.
>
> Information about the module's functions:
>
> STRINGS
> ltrim( $value ) - Removes leading whitespace from a string.
$value =~ s/^\s*//;
> rtrim( $value ) - Removes trailing whitespace from a string.
$value =~ s/\s*$//;
> trim( $value ) - Combines ltrim() and rtrim().
$value =~ s/^\s*(.*?)\s*$/$1/;
or some such.
> squish( $value ) - Removes (all the) whitespace from a string.
$values =~ s/\s+//g;
> split_csv( $value ) - Easy CSV splitting. (*)
As you say, there are modules to do this. Or
@values = split /,/, $values;
> random_string( $length ) - Generates a random string $length
> characters long.
This is a little harder... also *much* less useful.
> CASTING
> as_string( $value, [$default] ) - Always returns a defined value,
> optionally $default if $value
> isn't defined.
Eh what? You don't *need* to cast in Perl.
$value || $default
or defined($value) ? $value : $default
or (Perl6 :) $value // $default
> as_int( $value ) - Always returns $value as an integer.
err.. int($value)
Or POSIX::floor, or POSIX::ceil.
> as_decimal( $value, [$decimals] ) - Always returns $value as a
> decimal number with $decimals
> numbers after the decimal point.
sprintf
> as_boolean( $value ) - Always returns $value as a boolena value (ie.
> TRUE/1 or FALSE/0).
!!$value
> as_date( $value ) - Always returns $value as a date (YYYY-MM-DD).
> as_time( $value ) - Always returns $value as a time (HH:MM:SS).
> as_datetime( $value ) - Always returns $value as a datetime (which
> means combining as_date() and as_time()).
POSIX::strftime
> VALIDATION
> Each of the CASTING functions also have a is_* function, which returns
> TRUE/1 or FALSE/0 depending on wether the input argument conforms to
> the datatype.
Ummm... everything is (can be) a string.
Everything is either true or false.
is_int is simply $value =~ /^\d+$/.
Dates can and should be handled by your favourite date-time parsing
module: the code is non-trivial, so should be reused.
I can't think offhand how I'd do is_decimal, probably because I've
never had occasion to.
>
> NUMBERS
> round( $value ) - Rounds a number to the nearest integer.
int($value + 0.5) is usually good enough.
> random_number( $min, $max ) - Returns a random number in the range
> $min to $max.
(rand * ($max - $min)) + $min
> format_number( $value, $separator ) - Formats a number with a given
> separator; 1234 becomes 1,234.
Less trivial... is probably better done by something locale-aware.
> ARRAYS
> unique( $arrayref ) - Returns only the unique elements in $array.
> intersection( $arrayref1, $arrayref2 ) - Computes the intersection
> of two array references.
> union( $arrayref1, $arrayref2 ) - Computes the union of two array
> references.
These should all be in a module called Set::Util... feel free to write
it.
> shuffle( $arrayref ) - Returns the elements shuffled randomly.
List::Util::shuffle
> DATES
> now_year()
> now_month()
> now_day() - These three returns current the year, month and day.
> now_date() - Combines the three above.
> now_hour()
> now_minute()
> now_seconds() - These three returns the current hour, minute and
> second.
> now_time() - Combines the three above.
> now() - Combines now_date() and now_time(); 'YYYY-MM-DD hh:mm:ss'.
POSIX::strftime again, with 'localtime time'.
> is_leap_year( $year ) - Returns TRUE/1 if $year is a leap year.
> day_of_week( $date ) - Returns the day of the week for $date.
> day_of_year( $date ) - Returns the day of the year for $date.
> days_in_month( $year, $month ) - Returns the number of days in the
> given year/month.
> days_in_year( $year ) - Returns 365 or 366 depending on wether the
> year is a leap year or not.
I'm *quite* sure one of the many date-time modules handles this already.
> Well. These are the majority of the functions I've gathered. There are a
> few more, but never mind them for now. All the functions are written in
> pure Perl, so no modules - of course - or "anything else" is
> required.
This is *not* an advantage. Code should be reused where possible. It
is entirely sensible to have a module (say, Local::Utils) that loads
half-a-dozen modules you commonly use and exports a few trivial
functions; this will naturally reflect your own use of Perl and not be
of any use to anyone else.
Ben
--
Heracles: Vulture! Here's a titbit for you / A few dried molecules of the gall
From the liver of a friend of yours. / Excuse the arrow but I have no spoon.
(Ted Hughes, [ Heracles shoots Vulture with arrow. Vulture bursts into ]
/Alcestis/) [ flame, and falls out of sight. ] ben@morrow.me.uk
------------------------------
Date: 11 Feb 2004 07:55:52 -0800
From: ajay_kumarsingh@yahoo.com (ajay)
Subject: Re: Search and replace string
Message-Id: <2a0aec36.0402110755.443ccfa1@posting.google.com>
Gunnar Hjalmarsson <noreply@gunnar.cc> wrote in message news:<c0d5c5$159kbn$1@ID-184292.news.uni-berlin.de>...
> Ajay wrote:
> > I've to search and replace the value of strings from a file.
>
> http://www.perldoc.com/perl5.8.0/pod/perlop.html#s-PATTERN-REPLACEMENT-egimosx
Nice link tx
i expected a clearcut solution
------------------------------
Date: 11 Feb 2004 07:58:34 -0800
From: jon.m.landenburger@verizon.com (Jon Landenburer)
Subject: Re: Search and replace string
Message-Id: <feee7535.0402110758.4d05234e@posting.google.com>
Ajay <ajay_kumarsingh@yahoo.com> wrote in message news:<402A15F5.1F20D07B@yahoo.com>...
> Hi All,
> I've to search and replace the value of strings from a file.
> The first string "giccip" can have multiple instances but i have to replace it's
> value from the first instance only.
> In the file "gicc" appear as follows, and it appears in the last of that line:
>
Thougt I understood what you were doing until I rwead the code but I
can tell you if you do a search and replace. The replacement will
only effect the FIRST item found unless you make it a global replace.
$A = "AAAABBABABABBABBABBABBABBBA";
$A =~ s/A/X/; ## SINGLE REPLACE
print "$A\n";
$A =~ s/A/Z/g; ## GLOBAL
print "$A\n";
!./a.pl
XAAABBABABABBABBABBABBABBBA
XZZZBBZBZBZBBZBBZBBZBBZBBBZ
[Hit return to continue]
woof
------------------------------
Date: Wed, 11 Feb 2004 14:38:54 GMT
From: "Steve" <nospam_steved94@comcast.net>
Subject: Re: Software Quality & Fault Measurement
Message-Id: <28rWb.147627$U%5.671617@attbi_s03>
Please do not cross post to programming language newsgroups.
Replies are often by default cross posted as well, leading to excessive
chatter, and often to useless language wars.
If you must reply to such a post, please remove all cross posted newsgroups
from you reply.
Thank you,
Steve
(The Duck)
------------------------------
Date: 11 Feb 2004 14:44:24 GMT
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Re: UNIX Find on Windows
Message-Id: <6kem201pknogtgd5bcen43sfsdlcjk259t@4ax.com>
On Tue, 10 Feb 2004 16:51:30 -0700, Eric Schwartz <emschwar@pobox.com>
wrote:
>also, see find2perl (in the standard distribution on Unix; I'd assume
>also on Windows), which converts find commands into equivalent (but
Yes, it's there!
Michele
--
you'll see that it shouldn't be so. AND, the writting as usuall is
fantastic incompetent. To illustrate, i quote:
- Xah Lee trolling on clpmisc,
"perl bug File::Basename and Perl's nature"
------------------------------
Date: 11 Feb 2004 14:44:25 GMT
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Re: UNIX Find on Windows
Message-Id: <c34n20hu0jn5ne8ih05qjh2r9rnjjo40g9@4ax.com>
On Tue, 10 Feb 2004 23:13:36 GMT, Brian
<brian.getridofthis.bygland@boeingDELETEthis.com> wrote:
>$FOUND = `find $DIRROOT -name "$PATTERN" -print`;
BTW, it may be an idiosincrasy of mine, but I'd rather open() a find
cmd in pipe and read 'while <$fh>' as usual...
>Is there a way to do this on Windows using Perl and/or native Windows
>commands?
Using Perl: File::Find. Using native Windows commands: dir /b [/s]
(but may not do exactlty what you mean!). Alternatively I, for one,
have a straight find port from UNXUTILS.
But AFAICT I've never used a statement like the one above, the
rationale being *IMHO* that if the task is simple enough to fit nicely
in a series of piped commands on the cmd line, then find is the right
tool for this, and Perl may not even be necessary whereas if the task
is complex enough to deserve the full power of Perl, then I'd use
File::Find instead.
Michele
--
you'll see that it shouldn't be so. AND, the writting as usuall is
fantastic incompetent. To illustrate, i quote:
- Xah Lee trolling on clpmisc,
"perl bug File::Basename and Perl's nature"
------------------------------
Date: Wed, 11 Feb 2004 15:59:03 +0000 (UTC)
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: Why references??
Message-Id: <c0djg7$oe3$2@wisteria.csv.warwick.ac.uk>
Brian McCauley <nobull@mail.com> wrote:
> Ben Morrow <usenet@morrow.me.uk> writes:
>
> > sub hash {
> > my %h = (a => 'b');
> > return \%h;
> > }
> >
> > my $h = hash;
> >
> > then the *variable* %h is lexically scoped to the sub. It never gets
> > out, as in there is nowhere else in the program where %h refers to the
> > same data. The value, i.e. the hash (the HV), is refcounted like all
> > Perl values.
>
> The thing you call "value" is more conventinally called "variable"
> in Perl.
OK, if you say so... my reason for thinking otherwise is that I'm sure
I've read either Tad or MJD, I forget which, writing that my scopes a
variable whereas local scopes a variable's value.
> > The distinction is an important dichotomy in Perl (indeed, in all
> > compiled languages): variables are lexical, values dynamic; variables
> > are created at compile-time, values at run-time. my acts on variables,
> > local on values; this is the source of the differences between them.
>
> It is important, as you say, to understand the disticution between
> these entities. It is also a good idea to use (in public) the same
> nomenclature as other people (even if your nomenclature seems more
> logical).
Of course. I was not intending to rock any boats.
> Oh, and by the way, pad entries are lexical, stash entries are not
> lexical.
The FQ name (whatever you choose to call it) is lexically global:
visible from everywhere. The short name is, of course, package scoped;
package statements are lexically scoped, so the visibility of such
names is also lexically scoped. The point is that it is possible to
work out which variable a given name refers to solely from the lexical
structure of the program: there are no run-time action-at-a-distance
effects.
Ben
--
If I were a butterfly I'd live for a day, / I would be free, just blowing away.
This cruel country has driven me down / Teased me and lied, teased me and lied.
I've only sad stories to tell to this town: / My dreams have withered and died.
ben@morrow.me.uk <=>=<=>=<=>=<=>=<=>=<=>=<=>=<=>=<=>=<=>=<=> (Kate Rusby)
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>
Administrivia:
#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc. For subscription or unsubscription requests, send
#the single line:
#
# subscribe perl-users
#or:
# unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.
NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.
#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V10 Issue 6114
***************************************