[8336] in Perl-Users-Digest
Perl-Users Digest, Issue: 1953 Volume: 8
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Feb 23 02:07:36 1998
Date: Sun, 22 Feb 98 23:00:41 -0800
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Sun, 22 Feb 1998 Volume: 8 Number: 1953
Today's topics:
Re: Being Nice 2 Nice Beings [Was: Reading The FAQ's et <tchrist@mox.perl.com>
Re: Being Nice 2 Nice Beings [Was: Reading The FAQ's et <Rosie@dozyrosy.demon.co.uk>
Re: better way to do this? <cdkaiser@delete.these.four.words.concentric.net>
Re: better way to do this? <cdkaiser@delete.these.four.words.concentric.net>
Re: better way to do this? (blacktape)
Re: better way to do this? <cdkaiser@delete.these.four.words.concentric.net>
Re: better way to do this? (Sitaram Chamarty)
Re: Calling Perl Scripts <$_=qq!fearless\@NOSPAMio.com!;y/A-Z//d;print>
Re: Calling Perl Scripts (Abigail)
Re: CGI scripts and frames? <rjk@coos.dartmouth.edu>
Re: compile tk402.004 on Solaris 2.6 (Casper H.S. Dik - Network Security Engineer)
Re: Fill a hash ? (short post) (Bart Lateur)
Re: Generic Config-file maintenance <tchrist@mox.perl.com>
Getting started with CGI/Perl <thad@execpc.com>
Re: Getting started with CGI/Perl (Martien Verbruggen)
Glad I bought Effective Perl Programming <shaker@netusa1.net>
Re: Glad I bought Effective Perl Programming (Jonathan Feinberg)
Re: help with global variables needed <tchrist@mox.perl.com>
Re: Help: time a script in hundredth of second for Win3 (Martin Vorlaender)
Re: Help: time a script in hundredth of second for Win3 <tkho@technologist.com>
Re: Help: Warning in string to number conversion. (Bart Lateur)
Re: Help: Warning in string to number conversion. (Martin Vorlaender)
Re: Help: what is the simplest way to return the key of <rjk@coos.dartmouth.edu>
Digest Administrivia (Last modified: 8 Mar 97) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: 23 Feb 1998 00:57:08 GMT
From: Tom Christiansen <tchrist@mox.perl.com>
Subject: Re: Being Nice 2 Nice Beings [Was: Reading The FAQ's etc.: a teacher's perspective...]
Message-Id: <6cqhh4$k88$1@csnews.cs.colorado.edu>
[courtesy cc of this posting sent to cited author via email]
In comp.lang.perl.misc, Richard Caley <spt@cstr.ed.ac.uk> writes:
:Well, I suppose if the FSF ...
And we all remember what "FSF" *really* stands for, right? :-)
--tom
--
Tom Christiansen tchrist@jhereg.perl.com
"There is no idea so sacred that it cannot be questioned, analyzed...
and ridiculed." --Cal Keegan
------------------------------
Date: Sun, 22 Feb 1998 20:35:18 +0000
From: Rosemary I H Powell <Rosie@dozyrosy.demon.co.uk>
Subject: Re: Being Nice 2 Nice Beings [Was: Reading The FAQ's etc.: a teacher's perspective...]
Message-Id: <eb8HySAGwI80EwLw@dozyrosy.demon.co.uk>
In article <6ckih9$1ek$1@vnetnews.value.net>, twod@not.valid writes
>Nathan V. Patwardhan (nvp@shore.net) wrote:
>: Because people are more reactive to the original, me thinks.
>
>Agreed, there is nothing like listening to someone ask a question, pulling
>the manual off the shelf handing it to the individual and uttering the
>phrase in question.
>
>I don't use it in a nasty way as I generally show the user where and what to
>look at and consider. Most people take it as a learning experience and it
>encourages them to think and do some research before they act.
>
And FAQs of all sorts may be found at rtfm.mit.edu, which is quite a
memorable location :-)
Rosemary
-------------------------------------------------------------------
| Rosemary I.H.Powell EMail: Home: rosemary@dozyrosy.demon.co.uk |
| Work: r.i.h.powell@rl.ac.uk |
| http://www.netlink.co.uk/users/dozyrosy/ |
| http://www.dozyrosy.demon.co.uk/ |
-------------------------------------------------------------------
------------------------------
Date: 22 Feb 1998 22:09:08 GMT
From: Cameron Kaiser <cdkaiser@delete.these.four.words.concentric.net>
Subject: Re: better way to do this?
Message-Id: <6cq7m4$rpr@examiner.concentric.net>
I'm better at breaking than fixing, but let's see if my batting average
improves.
mocat@NOSPAM.best.com (blacktape) writes:
>#!/usr/bin/perl
>open(HTMLFILE, "$ARGV[0]") or die "cant open $ARGV[0]: $!";
>@html = <HTMLFILE>;
>foreach $line (@html) {
Turn this all into
while(<ARGV>) {
or even better
while(<>) {
Then you can accomodate multiple files on the command line, and you don't
have to read the entire thing into memory. This means you'll need to
change lines that mess with $line to lines that mess with $_ ...
> $line =~ /<img(.*?)>/i;
... like this one to $_ =~ ... , or better still just plain /<img ...
> $one = $1;
... or better still, (/<img(.*?)>/i) && ($one = $1);
> if ($one !~ /alt\s?=/i) {
> $one =~ /.*?src\s?=\s?"(.*?)"/i;
> $one = $1;
See above.
> $line =~ s/src/alt="$one" src/i;
See above.
What about other tags that have src in them? (See note at bottom of post.)
> print $line;
> } else {
> print $line;
> }
Why not
}
print;
(or, if you're still using $line, print $line;) and dump that else clause?
>}
I assume this is supposed to print alt tags for images, yes? You might
want to consider the possibility of lines that already have an alt tag,
or for lines that have multiple <img ..> tags in them. In that sense,
you'd do better to read in tag-by-tag instead of by line-by-line. A simple
minded method would be to do something like $/ = ">"; although that has
some pitfalls.
Just a suggestion. :-)
--
Cameron Kaiser
cdkaiser at concentric dot net (it hasn't helped the spam yet though)
*** visit the Spectre Server at www.sserv.com
*** C64 software lives! www.computerworkshops.home.ml.org
------------------------------
Date: 22 Feb 1998 22:42:20 GMT
From: Cameron Kaiser <cdkaiser@delete.these.four.words.concentric.net>
Subject: Re: better way to do this?
Message-Id: <6cq9kc$3qs@examiner.concentric.net>
Cameron Kaiser <cdkaiser@delete.these.four.words.concentric.net> writes:
>>#!/usr/bin/perl
>>open(HTMLFILE, "$ARGV[0]") or die "cant open $ARGV[0]: $!";
>>@html = <HTMLFILE>;
>>foreach $line (@html) {
>Turn this all into [ snip ]
Whoops, I broke it already. DON'T take out the /usr/bin/perl line :-)
--
Cameron Kaiser
cdkaiser at concentric dot net (it hasn't helped the spam yet though)
*** visit the Spectre Server at www.sserv.com
*** C64 software lives! www.computerworkshops.home.ml.org
------------------------------
Date: Sun, 22 Feb 1998 22:51:15 GMT
From: mocat@NOSPAM.best.com (blacktape)
Subject: Re: better way to do this?
Message-Id: <34f0aa72.81704522@nntp.best.com>
On 22 Feb 1998 22:09:08 GMT, Cameron Kaiser
<cdkaiser@delete.these.four.words.concentric.net> wrote:
>>open(HTMLFILE, "$ARGV[0]") or die "cant open $ARGV[0]: $!";
>>@html = <HTMLFILE>;
>>foreach $line (@html) {
>
>or even better
>
>while(<>) {
>
>Then you can accomodate multiple files on the command line, and you don't
>have to read the entire thing into memory. This means you'll need to
>change lines that mess with $line to lines that mess with $_ ...
>
>> $line =~ /<img(.*?)>/i;
>
>... like this one to $_ =~ ... , or better still just plain /<img ...
>
>> $one = $1;
the reason i didn't do this is because i try my best to avoid the $_
variable, even if it will take up a bit more memory. i think it's
just easier to keep track of what it's doing if i avoid the $_
variable.
maybe one day i'll warm up to $_, but not just yet. ;)
>See above.
>
>> $line =~ s/src/alt="$one" src/i;
>
>See above.
>What about other tags that have src in them? (See note at bottom of post.)
i thought of this after i posted my code. ;)
>> print $line;
>> } else {
>> print $line;
>> }
>
>Why not
> }
> print;
>
>(or, if you're still using $line, print $line;) and dump that else clause?
if i dump the else, it doesnt print tags that already have an alt="*"
in them. when the code is done, i'm going to have it write the whole
html file with the fixes to another file. (yeah yeah i could use >
but i have to do everything the hard way)
-j
the dumber a person is, the longer it will take for them to find out that you're killing them
------------------------------
Date: 22 Feb 1998 23:38:20 GMT
From: Cameron Kaiser <cdkaiser@delete.these.four.words.concentric.net>
Subject: Re: better way to do this?
Message-Id: <6cqctc$csa@examiner.concentric.net>
uri@sysarch.com writes:
>#!/usr/bin/perl -pi.old
>next unless /<img(.+?)>/i;
>next unless /<img.+?alt\s*=>/i
>s/src\s*=\s*"(.*?)"/alt="$1" src="$1"/i ;
Wow! I'm impressed. :-)
--
Cameron Kaiser
cdkaiser at concentric dot net (it hasn't helped the spam yet though)
*** visit the Spectre Server at www.sserv.com
*** C64 software lives! www.computerworkshops.home.ml.org
------------------------------
Date: 23 Feb 1998 01:19:08 GMT
From: sitaram@diac.com (Sitaram Chamarty)
Subject: Re: better way to do this?
Message-Id: <slrn6f0hru.q7k.sitaram@ltusitaram.diac.com>
On Sun, 22 Feb 1998 00:18:48 GMT, blacktape <mocat@NOSPAM.best.com> wrote:
>Also, I was wondering if there is a way to read AND write to the same
>file... I've tried to do this in the past, but I've ended up having to
>open a seperate file for writing... it'd be so much easier if I could
>just do the whole shebang with one filehandle open.
man perlrun and look for "-i". For what you want, use it (a little unsafely
:-), without any extension.
------------------------------
Date: Sun, 22 Feb 1998 13:16:37 -0800
From: "Creede Lambard" <$_=qq!fearless\@NOSPAMio.com!;y/A-Z//d;print>
Subject: Re: Calling Perl Scripts
Message-Id: <6cq4g3$hlq@bgtnsc01.worldnet.att.net>
I've solved this problem by including the command
<#include virtual="myscript.pl">
where myscript.pl is a working Perl script set up with proper permissions
from a directory that will allow CGI scripts to be run.
Ah, but how do you set up the permissions? Which directory is this? Those
things you'll have to learn from either your Webmaster or a newsgroup that
handles CGI matters. I wouldn't be able to begin to tell you, since it seems
to change from system to system.
--- Creede Lambard
Minister of Irregular Expressions
Programming Republic of Perl
amerar@unsu.com wrote in message <6cq2ve$ebr$1@nnrp2.dejanews.com>...
>
>
>Hello,
>
>From within an HTML document, how do I can a Perl script when no form or
>button is involved?
>
>For example, say I have a homepage:
>
><BODY>
> ... HTML CODE HERE ...
>
> I want to call the perl script here
>
> ... MORE HTML CODE ...
>
></BODY>
>
>How can I call the script unconditionally everytime the page is loaded?
>
>Thank you for your help.
>
>Arthur
>amerar@unsu.com
>http://www.unsu.com
>
>-----== Posted via Deja News, The Leader in Internet Discussion ==-----
>http://www.dejanews.com/ Now offering spam-free web-based newsreading
------------------------------
Date: 22 Feb 1998 22:43:18 GMT
From: abigail@fnx.com (Abigail)
Subject: Re: Calling Perl Scripts
Message-Id: <6cq9m6$6mv$11@client3.news.psi.net>
amerar@unsu.com (amerar@unsu.com) wrote on 1636 September 1993 in
<URL: news:6cq2ve$ebr$1@nnrp2.dejanews.com>:
++
++ From within an HTML document, how do I can a Perl script when no form or
++ button is involved?
The same way as you invoke an Intercal program, a BASIC program
or execute some PostScript.
RTFM of your server. It's not a Perl question.
Abigail
--
perl -wleprint -eqq-@{[ -eqw\\- -eJust -eanother -ePerl -eHacker -e\\-]}-
------------------------------
Date: Sun, 22 Feb 1998 16:46:42 -0500
From: Chipmunk <rjk@coos.dartmouth.edu>
To: Mikael Skogstrom <mikael@techtalk.se>
Subject: Re: CGI scripts and frames?
Message-Id: <34F09CC3.A2904D53@coos.dartmouth.edu>
[posted and mailed]
Mikael Skogstrom wrote:
>
> In article <34ee4166.174477446@news.btinternet.com>,
> Gellyfish@btinternet.com (Jonathan Stowe) wrote:
>
> >Deep down in your heart of hearts you know that this has nothing
> >whatsoever to do with Perl, whatever language was used to produce your
> >CGI program.
> >
> >However if you are using the CGI module (and I sincerely hope you are)
> >I suggest that you read the section in the documentation entitled:
> >
> >WORKING WITH NETSCAPE FRAMES
> >
> >which provides several strategies for working with frames.
>
> I haven't mentioned Perl. But I did write "Mime", which is tightly
> connected with HTTP and therefor with _any_ scripting language.
If you're asking about _any_ scripting language, then maybe you want to post
in comp.lang.misc, not comp.lang.perl.misc.
Here's a clue (you seem to need one): if it's equally related to _all_
scripting languages, then it's not specifically connected to _any one_
scripting language, and posting such a question in a newsgroup for a scripting
language is stupid.
> Since I
> happen to use Perl for scripting, and the frame system I've designed works
> with a plain page but not with a CGI call, I found it allright to post my
> message here.
Yeah, and you happen to be typing your program on a keyboard, right? So why
don't you post in alt.devices.keyboards. And what OS are you running? Maybe
you should post on the newsgroup for that.
Whatever you figure out for your CGI script, you could do the same thing in
any other language (even ignoring the superficial "scripting" designation).
So, since your question has nothing to do with Perl, we find it alright to
flame you.
Have a nice day.
--
_ / ' _ / rjk@coos.dartmouth.edu
( /)//)//)(//)/( chipmunk@m-net.arbornet.org
/ http://www.ziplink.net/~rjk/
------------------------------
Date: 22 Feb 1998 21:24:32 GMT
From: Casper.Dik@Holland.Sun.Com (Casper H.S. Dik - Network Security Engineer)
Subject: Re: compile tk402.004 on Solaris 2.6
Message-Id: <casper.888183153@uk-usenet.uk.sun.com>
[[ PLEASE DON'T SEND ME EMAIL COPIES OF POSTINGS ]]
pingxj@karp.cs.albany.edu (Xiaojun Ping) writes:
>I am trying to compile tk402.004 on solaris 2.6 by gcc 2.6.2,
>but I got the following err message,
>/usr/include/sys/select.h:45: warning: this is the location of
>previous definition
>In file include from /usr/include/sys/stream.h:26
> from /usr/include/netinet/in.h:38
> from /u/xiaojun/lib/perl5/sun4-solaris/5.00403/CORE/perl.h:361
> from Bitmap.xs:8:
>/usr/include/sys/model.h: #error "No DATAMODEL_NATIVE specified"
>*** Error code 1
Looks like a bad gcc install.
Did your install gcc for 2.6 (it need to have newly fixed
includes; sinc eyour gcc is kinda old, I guess it's a left
over installation)
Casper
--
Expressed in this posting are my opinions. They are in no way related
to opinions held by my employer, Sun Microsystems.
Statements on Sun products included here are not gospel and may
be fiction rather than truth.
------------------------------
Date: Sun, 22 Feb 1998 22:11:31 GMT
From: bart.mediamind@tornado.be (Bart Lateur)
Subject: Re: Fill a hash ? (short post)
Message-Id: <34f29f3c.3921425@news.tornado.be>
autopen@autopen.com (Laurel Shimer) wrote:
>What is the proper technique for taking data from a single dimensional
>array and shoving it into a hash?
Simply take out a key and value pair, and assign this in the hash.
Repeat until you're through.
Example:
%hash = ( 'A' => 'able', 'B' => 'baker', 'C' => 'charlie);
#example of key/value pair
($key, $value) = ('O', 'omega');
#add it to the hash
$hash{$key} = $value;
Examine the hash, and you'll see that both the old pairs and the new one
are there, but in no particular order.
Also note that assigning a new value to an already existing key replaces
the old value.
$hash{'B'} = 'beta'; # 'B' => 'baker' will be gone
Bart.
------------------------------
Date: 23 Feb 1998 00:57:06 GMT
From: Tom Christiansen <tchrist@mox.perl.com>
Subject: Re: Generic Config-file maintenance
Message-Id: <6cqhh2$k7q$1@csnews.cs.colorado.edu>
[courtesy cc of this posting sent to cited author via email]
In comp.lang.perl.misc,
tgdcuro1@swissptt.ch (Roy Culley) writes:
:> Just make the config file straight perl! Anything else is dubious at best.
:Thats ok for perl hackers but what if the config file is maintained
:by non-programmers?
Then teach them quotes, semicolons, and dollar signs. Even my grandmother
can do this.
To paraphrase Doug Gwyn, Perl was not designed to stop people from doing
stupid things, because that would also stop them from doing clever things.
As with GUIs, you infuriatingly penalize everyone else just because
some people have room-temperature IQs and shouldn't be allowed to feed
themselves.
--tom
--
Tom Christiansen tchrist@jhereg.perl.com
There is no problem so small that it can't be blamed on Datakit --Andrew Hume
------------------------------
Date: Sun, 22 Feb 1998 17:23:35 -0600
From: "Thad Rolling" <thad@execpc.com>
Subject: Getting started with CGI/Perl
Message-Id: <6cqc2r$cqv@newsops.execpc.com>
Hello,
I am getting started using Perl scripts via CGI on my web server. My server
is RedHat 4.0, Apache and Perl 5.003. It looks like I am missing a
configuration somewhere because when I try to run a Perl program through a
web page, I get a "Server Error".
Since I am new to Perl and CGI and Apache (but not HTML), I was wondering if
anyone has a checkoff list of things needed to run Perl CGI programs. A
simple working example would be great too!
Thanks,
Thad
thad@execpc.com
------------------------------
Date: 23 Feb 1998 00:35:19 GMT
From: mgjv@comdyn.com.au (Martien Verbruggen)
Subject: Re: Getting started with CGI/Perl
Message-Id: <6cqg87$hnq$7@comdyn.comdyn.com.au>
In article <6cqc2r$cqv@newsops.execpc.com>,
"Thad Rolling" <thad@execpc.com> writes:
> Since I am new to Perl and CGI and Apache (but not HTML), I was wondering if
> anyone has a checkoff list of things needed to run Perl CGI programs. A
> simple working example would be great too!
http://www.perl.com/CPAN/doc/FAQs/cgi/idiots-guide.html
http://www.perl.com/CPAN/doc/FAQs/cgi/perl-cgi-faq.html
http://www.webthing.com/page.cgi/cgifaq
comp.infosystems.www.authoring.cgi
http://www.boutell.com/faq/
http://hoohoo.ncsa.uiuc.edu/cgi/interface.html
perldoc perlfaq9
And of course, the documentation for the Apache server.
Martien
--
Martien Verbruggen |
Webmaster www.tradingpost.com.au | In the fight between you and the world,
Commercial Dynamics Pty. Ltd. | back the world - Franz Kafka
NSW, Australia |
------------------------------
Date: 23 Feb 1998 00:53:17 GMT
From: "V. Chandrasekhar" <shaker@netusa1.net>
Subject: Glad I bought Effective Perl Programming
Message-Id: <01bd3ff5$7d166900$8faa8bcd@space.netusa1.net>
1. I have been using Effective Perl Programming for a couple of
hours now. I just bought this book. I am glad I did. This is
exactly
what I need at this point in time (having just completed a 2000
line
program in Perl5, having worked on Perl4 a few years ago, having
programmed in several languages for several years.)
2. For those unfamiliar with the book, there are 60 items. You
can
pick and choose what you want to master, in any order. I zeroed
in on the OO items and this was very profitable. I started
posting
in this newsgroup about a month ago. The above 2000 line program
underwent quantum improvements because of some of the help I
received from responders - to mention some off the top of my
head
(at the risk of missing some, for which I apologize): John
Porter, Bart
Lateur. I am sure that Effective Perl Programming is going to
take
me to the next level rather quickly. I wish I had bought this on
the first
week I started working with Perl, during the current stint.
3. Joe Hall calls it the 'bouncing ball' book. I would rather
call it the
ball book or the Hall book (not to be confused with the Wall
book) :-)
4. I also saw the O'Riley Perl Resource Kit CDs (for sale in our
small
midwestern town bookstore). I would be interested in hearing
from people
who have bought this.
5. I also bought Freidl's book (based on the high praise it
received here).
I was delighted to see that it has features of 'programmed
learning'.
However, this has to compete with the ball book for my time the
next
few days.
V. Chandrasekhar
------------------------------
Date: Sun, 22 Feb 1998 21:53:33 -0500
From: jdf@pobox.com (Jonathan Feinberg)
Subject: Re: Glad I bought Effective Perl Programming
Message-Id: <MPG.f5aaebd85da8000989735@news.concentric.net>
[courtesy cc of this posting sent to cited author via email]
shaker@netusa1.net said...
: 1. I have been using Effective Perl Programming for a couple of
: hours now. I just bought this book. I am glad I did. This is
: exactly
: what I need at this point in time (having just completed a 2000
: line
I am proud to be (one of) the first of (probably) many people to say that (1)I
appreciate your review of EPP and other bits of info but that (2) your broken
newsreader makes your ideas difficult to read. Do try to fix the linewrapping
option!
--
Jonathan Feinberg jdf@pobox.com Sunny Brooklyn, NY
in all sincerity
------------------------------
Date: 23 Feb 1998 01:25:27 GMT
From: Tom Christiansen <tchrist@mox.perl.com>
Subject: Re: help with global variables needed
Message-Id: <6cqj67$n09$1@csnews.cs.colorado.edu>
[courtesy cc of this posting sent to cited author via email]
In comp.lang.perl.misc,
twv@rose.hp.com (Terry von Gease) writes:
:Assume a module 'mymod.pm' that contains...
: my $var1;
: my $var2;
: my $gvar1;
:
:assume a perl script 'mymain.pl' that contain
(funny name for a script :-)
: use strict;
: use mymod;
: my $mvar1;
: my $mvar2;
: my $gmvar1;
:
:Now mymod.pm wants to make its variable $gvar1 available to
:whatever module might do a 'use mymod'.
Um, you can't share "my" variables. They're private. Strictly lexical.
Don't listen to anyone telling you naughty caller games. :-) They don't
work but with globals, and you don't have any there.
Here's what you should do:
The file mymain should be executable, and contain:
# mymain - primary executable
package main; # superfluous because it's the executable
use strict;
use MainVars;
use MyMod1;
use MyMod2;
The file MainVars.pm should contain:
# MainVars.pm
package MainVars;
use strict;
require Exporter;
use vars qw/@ISA @EXPORT/;
@ISA = qw(Exporter);
use vars qw/$var1 $var2 $gvar/;
@EXPORT = qw/$var1 $var2 $gvar/;
$var1 = 'init';
$var2 = 'init';
$gvar1 = 'init';
1;
The file MyMod1.pm should contain:
# MyMod1.pm
package MyMod1;
use strict;
use MainVars;
# now you may access MainVar's $var1 $var2, and $var3
The file MyMod2.pm should contain:
# MyMod2.pm
package MyMod2;
use strict;
use MainVars;
# now you may also access MainVar's $var1 $var2, and $var3
When you say "my", you have locked out everyone outside your scope.
For sharing, only (package) globals will do, not (file-scoped) lexicals.
The following is a small bit of explanation on packages and modules I
cranked out this morning destined for the Perl Cookbook, out in early
summer. It's a brain-dump of what I plan to include in the introduction
to the Modules chapter. The complete Table of Contents (the chapter
is finished, but very rough) is also included to give you a taste.
Feedback welcome, but this is a rough draft that I haven't really even
looked over yet.
--tom
Table of Contents
Introduction
Creating Your Own Module
Trapping Errors in require or use
Delaying use Until Run-Time
Making Variables and Functions Private to a Module
Determining Your Caller's Package
Automating Module Clean-Up
Keeping Your Own Module Directory
Using the Selfloader
Using the Autoloader
Overriding Built-in Functions
Reporting Errors and Warnings Like Built-ins
Referring to Packages Indirectly
Using h2ph to Create foo.ph Files
Using h2xs to Make a Module with C Code
Documenting Your Module with Pod
Building and Installing a CPAN Module
Program: Module Template
Program: Finding Versions and Descriptions of Installed Modules
=head1 Introduction
You've written a program with these nifty subroutines. You put a lot
of careful work into them, and they work well. But they're only in
that one program, and you'd like to use a few in other programs, too.
What do you do?
This is where libraries come in. A library is a collection of a loosely
related functions meant to be used by other programs. The file extension
C<.pl> indicates that it's a perl library file, particularly prior to
the version 5 release of Perl.[FOOTNOTE: Systems without the notion of an
execution bit are sometimes configured to assume that C<.pl> indicates a
Perl executable. This is an error, and should be strenuously avoided.]
Examples of Perl library files include I<syslog.pl> and I<chat2.pl>.
Perl libraries--or in fact, any arbitrary file with Perl code in it--can
be loaded in using C<do 'file.pl'> or C<require 'file.pl'>, the latter
being preferred over the former in virtually all situations.
Such libraries worked ok when used by the main program, but when it
came time for one library to use another, it didn't work out well.
So simple Perl libraries like these have been rendered mostly obsolete,
replaced by more modern incarnations called I<modules>. A module is
a Perl library that adheres to extra rules, something like a contract.
This contract helps make code reusable. In fact, the unit of software
reuse in Perl is the module file. Module files always end in I<.pm>,
and are loaded via C<require Modname> or--preferably--with C<use Modname>.
=head2 Packages
Modules want their own space to store names of global identifiers, where
by identifiers we specifically mean subroutines, variables, handles,
and formats. If one module has a variable called $login, it doesn't
want this to interfere with a different module's $login variable, nor
with one in the main program.
Perl groups together related identifiers somewhat like the way related
files are grouped together. In the filesystem, directories group
related files. Just because Joe has a file named I<startup> doesn't
mean that Bob can't also have one by an identical name. The first is
called I</home/joe/startup>, the second I</home/bob/startup>. These are
completely different files sharing the same base name ("startup"), but
with different fully-qualified names. If you are in Bob's home directory
and mention the file I<startup>, the default prefix of the current working
directory means you get I</home/bob/startup>. Bob can still talk about
Joe's file of that name, but he must either fully qualify that name by
supplying a complete path or else change directory there first.
Just as files can be thought of as living in directories, Perl
identifiers reside in I<packages>. Every global identifier in Perl has
a fully-qualified name. If you are currently in a particular package,
you can access its identifiers without fully qualifying them, but to
access those of another package, you have to prefix those identifiers
with the package name.
The C<package> statement is a compiler declaration that sets the default
package for all unqualified global identifiers. This lasts until the end
of the current scope (block, file, or C<eval>). Where the filesystems
uses slashes to separate the directory component from the filename
component, Perl uses a double colon. $Bob::startup is the variable named
$startup in the package Bob, and $Joe::startup is the $startup variable
in package Joe. Just saying $startup by itself, without package name,
means the global variable $startup in the current package.
A module stored in I<Bob.pm> uses package Bob for its global identifiers.
The module exports any of these to the outside world it cares to. From
outside the module, these names all start with C<Bob::>. But because
the first thing the module typically does is use a C<package Bob;>
statement, they don't have to be fully qualified from within that module.
For privacy, modules use the file scope to store lexicals. But enforced
privacy is overrated.
Packages are for used for grouping and organizing of global identifiers.
They are totally unrelated to matters of privacy. Code compiled in
package Bob is free to examine and alter variables in package Joe.
It's useful to think of packages as directories without any permissions
associated with them. If you want privacy, you need to use lexical
variables, not globals. Package variables are always global, and are used
for sharing.
However, if a lexical variable named $startup is visible in the current
scope, then an unqualified $startup always gets the lexical variable,
not the global one. If you want a global variable that's been shadowed
by a lexical, you have to fully qualify it. Remember that lexical
variables are only created with C<my>. The C<local> modifier affects
global (that is, package) variables, not lexicals.
=head2 Exporting
A traditional (not object-oriented) module contains subroutines and
variables that it exports into its caller's namespace. A C<use> statement
imports global identifiers from the module's namespace into that of the
caller. If package Fred imported $Bob::startup, then $Fred::startup
becomes an alias for $Bob::startup. Any mention of the unqualified
global variable $startup in package Fred would get $Fred::startup,
which because of the import is then just an alias for $Bob::startup.
Importing identifiers from one package to another is rather like linking
files from one directory to another. They're still the same file,
but you don't need to fully qualify them.
Because a C<use> statement is a compile-time event, this works a bit like
a C<#include> of a header file in C. Right then and there the compiler
can detect that you've tried to access a non-existent module, or to import
something which that module doesn't export. With a compilation failure,
the interpreter never begins to execute your program at all. With a
C<require>, there is no compile time checking, and no import either.
All module names would have to be fully-qualified.
Another advantage of compile-time C<use> over run-time C<require> is
that function prototypes in the module's subroutines become visible to
the compiler. This is important because only the compiler cares about
prototypes, not the interpreter.
=head2 Other Kinds of Modules
So far we've talked only about traditional modules, those entirely written
in Perl and which export their interface by allowing the caller direct
access to particular subroutines and variables. But a module doesn't have
to written entirely in Perl. It can also have components were written in
XS, Perl's external subroutine interface, translated into C, and finally
compiled into a shared library. Such modules are normally referred to
as I<extension modules>, and are the topic of the recipe on ...
--
Tom Christiansen tchrist@jhereg.perl.com
X-Windows: It was hard to write; it should be hard to use.
--Jamie Zawinski
------------------------------
Date: Sun, 22 Feb 1998 22:13:43 +0100
From: martin@RADIOGAGA.HARZ.DE (Martin Vorlaender)
Subject: Re: Help: time a script in hundredth of second for Win32?
Message-Id: <34f09507.524144494f47414741@radiogaga.harz.de>
Tommy (tkho@technologist.com) wrote:
: I am try to build a stop watch that count the time (in hundredth of
: second) for a script to finish. In Unix, I can use (times)[0], which
: return second up to two decimal place. How to run times on Win32?
Have a look at the Benchmark module, bundled with the standard distribution.
cu,
Martin
--
| Martin Vorlaender | VMS & WNT programmer
Ceterum censeo | work: mv@pdv-systeme.de
Redmondem delendam esse. | http://www.pdv-systeme.de/users/martinv/
| home: martin@radiogaga.harz.de
------------------------------
Date: Sun, 22 Feb 1998 17:20:17 -0800
From: Tommy <tkho@technologist.com>
To: Jonathan Feinberg <jdf@pobox.com>
Subject: Re: Help: time a script in hundredth of second for Win32?
Message-Id: <34F0CED1.DE415F1E@technologist.com>
Jonathan Feinberg wrote:
> tkho@technologist.com said...
> : I am try to build a stop watch that count the time (in hundredth of
> : second) for a script to finish. In Unix, I can use (times)[0], which
> : return second up to two decimal place. How to run times on Win32?
>
> Stop right there! Benchmark.pm comes with perl and works just fine on Win32.
> Here's an example:
>
> #!/usr/bin/perl -w
> use Benchmark;
> my $text = 'Now is the time for...';
> sub foo {
> for (my $i = 0; $i < 1000; $i++) {
> my($beef, $jerky) = $text =~ /^([^.]*)(\.*)$/;
> }
> }
> sub bar {
> for (my $i = 0; $i < 1000; $i++) {
> my($beef, $jerky) = $text =~ /^([^.]+)(\.+)$/;
> }
> }
> timethese( 1000, {
> 'star' => \&foo,
> 'plus' => \&bar,
> });
> __END__
Thank you for your tips. However, I have problem running Benchmark.pm in Win32.
"times" is a command line of the modules. Here is the result of your script.
Benchmark: timing 1000 iterations of plus, star...
times not implemented at F:\Webstuff\perl\lib/Benchmark.pm line 274
Question: Is times a Perl modules or UNIX command?
---
Tommy Ho
Email: tkho@technologist.com
------------------------------
Date: Sun, 22 Feb 1998 23:02:47 GMT
From: bart.mediamind@tornado.be (Bart Lateur)
Subject: Re: Help: Warning in string to number conversion.
Message-Id: <34f6ae09.7710701@news.tornado.be>
Diego Zamboni <zamboni@cs.purdue.edu> wrote:
>However, given that string-numeric conversions are supposed to be common
>thing in Perl, shouldn't there be an easy way of taking the numeric value
>of something without having to resort to this kind of things, and without
>getting noise from -w?
Simply temporarily disable the warnings.
{
local $^W;
#use scalar as numeric any way you want. Example:
$fh +=0 ;
}
Yes, warnings are intended to be helpful, i.e. to warn you when
something is fishy. But sometimes they're just trying too hard.
HTH,
Bart.
------------------------------
Date: Mon, 23 Feb 1998 05:27:11 +0100
From: martin@RADIOGAGA.HARZ.DE (Martin Vorlaender)
Subject: Re: Help: Warning in string to number conversion.
Message-Id: <34f0fa9f.524144494f47414741@radiogaga.harz.de>
Diego Zamboni (zamboni@cs.purdue.edu) wrote:
: jdf@pobox.com (Jonathan Feinberg) writes:
: > Check out perldata, in the section "Scalar Values," for several flavors of
: > detecting "numberish" SVs. HTH.
: Based on what I read there, I'm using this now:
: if($fh =~ /^1$/) { print "One\n"; }
Regexp here is overkill. Just use string comparison
if ($fh eq '1')
: <gripe>
: However, given that string-numeric conversions are supposed to be common
: thing in Perl, shouldn't there be an easy way of taking the numeric value
: of something without having to resort to this kind of things, and without
: getting noise from -w?
: </gripe>
Why would you want to take the numeric value of something that isn't numeric?
If it's user input, it comes as a string, so compare string-wise.
Once you've verified it's numeric, you can use it in a numeric context.
cu,
Martin
--
| Martin Vorlaender | VMS & WNT programmer
Ceterum censeo | work: mv@pdv-systeme.de
Redmondem delendam esse. | http://www.pdv-systeme.de/users/martinv/
| home: martin@radiogaga.harz.de
------------------------------
Date: Sun, 22 Feb 1998 17:19:39 -0500
From: Chipmunk <rjk@coos.dartmouth.edu>
Subject: Re: Help: what is the simplest way to return the key of an associated array?
Message-Id: <34F0A47E.D32B8DE3@coos.dartmouth.edu>
? the platypus {aka David Formosa} wrote:
>
> In <34EFFDE5.8244ACC9@technologist.com> Tommy <tkho@technologist.com> writes:
>
> >What is the simplest and fastest way to return the key of an associated
> >array?
>
> >my %Array= ("A" => 65, "B"=> 66, "C" => 67, "D" => 68);
> >@ANS= &key(VALUE => 67, ARRAY= \%Array);
>
> sub key {
> my %arg=@_;
> my $value=$arg{VALUE};
> my %array=reverse %{$arg{ARRAY}}; #Should realy be called hash
>
> return $array{$value};
> }
>
> when reverse is called on a hash it inversts it and gets the behavour you
> want.
I thought of the same clever solution. There is one important problem with
this code, though; it assumes that the values are unique. Note that the
original poster's solution did not; it returned an array of all the keys with
the specified value.
--
_ / ' _ / rjk@coos.dartmouth.edu
( /)//)//)(//)/( chipmunk@m-net.arbornet.org
/ http://www.ziplink.net/~rjk/
------------------------------
Date: 8 Mar 97 21:33:47 GMT (Last modified)
From: Perl-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 8 Mar 97)
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.
To submit articles to comp.lang.perl.misc (and this Digest), send your
article to perl-users@ruby.oce.orst.edu.
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.
The Meta-FAQ, an article containing information about the FAQ, is
available by requesting "send perl-users meta-faq". The real FAQ, as it
appeared last in the newsgroup, can be retrieved with the request "send
perl-users FAQ". Due to their sizes, neither the Meta-FAQ nor the FAQ
are included in the digest.
The "mini-FAQ", which is an updated version of the Meta-FAQ, is
available by requesting "send perl-users mini-faq". It appears twice
weekly in the group, but is not distributed in the digest.
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 V8 Issue 1953
**************************************