[13875] in Perl-Users-Digest
Perl-Users Digest, Issue: 1285 Volume: 9
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Nov 4 21:10:36 1999
Date: Thu, 4 Nov 1999 18:10:28 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Message-Id: <941767827-v9-i1285@ruby.oce.orst.edu>
Content-Type: text
Perl-Users Digest Thu, 4 Nov 1999 Volume: 9 Number: 1285
Today's topics:
Re: how sort an array? <cassell@mail.cor.epa.gov>
Re: how sort an array? <uri@sysarch.com>
Re: how sort an array? (Abigail)
Re: how sort an array? <lr@hpl.hp.com>
Re: how sort an array? <cassell@mail.cor.epa.gov>
Re: how sort an array? <lr@hpl.hp.com>
Re: how sort an array? <lr@hpl.hp.com>
how to filter out a field gande@informix.com
Re: how to filter out a field <rootbeer@redcat.com>
Re: how to filter out a field (Michael Budash)
Re: How to remove control-M? (Tad McClellan)
Re: howto download a GIF from a URI? <NOSPAMbubba@titan.ndhm.gtegsc.com>
Re: howto download a GIF from a URI? <rootbeer@redcat.com>
Re: Inplace editing and safety <cassell@mail.cor.epa.gov>
Re: Inplace editing and safety <uri@sysarch.com>
Re: It is always like this here? (Kragen Sitaker)
Re: length (number of items) of an array (Abigail)
Library files and how to use them -- insert as an FAQ? (Eric Dew)
Re: Library files and how to use them -- insert as an F <rootbeer@redcat.com>
Re: Library files and how to use them -- insert as an F (Kragen Sitaker)
Digest Administrivia (Last modified: 16 Sep 99) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Thu, 04 Nov 1999 15:23:49 -0800
From: David Cassell <cassell@mail.cor.epa.gov>
Subject: Re: how sort an array?
Message-Id: <38221585.9BB11295@mail.cor.epa.gov>
Mark Bluemel wrote:
>
> In article <7vs455$ogn$1@nnrp1.deja.com>, skeeta@gmx.net wrote:
> > Hello,
> > I have a problem with sorting an array.
> > I have a file, in which all the data is stored like this:
[snip]
> Don't do it in Perl - use your operating system's "sort" command...
Umm, since he sent this from a WinNT box, are you sure that's
a good idea?
Besides, if you want to look like a Perl guru, you have to use
the latest, most obscure technique: GRP, the Guttman-Rosler
Packsort. Pronounce it 'gripe' or 'group' or 'grope' or
'grape' or 'garp' depending on your feelings about it...
David
--
David Cassell, OAO cassell@mail.cor.epa.gov
Senior computing specialist
mathematical statistician
------------------------------
Date: 04 Nov 1999 19:42:21 -0500
From: Uri Guttman <uri@sysarch.com>
Subject: Re: how sort an array?
Message-Id: <x7ln8d7or6.fsf@home.sysarch.com>
>>>>> "DC" == David Cassell <cassell@mail.cor.epa.gov> writes:
DC> Besides, if you want to look like a Perl guru, you have to use the
DC> latest, most obscure technique: GRP, the Guttman-Rosler Packsort.
DC> Pronounce it 'gripe' or 'group' or 'grope' or 'grape' or 'garp'
DC> depending on your feelings about it...
we resemble that remark! and it ain't obscure. there is a very well
received paper on the web (larry posted the url in this thread), it won
top technical paper at the last perl conference and the method is
actually simpler than the ST in many (complex) sorts and is almost
always faster. it does take a little more brains to use but it can be
wworth it if you sort large data sets. that is always an issue. very few
programs actually need to sort large enough data sets to worry about
it. just getting it right is good enough. someday my module which will
make it easy to do the GRP (i'll use that for now) sort on any set of
records. it needs some serious work (haven't touched it since june).
and those names sound more like what goes on in #perl than in misc.
uri
--
Uri Guttman --------- uri@sysarch.com ---------- http://www.sysarch.com
SYStems ARCHitecture, Software Engineering, Perl, Internet, UNIX Consulting
The Perl Books Page ----------- http://www.sysarch.com/cgi-bin/perl_books
The Best Search Engine on the Net ---------- http://www.northernlight.com
------------------------------
Date: 4 Nov 1999 19:07:52 -0600
From: abigail@delanet.com (Abigail)
Subject: Re: how sort an array?
Message-Id: <slrn824bm9.dk.abigail@alexandra.delanet.com>
David Cassell (cassell@mail.cor.epa.gov) wrote on MMCCLVI September
MCMXCIII in <URL:news:38221585.9BB11295@mail.cor.epa.gov>:
**
** Besides, if you want to look like a Perl guru, you have to use
** the latest, most obscure technique: GRP, the Guttman-Rosler
** Packsort. Pronounce it 'gripe' or 'group' or 'grope' or
** 'grape' or 'garp' depending on your feelings about it...
How about LUPS: Larry's and Uri's Pack Sort?
Abigail
--
perl -wle 'print "Prime" if (0 x shift) !~ m 0^\0?$|^(\0\0+?)\1+$0'
-----------== Posted via Newsfeeds.Com, Uncensored Usenet News ==----------
http://www.newsfeeds.com The Largest Usenet Servers in the World!
------== Over 73,000 Newsgroups - Including Dedicated Binaries Servers ==-----
------------------------------
Date: Thu, 4 Nov 1999 17:04:39 -0800
From: Larry Rosler <lr@hpl.hp.com>
Subject: Re: how sort an array?
Message-Id: <MPG.128bccff5bdb27bf98a1ac@nntp.hpl.hp.com>
In article <070dbf92.cd0ce3f7@usw-ex0101-001.remarq.com> on Thu, 04 Nov
1999 08:43:08 -0800, Mark Bluemel
<mark.bluemelNOmaSPAM@siemens.co.uk.invalid> says...
> In article <MPG.128b4ecdf14ace0c98a1a2@nntp.hpl.hp.com>, Larry Rosler
> <lr@hpl.hp.com> wrote:
> > In article <1415c574.b25aeb86@usw-ex0101-001.remarq.com> on Thu,
> > 04 Nov
> > 1999 07:00:48 -0800, Mark Bluemel
> > <mark.bluemelNOmaSPAM@siemens.co.uk.invalid> says...
...
> > > Don't do it in Perl - use your operating system's "sort"
> > command...
>
> To which Larry responded
>
> > Aarrrgh!
> > <Sound of months of effort on efficient Perl sorting hitting the
> > ground>
> > http://www.hpl.hp.com/personal/Larry_Rosler/sort/
> > discusses when your answer is appropriate. Hint: Not often!
>
> Yes, OK, but this may be one of of those infrequent times :-) I'm not
> pre-judging the issue, but ...
>
> I've scanned your paper and I didn't see any reference to benchmarking
> against an OS sort utility, given that you already _have_ the data in a
> flat file. Have you actually shown that it is more efficient to read an
> unsorted file into an array and sort it using your technique, as
> opposed to sorting the file and then reading it in?
#!/usr/local/bin/perl -w
use strict;
my $file = 'ips.txt';
sub external {
my @start = times;
my @array = `sort $file`;
my @end = times;
print map { my $t = $end[$_] - $start[$_]; "$t\n"} 0 .. 3;
}
sub internal {
my @start = times;
open IN, $file or die "Couldn't open '$file'. $!\n";
my @array = sort <IN>;
my @end = times;
print map { my $t = $end[$_] - $start[$_]; "$t\n"} 0 .. 3;
}
external();
print "\n";
internal();
__END__
Output (2 runs on single-user HP-UX 9.05, perl 5.005_03):
2.04 1.93
0.29 0.45
10.33 10.07
0.6 0.57
6.7 6.61
0.3 0.27
0 0
0 0
Interpretation:
The input file is 100000 lines of about 35 characters/line (the same
file as used in the benchmarks for the sorting paper).
The internal sort seems to run about twice as fast, for this one test.
Any other experiments would be welcome.
--
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com
------------------------------
Date: Thu, 04 Nov 1999 17:26:49 -0800
From: David Cassell <cassell@mail.cor.epa.gov>
Subject: Re: how sort an array?
Message-Id: <38223259.C7731F50@mail.cor.epa.gov>
Uri Guttman wrote:
>
> >>>>> "DC" == David Cassell <cassell@mail.cor.epa.gov> writes:
>
> DC> Besides, if you want to look like a Perl guru, you have to use the
> DC> latest, most obscure technique: GRP, the Guttman-Rosler Packsort.
> DC> Pronounce it 'gripe' or 'group' or 'grope' or 'grape' or 'garp'
> DC> depending on your feelings about it...
>
> we resemble that remark! and it ain't obscure.
[snip]
I agree. In fact, I have a hardcopy version of your paper on
my desk right now [although I have to admit I have a lot of other
papers on my desk too]. I hope you saw the zero-width smiley
assertion I stuck in there right after 'if you want to look like
a Perl guru'.
> and those names sound more like what goes on in #perl than in misc.
You mean you don't want GRP to turn into an acronym? :-)
David [now contemplating 'Forrest Grp' and ducking]
--
David Cassell, OAO cassell@mail.cor.epa.gov
Senior computing specialist
mathematical statistician
------------------------------
Date: Thu, 4 Nov 1999 17:35:10 -0800
From: Larry Rosler <lr@hpl.hp.com>
Subject: Re: how sort an array?
Message-Id: <MPG.128bd42c8e495ec298a1ad@nntp.hpl.hp.com>
[Posted and a courtesy copy mailed.]
In article <x7ln8d7or6.fsf@home.sysarch.com> on 04 Nov 1999 19:42:21 -
0500, Uri Guttman <uri@sysarch.com> says...
> >>>>> "DC" == David Cassell <cassell@mail.cor.epa.gov> writes:
>
> DC> Besides, if you want to look like a Perl guru, you have to use the
> DC> latest, most obscure technique: GRP, the Guttman-Rosler Packsort.
> DC> Pronounce it 'gripe' or 'group' or 'grope' or 'grape' or 'garp'
> DC> depending on your feelings about it...
>
> we resemble that remark!
I was going to let that remark go by, but as you included me in your
retort...
> and it ain't obscure. there is a very well
> received paper on the web (larry posted the url in this thread), it won
> top technical paper at the last perl conference and the method is
> actually simpler than the ST in many (complex) sorts and is almost
> always faster. it does take a little more brains to use but it can be
> wworth it if you sort large data sets. that is always an issue. very few
> programs actually need to sort large enough data sets to worry about
> it. just getting it right is good enough.
Here is about the simplest non-trivial sort: Sort an array of strings
by the first substring of digits in each element.
ST (elementary school -- or is it?):
my @out = map { $_->[0] }
sort { $a->[1] <=> $b->[1] }
map { [ $_, /(\d+)/ ] }
@in;
GR Prefix Sort (high-school version):
my @out = map { substr $_, 10 }
sort
map { sprintf('%.10d', /(\d+)/) . $_ }
@in;
GR Packed Sort (junior-college version):
my @out = map { substr $_, 4 }
sort
map { pack('N', /(\d+)/) . $_ }
@in;
An argument could be made that the ST has more punctuation and is harder
to understand than the 'obscure' GRP sorts. It is certainly slower, if
that matters. It may be slightly faster to write (because as Uri says,
you have to use some brains -- or a cheatsheet such as Appendix B in our
paper -- to figure out the string prefix), but his module will help with
that (if and when :-).
Things about nuclear physics that I learned in graduate school are now
taught in high school. It's just a matter of technical maturity, which
takes time. Even those who (like me) can toss off an ST by rote were
boggled by it on very first acquaintance. For the GRP, this too shall
pass.
> someday my module which will
> make it easy to do the GRP (i'll use that for now) sort on any set of
> records. it needs some serious work (haven't touched it since june).
Yeah, yeah. Real Soon Now. :-)
> and those names sound more like what goes on in #perl than in misc.
Note my ambivalence about whether the P stands for Prefix (my first
example) or Packed (my second example). This will resolve eventually.
--
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com
------------------------------
Date: Thu, 4 Nov 1999 17:37:23 -0800
From: Larry Rosler <lr@hpl.hp.com>
Subject: Re: how sort an array?
Message-Id: <MPG.128bd4ad52bee80b98a1ae@nntp.hpl.hp.com>
In article <slrn824bm9.dk.abigail@alexandra.delanet.com> on 4 Nov 1999
19:07:52 -0600, Abigail <abigail@delanet.com> says...
> David Cassell (cassell@mail.cor.epa.gov) wrote on MMCCLVI September
> MCMXCIII in <URL:news:38221585.9BB11295@mail.cor.epa.gov>:
> **
> ** Besides, if you want to look like a Perl guru, you have to use
> ** the latest, most obscure technique: GRP, the Guttman-Rosler
> ** Packsort. Pronounce it 'gripe' or 'group' or 'grope' or
> ** 'grape' or 'garp' depending on your feelings about it...
>
>
> How about LUPS: Larry's and Uri's Pack Sort?
ULP!
--
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com
------------------------------
Date: Thu, 04 Nov 1999 22:49:49 GMT
From: gande@informix.com
Subject: how to filter out a field
Message-Id: <38220c5d.19098982@134.168.12.199>
Hi Gurus,
I have a line like
$arg= -r-s---- 1 1343 GROUP 0 sept 97 /local/work/dir1/.dir2/file.
Now my question is how to filter out only the path name?
i.e.,/local/work/dir1
I thought of using split function. But is there any better solution to
get this path name??
Thanks
gande
------------------------------
Date: Thu, 4 Nov 1999 16:12:24 -0800
From: Tom Phoenix <rootbeer@redcat.com>
Subject: Re: how to filter out a field
Message-Id: <Pine.GSO.4.10.9911041610220.29670-100000@user2.teleport.com>
On Thu, 4 Nov 1999 gande@informix.com wrote:
> I have a line like
> $arg= -r-s---- 1 1343 GROUP 0 sept 97 /local/work/dir1/.dir2/file.
>
> Now my question is how to filter out only the path name?
>
> i.e.,/local/work/dir1
Maybe you want this code?
$arg =~ s#/local/work/dir1##g;
That'll filter out that string wherever it appears in $arg. If that's not
what you meant, perhaps you should look up what s/// does in the perlop
and perlre manpages to see whether you can improve upon it. Cheers!
--
Tom Phoenix Perl Training and Hacking Esperanto
Randal Schwartz Case: http://www.rahul.net/jeffrey/ovs/
------------------------------
Date: Thu, 04 Nov 1999 16:15:51 -0800
From: mbudash@wcws.com (Michael Budash)
Subject: Re: how to filter out a field
Message-Id: <mbudash-0411991615510001@adsl-216-103-91-123.dsl.snfc21.pacbell.net>
In article <38220c5d.19098982@134.168.12.199>, gande@informix.com wrote:
> Hi Gurus,
>
> I have a line like
> $arg= -r-s---- 1 1343 GROUP 0 sept 97 /local/work/dir1/.dir2/file.
>
> Now my question is how to filter out only the path name?
>
> i.e.,/local/work/dir1
>
er... do you mean /local/work/dir1/.dir2 ?
if so:
$path = $1 if ( $arg =~ m|\s(\S+)/.+$| );
if you really did mean /local/work/dir1:
$path = $1 if ( $arg =~ m|\s(\S+)/.+/.+$| );
(tested before posting)
hth-
--
Michael Budash ~~~~~~~~~~ mbudash@wcws.com
------------------------------
Date: Thu, 4 Nov 1999 14:49:50 -0500
From: tadmc@metronet.com (Tad McClellan)
Subject: Re: How to remove control-M?
Message-Id: <u0osv7.0bf.ln@magna.metronet.com>
Peter Sakalaukus (sakalauk@ssc.usm.edu) wrote:
: Never mind. Once I figured out that control-M maps to the carriage
: return (\r) it was simple to remove.
but you aren't using s///g to do tr///d's job, are you?
--
Tad McClellan SGML Consulting
tadmc@metronet.com Perl programming
Fort Worth, Texas
------------------------------
Date: Thu, 04 Nov 1999 18:08:10 -0500
From: "J.Z. Brody" <NOSPAMbubba@titan.ndhm.gtegsc.com>
Subject: Re: howto download a GIF from a URI?
Message-Id: <382211DA.B2D300A3@titan.ndhm.gtegsc.com>
use LWP::Simple;
($thingtoget,$name) = @ARGV;
getstore "$thingtoget", "$name";
usage:
foo.pl http://www.foo.com/some.gif myfile.gif
-jZb
bmlam wrote:
> > Can someone please point out how I can download image files found in a
> > URI and save thme as binary files?
> > Assuming I have the full path name of the image files, e.g,
> >
> > www.abc.org/img/beelaabong.gif
> >
>
> This is just a URL out of my fancy. Below is a realistic one
>
> http://www.oreilly.com/graphics_new/navbar/off-perl.gif
>
> >
> > Thanks!
------------------------------
Date: Thu, 4 Nov 1999 15:36:16 -0800
From: Tom Phoenix <rootbeer@redcat.com>
Subject: Re: howto download a GIF from a URI?
Message-Id: <Pine.GSO.4.10.9911041535000.29670-100000@user2.teleport.com>
On Thu, 4 Nov 1999, bmlam wrote:
> Can someone please point out how I can download image files found in a
> URI and save thme as binary files?
I think you want LWP from CPAN. Cheers!
--
Tom Phoenix Perl Training and Hacking Esperanto
Randal Schwartz Case: http://www.rahul.net/jeffrey/ovs/
------------------------------
Date: Thu, 04 Nov 1999 15:12:40 -0800
From: David Cassell <cassell@mail.cor.epa.gov>
Subject: Re: Inplace editing and safety
Message-Id: <382212E8.104F5AB4@mail.cor.epa.gov>
Uri Guttman wrote:
[snip of good advice]
> something like (untested):
>
> #!/bin/perl -pi.bak
>
> BEGIN{ $SIG{ INT } = sub { $sig_int++ } ;
Hmmm. Missing a closing curly?
> close( ARGVOUT ) && exit if $sig_int && eof) ;
Double hmmm. eof or eof() are legal, but what's this eof) thing?
> blah edit blah
The PSI::ESP module tells me that this part really does work
when you include your well-written subs 'blah' and 'edit'. :-)
David
--
David Cassell, OAO cassell@mail.cor.epa.gov
Senior computing specialist
mathematical statistician
------------------------------
Date: 04 Nov 1999 19:35:24 -0500
From: Uri Guttman <uri@sysarch.com>
Subject: Re: Inplace editing and safety
Message-Id: <x7ogd97p2r.fsf@home.sysarch.com>
>>>>> "DC" == David Cassell <cassell@mail.cor.epa.gov> writes:
DC> Uri Guttman wrote: [snip of good advice]
>> something like (untested):
>> BEGIN{ $SIG{ INT } = sub { $sig_int++ } ;
DC> Hmmm. Missing a closing curly?
covered by the untested pragma as is shown above.
>> close( ARGVOUT ) && exit if $sig_int && eof) ;
DC> Double hmmm. eof or eof() are legal, but what's this eof) thing?
ditto.
>> blah edit blah
DC> The PSI::ESP module tells me that this part really does work when
DC> you include your well-written subs 'blah' and 'edit'. :-)
go to cpan to find those. all modules have a blah method. and you can
edit their code.
uri
--
Uri Guttman --------- uri@sysarch.com ---------- http://www.sysarch.com
SYStems ARCHitecture, Software Engineering, Perl, Internet, UNIX Consulting
The Perl Books Page ----------- http://www.sysarch.com/cgi-bin/perl_books
The Best Search Engine on the Net ---------- http://www.northernlight.com
------------------------------
Date: Thu, 04 Nov 1999 23:20:28 GMT
From: kragen@dnaco.net (Kragen Sitaker)
Subject: Re: It is always like this here?
Message-Id: <0xoU3.28525$23.1514419@typ11.nn.bcandid.com>
In article <slrn81qof0.vrq.mgjv@wobbie.heliotrope.home>,
Martien Verbruggen <mgjv@comdyn.com.au> wrote:
>On 31 Oct 1999 22:22:03 -0800,
> Mike Coffin <mhc@Eng.Sun.COM> wrote:
>> For better or worse, Perl, like Basic, has become a computer language
>> for non-programmers. It's used by system administrators, web-page
>> designers, and high school English teachers.
>
>That is the issue. It may be true that English teachers want to use
>Perl, but if they can't be bothered to put the same amount of work into
>it that a professional programmer has, then they never will be very
>good.
>
>It is no more than reasonable that we expect from someone who wants to
>program that they behave like a programmer, or at least are willing to
>learn how to be one. And if they aren't willing, they should pay someone
>who is.
Well, a lot of those English teachers have already written programs.
Most of them have written them in Microsoft Excel, or possibly even
Visual Basic. The fact remains that they produce useful programs.
One of the two most significant trends in the history of computers has
been a continuous decrease in the level of skill required to do useful
things with them, which often requires you to write programs. I think
this is a good thing, and Perl is surely part of it. (The other
significant trend is Moore's Law, which is not as important.)
It is verifiably true that learning to read documentation will help
people. But I do suspect that the growing numbers of occasional
programmers will behave differently and come up with different support
systems than those of us who do it full-time.
>Why is it that everyone _thinks_ they can program in Perl without even
>lifting a finger to type m-a-n ?
Maybe because they did that in Excel? I don't know. Maybe they didn't.
--
<kragen@pobox.com> Kragen Sitaker <http://www.pobox.com/~kragen/>
Tue Nov 02 1999
6 days until the Internet stock bubble bursts on Monday, 1999-11-08.
<URL:http://www.pobox.com/~kragen/bubble.html>
------------------------------
Date: 4 Nov 1999 18:36:17 -0600
From: abigail@delanet.com (Abigail)
Subject: Re: length (number of items) of an array
Message-Id: <slrn8249r3.dk.abigail@alexandra.delanet.com>
Dean Karres (karres@southwind.net) wrote on MMCCLVI September MCMXCIII in
<URL:news:WPlU3.28186$23.1489064@typ11.nn.bcandid.com>:
'' What if you have an array of arrays?
''
'' use strict;
''
'' my($i) = 0;
''
'' my(@a) = ( [1,2,3,4],
'' [2,3,4],
'' [3,4],
'' [4],
'' [] );
''
'' print STDOUT "$#a\n";
''
'' This produces "4" as I expect it to -- off by one problems aside for
'' now.
''
'' How do I find the length of any sub-array? What if it were a 3 or N
'' dimensional array?
There's no such thing as an N dimensional array in Perl. You can have
an array of references to arrays, or an array of references of arrays
of references of arrays, etc. A true N dimensional array would be an
N dimensional block, for instance, that is, all rows have the same size.
#!/opt/perl/bin/perl -w
use strict;
my (@a) = ([1, 2, 3, 4],
[2, 3, 4],
[3, 4],
[4],
[]);
print '@a has ' . @a . " rows.\n";
print 'The rows of @a have sizes ',
join (", and " => join (", " => map {scalar @$_} @a [0 .. $#a - 1]),
scalar @{$a [$#a]}), ".\n";
__END__
@a has 5 rows.
The rows of @a have sizes 4, 3, 2, 1, and 0.
Abigail
--
sub camel (^#87=i@J&&&#]u'^^s]#'#={123{#}7890t[0.9]9@+*`"'***}A&&&}n2o}00}t324i;
h[{e **###{r{+P={**{e^^^#'#i@{r'^=^{l+{#}H***i[0.9]&@a5`"':&^;&^,*&^$43##@@####;
c}^^^&&&k}&&&}#=e*****[]}'r####'`=437*{#};::'1[0.9]2@43`"'*#==[[.{{],,,1278@#@);
print+((($llama=prototype'camel')=~y|+{#}$=^*&[0-9]i@:;`"',.| |d)&&$llama."\n");
-----------== Posted via Newsfeeds.Com, Uncensored Usenet News ==----------
http://www.newsfeeds.com The Largest Usenet Servers in the World!
------== Over 73,000 Newsgroups - Including Dedicated Binaries Servers ==-----
------------------------------
Date: 4 Nov 1999 23:15:06 GMT
From: edew@netcom.com (Eric Dew)
Subject: Library files and how to use them -- insert as an FAQ?
Message-Id: <7vt41q$nti$1@nntp2.atl.mindspring.net>
A week ago (or so) I asked how to do the following:
I want to put a common subroutine into a separate file because it will be
used by many scripts, but hey, I don't want to put it as a subroutine in
each of these scripts, especially when the subroutine may change over time.
So, how to do it?
After receiving various suggestions and answers from people, the short answer
is: Look at Ch 5 of the Camel book, in particular, in the section on Modules.
The explicit answer is:
1) Create your library file (called a perl module), UseMe.pm (capitalize
the first letter to follow convention). Place it where you might want to
place library files. In this example, it will be in /usr/local/tools/lib/.
2) In UseMe.pm the content will resemble:
#----Top of file-----
package UseMe;
require Exporter;
@ISA = qw(Exporter);
@Export = qw(this that);
@Export = qw($var1 $var2 $var3);
sub this
{
my($var1,$var2);
<do this to>($var1);
<and this to>($var2);
<return or evaluate or something>;
}
sub that
{
my($var3);
<do that to>($var3);
<return or something>;
}
#----End of file-----
3) In your scripts which need "this" and "that", say, scriptA, include the
following:
@INC = (@INC,"/usr/local/tools/lib"); # to include path to the package
use UseMe;
[. . .]
sub main
{
[. . .]
this();
that();
[. . .]
}
If there are mistakes here, I'd be happy to hear about it.
That should do it, eh?
EDEW
------------------------------
Date: Thu, 4 Nov 1999 16:31:27 -0800
From: Tom Phoenix <rootbeer@redcat.com>
Subject: Re: Library files and how to use them -- insert as an FAQ?
Message-Id: <Pine.GSO.4.10.9911041617240.29670-100000@user2.teleport.com>
On 4 Nov 1999, Eric Dew wrote:
> 1) Create your library file (called a perl module), UseMe.pm
> (capitalize the first letter to follow convention).
Also, choose your module name with care. Avoid making new top-level
names when possible.
> Place it where you might want to place library files. In this
> example, it will be in /usr/local/tools/lib/.
Don't forget to use h2xs to create this file and the others you'll need.
Of course, it creates only the skeleton of the file...
> 2) In UseMe.pm the content will resemble:
>
> #----Top of file-----
>
> package UseMe;
> require Exporter;
> @ISA = qw(Exporter);
> @Export = qw(this that);
> @Export = qw($var1 $var2 $var3);
...but that skeleton will make it easier to get this stuff right. For
example, @Export is just an ordinary array - so that last line wipes out
what you stored into it in the previous line.
> 3) In your scripts which need "this" and "that", say, scriptA, include the
> following:
>
> @INC = (@INC,"/usr/local/tools/lib"); # to include path to the package
> use UseMe;
Oops! Take another look at perlfunc - including the entries for push,
unshift, and use. Then see the lib pragma.
Cheers!
--
Tom Phoenix Perl Training and Hacking Esperanto
Randal Schwartz Case: http://www.rahul.net/jeffrey/ovs/
------------------------------
Date: Fri, 05 Nov 1999 01:35:53 GMT
From: kragen@dnaco.net (Kragen Sitaker)
Subject: Re: Library files and how to use them -- insert as an FAQ?
Message-Id: <ZvqU3.29617$23.1529522@typ11.nn.bcandid.com>
In article <Pine.GSO.4.10.9911041617240.29670-100000@user2.teleport.com>,
Tom Phoenix <rootbeer@redcat.com> wrote:
>On 4 Nov 1999, Eric Dew wrote:
>> Place it where you might want to place library files. In this
>> example, it will be in /usr/local/tools/lib/.
>
>Don't forget to use h2xs to create this file and the others you'll need.
>Of course, it creates only the skeleton of the file...
I only learned about this last weekend.
>> @ISA = qw(Exporter);
>> @Export = qw(this that);
>> @Export = qw($var1 $var2 $var3);
>
>...but that skeleton will make it easier to get this stuff right. For
>example, @Export is just an ordinary array - so that last line wipes out
>what you stored into it in the previous line.
And it's the wrong ordinary array -- you want @EXPORT.
>> @INC = (@INC,"/usr/local/tools/lib"); # to include path to the package
Needs to be done at compile-time so the compiler can find UseMe.pm.
Look at perldoc lib.
>> use UseMe;
>
>Oops! Take another look at perlfunc - including the entries for push,
>unshift, and use. Then see the lib pragma.
Yeah, and listen to Tom. He really knows what he's talking about :)
--
<kragen@pobox.com> Kragen Sitaker <http://www.pobox.com/~kragen/>
Tue Nov 02 1999
6 days until the Internet stock bubble bursts on Monday, 1999-11-08.
<URL:http://www.pobox.com/~kragen/bubble.html>
------------------------------
Date: 16 Sep 99 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 16 Sep 99)
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: The mail to news gateway, and thus the ability to submit articles
| through this service to the newsgroup, has been removed. I do not have
| time to individually vet each article to make sure that someone isn't
| abusing the service, and I no longer have any desire to waste my time
| dealing with the campus admins when some fool complains to them about an
| article that has come through the gateway instead of complaining
| to the source.
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 V9 Issue 1285
**************************************