[12694] in Perl-Users-Digest
Perl-Users Digest, Issue: 103 Volume: 9
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat Jul 10 16:07:19 1999
Date: Sat, 10 Jul 1999 13:05:06 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Sat, 10 Jul 1999 Volume: 9 Number: 103
Today's topics:
Re: ActiveState's PerlScript <cassell@mail.cor.epa.gov>
Re: Changing case local-specifically (Abigail)
Re: FAQ 5.19: I still don't get locking. I just want t (Tony Greenwood)
Re: Formatting a number to a currency string <cassell@mail.cor.epa.gov>
Re: Hacker fouls off All-Star site (Andrew M. Langmead)
Help: creating files through a CGI <scientia@XXXtechnologist.com>
Re: help: using eggs in a skillet (was: help: using qma (Neko)
Re: How to dereference an array reference? (Randal L. Schwartz)
Re: How to dereference an array reference? (Abigail)
Re: Idiot requires help <rick.delaney@home.com>
Re: match multiple lines (Marcel)
one-file Perl for Win32 <jeff@idir.net>
Re: PERLFUNC: tr/// - transliterate a string <cassell@mail.cor.epa.gov>
Re: Speed Differences Perl v. Cold Fusion (brian d foy)
Re: variable "$foo" will not stay shared (brian d foy)
Re: variable "$foo" will not stay shared <cassell@mail.cor.epa.gov>
Re: variable "$foo" will not stay shared (random static)
Re: variable "$foo" will not stay shared <tchrist@mox.perl.com>
Digest Administrivia (Last modified: 1 Jul 99) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Sat, 10 Jul 1999 11:35:08 -0700
From: David Cassell <cassell@mail.cor.epa.gov>
To: "John M. Dlugosz" <john@dlugosz.com>
Subject: Re: ActiveState's PerlScript
Message-Id: <3787925C.7E65C418@mail.cor.epa.gov>
[courtesy e-mail cc'ed to poster]
John M. Dlugosz wrote:
>
> Is there a newsgroup for this, or any other information to be had?
You can ask Perl programming problems here. ActiveState's Perl
counts. It's about as close to the latest stable unix version
as you'll get without installing cygwin32 and building a version
yourself. But.. if you are going to ask Perl questions here,
please follow our guidelines, as set down in the e-mail you
should have received from gnat's autobot. Show your code
(trimmed to the smallest that still runs and shows your problem),
your results and/or errors, and what you wanted to get.
There are also listservs to which you can subscribe just by
going to http://www.activestate.com/support/mailing_lists.htm
and choosing the most relevant list(s) for you.
BTW, ActiveState's PerlScript is an ActiveX scripting engine
which allows one to use Perl with any ActiveX scripting host.
Do you mean Perl or PerlScript?
> I've been using it, and have discovered a lack of documentation, and some
> weird problems.
That's very peculiar. ActiveState Perl comes with more
on-line docs than you can shake a stick at.. assuming one
would want to shake a stick at documents. :-)
Don't you have the entire HTML doc tree? Don't you have the
FAQs? The POD? perldoc? The module documentation? If
you do not, then you do not have Perl. Real copies of Perl
have all that stuff as part of the install.
> For example, why does the File::Find module not work,
> and why does any use of $main::document crash Perl?
File::Find works for everyone else. I'm not being flippant
here. What do you mean 'not work'? Are you using -w and
'use strict'? Are you getting errors? Are you getting
results different from what you expected? You're not giving
me much to work with here.
I've never seen $main::document or anything similar crash
Perl. Try this at the command line:
perl -e "$document=qq(yuck!\n); print $main::document;"
It works, right? If it doesn't then your Perl may be busted.
And why on earth would you *need* to say $main::document ?
Are you defining a global variable somewhere, then creating
a variable with the identical name in some package and
having to tell the difference while in that package?
I think that you're going to have to submit some code and
results in this NG before I believe that the problem is
Perl rather than your code.
David
--
David Cassell, OAO cassell@mail.cor.epa.gov
Senior computing specialist
mathematical statistician
------------------------------
Date: 10 Jul 1999 14:45:07 -0500
From: abigail@delanet.com (Abigail)
Subject: Re: Changing case local-specifically
Message-Id: <slrn7of8l9.h7.abigail@alexandra.delanet.com>
Larry Rosler (lr@hpl.hp.com) wrote on MMCXXXIX September MCMXCIII in
<URL:news:MPG.11f11775a159317d989c97@nntp.hpl.hp.com>:
!! In article <7m6u9u$s6h$0@216.39.141.200> on 10 Jul 1999 07:55:42 GMT,
!! Neko <tgy@chocobo.org> says...
!! ...
!! > Nice. And a variation using map() instead of s///:
!! >
!! > my @left = map chr, ord('A') .. 0xFF;
!! > my @right = map {$_ ne uc $_ ? uc : lc} @left;
!! > eval "sub TR { \$_[0] =~ tr/@left/@right/ }";
!! > $@ and die $@;
!!
!! Definitely cleaner, and a clear Perl-golf winner. But I'll beat it by
!! three [key]strokes:
!!
!! my @right = map {uc ne $_ ? uc : lc} @left;
!!
!! Do you still think constructing the 'tr' is ugly, Jack?
Without a $" = ""? Yeah, I think that's ugly.... It works though.
Abigail
--
srand 123456;$-=rand$_--=>@[[$-,$_]=@[[$_,$-]for(reverse+1..(@[=split
//=>"IGrACVGQ\x02GJCWVhP\x02PL\x02jNMP"));print+(map{$_^q^"^}@[),"\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: Sat, 10 Jul 1999 18:38:39 GMT
From: tony@webscripts.org (Tony Greenwood)
Subject: Re: FAQ 5.19: I still don't get locking. I just want to increment the number in the file. How can I do this?
Message-Id: <3788920d.7958534@news.freeserve.co.uk>
Hey! Tom Christiansen <perlfaq-suggestions@perl.com>
> Anyway, this is what you can do if you can't help yourself.
> flock(FH, LOCK_UN) or die "can't flock numfile: $!";
> close FH or die "can't close numfile: $!";
I thought using close would automatically unlock the file, so while
you are on the subject may I ask if the routine I am using to print a
small (12 cell) array to a file is indeed correct as now I am
a-wondering:)
use Fcntl qw(:flock);
open(DB,">$info") || print "Error: $info $!\n";
#flock(DB, LOCK_EX);
foreach $i (@stuff)
{
print DB "$i\n";
}
close(DB);
I am aware theres more than one way for everything, is this one OK?
------------------------------
Date: Sat, 10 Jul 1999 11:40:30 -0700
From: David Cassell <cassell@mail.cor.epa.gov>
Subject: Re: Formatting a number to a currency string
Message-Id: <3787939E.E87259ED@mail.cor.epa.gov>
Doc Data wrote:
>
> I saw that in the faq but it would appear that it only adds commas and currency
> format also requires 2 zero-filled decimal places.
Yippee! Someone else who read the FAQ first!
That makes you only the second today, I think.
Yes, that part of the FAQ only tells about shoving commas into
a string of numbers. You'll want to look up the printf() and
sprintf() functions, which can do the 2-decimal-place padding
you're looking for. The newest version of the FAQ has this
in it as well, with *corrected* answers, thanks to Uri.
HTH,
David
--
David Cassell, OAO cassell@mail.cor.epa.gov
Senior computing specialist
mathematical statistician
------------------------------
Date: Sat, 10 Jul 1999 18:05:29 GMT
From: aml@world.std.com (Andrew M. Langmead)
Subject: Re: Hacker fouls off All-Star site
Message-Id: <FEo295.30@world.std.com>
"DC" <church@NOSPAMspinn.net> writes:
>Can someone point me to some sample PERL script or code that will enable me
>to write a script of my own to go out to the Web in a similar fashion to
>this guy's script?
I'd start by reading the LWP::Simple or LWP man
pages. <URL:;http://www.perl.com/cgi-bin/cpan_mod?module=LWP> I was
also going to suggest the book "Web Client Progamming With Perl",
published by O'Reilly, but it seems to be out of
print. <URL:http://www.oreilly.com/catalog/webclient/> It does seem to
be part of the Perl CD Bookshelf
<URL:http://www.oreilly.com/catalog/perlcdbs/>
--
Andrew Langmead
------------------------------
Date: Sat, 10 Jul 1999 20:29:48 -0700
From: Scientia <scientia@XXXtechnologist.com>
Subject: Help: creating files through a CGI
Message-Id: <37880FAC.18D4@XXXtechnologist.com>
Help: creating files through a CGI
Hello
Abstract: my CGI is supposed to create new files (not existing files)
but I am not able to make it work!
I mean this: imagine that someone visits my site
and I want him to write some informations.
For reasons that I can't explain here, I need that
the CGI creates a file within my directory where such informations
must be stored (the name of the files depends on the informations
themselves).
Of course, the visitor is not aware that the CGI is creating a file:
he simply fills some fields in a form.
My Perl program is not able to create such file on my web directory!
However, I have Win95/DOS and ActivePerl and in this case the file is
created with no problems.
For example:
open (outf,">created.txt");
print outf "Hello\n";
This works perfectly in ActivePerl with Win95/DOS.
My provider is always busy and he never replies me,
so I must solve this problem by myself (with your help :-).
I can easily program in PERL (as I know also other languages).
But unfortunately I do not know UNIX or LINUX
and I do not even know what is running on my web server!
I guess UNIX, as I can read this informations:
SERVER_SOFTWARE = Apache/1.3.3 (Unix)
I understand that in UNIX and LINUX a file must be created
with the command "touch filename", so I tried to do that
from my Perl program:
system "touch created.txt";
but nothing happens;
I also chmod-ed it:
system "chmod 777 created.txt"
but nothing happens.
My CGI runs with no errors, but the file is not created!
Somewhere I read that I should make the entire directory
readable/writable, but this is not a good idea.
So, what can I do?
Final question: commands like "chmod 777" are the same
for UNIX and LINUX?!
Thanks
Fabrizio
You can e-mail to:
scientia@technologist.com
------------------------------
Date: 10 Jul 1999 18:23:14 GMT
From: tgy@chocobo.org (Neko)
Subject: Re: help: using eggs in a skillet (was: help: using qmail on NT)
Message-Id: <7m832i$okj$0@216.39.141.200>
On 10 Jul 1999 03:08:10 -0500, abigail@delanet.com (Abigail) wrote:
>Ashish Jain (ashishkjain@hotpop.com) wrote on MMCXXXIX September MCMXCIII
>in <URL:news:7m7v33$hbq$1@news.vsnl.net.in>:
>.. I am having problems using qmail on NT. Or if there are any altenative.
>.. Please suggest.
>
>I am having problems using eggs in a skillet. Or if there are any altenative.
>Please suggest.
(my $sister)->buy(
item => 'eggs',
package => 'milk carton',
quart => 1,
)->microwave;
--
Neko | tgy@chocobo.org | Will hack Perl for a moogle stuffy! =^.^=
------------------------------
Date: 10 Jul 1999 11:07:55 -0700
From: merlyn@stonehenge.com (Randal L. Schwartz)
Subject: Re: How to dereference an array reference?
Message-Id: <m1emig74zo.fsf@halfdome.holdit.com>
>>>>> "Calvin" == Calvin <nospam@nospam.nospam> writes:
Content of question deleted. But for the double violation
of having a spamproofed address *and* asking a question that
suggests that Calvin treats comp.lang.perl.misc as a Help Desk,
I present Calvin's real email address in a nice place where
the spammers will be sure to pick it up:
citidancer@hongkong.com
Calvin> --
Calvin> Best regards
Calvin> Calvin
Calvin> http://www.geocities.com/SiliconValley/Code/9129/
And the same to you. Please read news.announce.newusers and
the FAQ for this group before posting again.
<sigh>
--
Name: Randal L. Schwartz / Stonehenge Consulting Services (503)777-0095
Keywords: Perl training, UNIX[tm] consulting, video production, skiing, flying
Email: <merlyn@stonehenge.com> Snail: (Call) PGP-Key: (finger merlyn@teleport.com)
Web: <A HREF="http://www.stonehenge.com/merlyn/">My Home Page!</A>
Quote: "I'm telling you, if I could have five lines in my .sig, I would!" -- me
------------------------------
Date: 10 Jul 1999 14:45:44 -0500
From: abigail@delanet.com (Abigail)
Subject: Re: How to dereference an array reference?
Message-Id: <slrn7of8mh.h7.abigail@alexandra.delanet.com>
Calvin (nospam@nospam.nospam) wrote on MMCXXXIX September MCMXCIII in
<URL:news:7m7u2j$2de$1@hfc.pacific.net.hk>:
&& Hi,
&& For example, i use the method fetchall_arrayref in the statement handle
&& in DBI. Than i got an array reference. But how can i dereference them to get
&& the actually values of the array???
What did man perlref tell you?
Abigail
--
sub f{sprintf'%c%s',$_[0],$_[1]}print f(74,f(117,f(115,f(116,f(32,f(97,
f(110,f(111,f(116,f(104,f(0x65,f(114,f(32,f(80,f(101,f(114,f(0x6c,f(32,
f(0x48,f(97,f(99,f(107,f(101,f(114,f(10,q ff)))))))))))))))))))))))))
-----------== 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: Sat, 10 Jul 1999 18:20:22 GMT
From: Rick Delaney <rick.delaney@home.com>
Subject: Re: Idiot requires help
Message-Id: <37878EB4.5CAC621@home.com>
[posted & mailed]
Tony wrote:
>
> OK- maybe not such an idiot, at least I know where to ask for help !
No, you were right the first time. This is not the place to ask this.
You might want to try a group with 'jobs' in its name.
--
Rick Delaney
rick.delaney@home.com
------------------------------
Date: Sat, 10 Jul 1999 17:21:06 GMT
From: marcel.grunauer@lovely.net (Marcel)
Subject: Re: match multiple lines
Message-Id: <378c7ff6.2535636@enews.newsguy.com>
On Fri, 09 Jul 1999 20:07:23 GMT, infogateinc@usa.net wrote:
>Hi
>I try to extract a range of lines from a file containing patterns on
>different lines. How can I get the lines matching the pattern at the
>very first time? In the following example, I only want:
>abc
>edf
>efg
>
>But If I run the program, I get the second match, too.
>
>The program is like:
>#! /usr/local/bin/perl
>use IO::File;
What for?
>open (DUMP, "text")|| die ("open lynx failed: $text_data\n");
What is $text_data? If you had used
#!/usr/local/bin/perl -w
use strict;
you would have caught that.
And you might want to output $! with the error message so you know
what went wrong.
And what has this got to do with lynx?
>while (<DUMP>) {
> print if (m#abc#i ... m#efg#i );
> }
>close(DUMP);
>
>And the file is like:
>abc
>edf
>efg
>00000
>abc
>assf
>efg
>1111112
>
The output I got was
abc
edf
efg
abc
assf
efg
Which is understandable if you had bothered to read perlop (Range
Operators):
In scalar context, ".." returns a boolean value. The operator is
bistable, like a flip-flop, and emulates the line-range (comma)
operator of sed, awk, and various editors. Each ".." operator
maintains its own boolean state. It is false as long as its left
operand is false. Once the left operand is true, the range operator
stays true until the right operand is true, AFTER which the range
operator becomes false again. (It doesn‘t become false till the next
time the range operator is evaluated. It can test the right operand
and become false on the same evaluation it became true (as in awk),
but it still returns true once. If you don‘t want it to test the right
operand till the next evaluation (as in sed), use three dots ("...")
instead of two.) The right operand is not evaluated while the operator
is in the "false" state, and the left operand is not evaluated while
the operator is in the "true" state.
So, the range operator becomes true as it encounters the first 'abc',
then outputs all lines until 'efg', at which point it becomes false.
It stays false until it encounters the next 'abc', and stays true
until the next 'efg'.
Marcel
--
perl -e 'print unpack(q$u$,q$82G5S="!!;F]T:&5R(%!E<FP@2&%C:V5R$)'
------------------------------
Date: Sat, 10 Jul 1999 14:01:21 -0500
From: "Jeff Robertson" <jeff@idir.net>
Subject: one-file Perl for Win32
Message-Id: <xKMh3.563$U6.4195@newsfeed.slurp.net>
I need to either build or find an installation of Perl for Windows NT and
Windows 95 that consists of nothing but the interpreter, PERL.EXE. No .DLLs,
no modules. The reason I want to do this is because I want to call Perl from
batch files that are going to run on a lot of 95 and NT machines that have
no Perl installed. I want the batch file to be able to call PERL.EXE from a
network share, like this:
\\MY_NT_SERVER\MY_SHARE\PERL.EXE -e "print qq(Hello world\n)"
I have found that the normal Perl distributions for Win32 require certain
.DLLs in order for the interpreter to work, and thus I have had to put the
DLLs on the network and add the shared directory into the %PATH% variable.
This is a problem under Win95 where the DOS prompt (and thus the batch file)
has limited space for environment variables.
So what I want is a PERL.EXE that will run all by itself, with no other
files required. I assume that the best way to do this is to build it from
the sources, but I have little experience compiling anything under NT or 95.
Any advice or help would be appreciated, especially if someone has already
done what I want to do.
------------------------------
Date: Sat, 10 Jul 1999 11:43:03 -0700
From: David Cassell <cassell@mail.cor.epa.gov>
Subject: Re: PERLFUNC: tr/// - transliterate a string
Message-Id: <37879437.1DAE2BC0@mail.cor.epa.gov>
Tom Christiansen wrote:
> [snippety]
> NAME
> tr/// - transliterate a string
>
> SYNOPSIS
> tr///
>
> DESCRIPTION
> The transliteration operator
, also known as the translation operator to those Perl
troublemakers who are just bucking Larry and Tom.
> Same as `y///'. See the perlop
> manpage.
David
--
David Cassell, OAO cassell@mail.cor.epa.gov
Senior computing specialist
mathematical statistician
------------------------------
Date: Sat, 10 Jul 1999 13:20:33 -0500
From: brian@pm.org (brian d foy)
Subject: Re: Speed Differences Perl v. Cold Fusion
Message-Id: <brian-1007991320340001@218.chicago-03-04rs.il.dial-access.att.net>
In article <Pine.GSO.4.05.9907092041480.18443-100000@chaos.wustl.edu>,
elaine ashton <elaine@chaos.wustl.edu> wrote:
> > I'm having difficulty retrieving any information on comparisons between
> > the two development environments. Any time Cold Fusion has been
> > compared, it has been against other commercial products.
>
> I am currently working on such a document. I've taken your email address
> and will forward you a copy when I have something ready.
>
> > Has anyone had any experience in comparing CF v. PERL for speed in
> > processing CGI type scripts?
>
> Speed is really dependent on many different factors so benchmarks aren't
> always reliable in giving you good information for your specific
> situation. What architecture? What platform? What webserver? Ram?
> Filesystems? etc.
i have seen one comparison like this in a trade magazine, although i'm
not going to let on which one it was. i've noticed that when these
comparisons are made that they are fixed - that is, the author
tries to push one particular product. for example, if Sun wants to
compare Java to Perl, it will compare Java servlets to vanilla CGI
script done in Perl (rather than mod_perl). another heinous trick to
compare something like ASP running on a multi-processor intel platform
against a vanilla CGI script in Perl on a Sparc 2.
remember that any benchmark is usually biased. even the ones that
show Perl way out in front of the pack are probably biased towards
Perl.
--
brian d foy
CGI Meta FAQ <URL:http://www.smithrenaud.com/public/CGI_MetaFAQ.html>
Perl Monger Hats! <URL:http://www.pm.org/clothing.shtml>
------------------------------
Date: Sat, 10 Jul 1999 13:10:48 -0500
From: brian@pm.org (brian d foy)
Subject: Re: variable "$foo" will not stay shared
Message-Id: <brian-1007991310480001@218.chicago-03-04rs.il.dial-access.att.net>
In article <slrn7oeq37.p1.rand@localhost.localdomain>,
random_static@bigfoot.com wrote:
> writing CGI scripts using CGI.pm and CGI::Carp, i'm getting warnings
> saying `Variable "$foo" will not stay shared at...' when calling a
> subroutines in a homebrew module at various places.
are you using mod_perl?
--
brian d foy
CGI Meta FAQ <URL:http://www.smithrenaud.com/public/CGI_MetaFAQ.html>
Perl Monger Hats! <URL:http://www.pm.org/clothing.shtml>
------------------------------
Date: Sat, 10 Jul 1999 11:49:42 -0700
From: David Cassell <cassell@mail.cor.epa.gov>
To: random_static@bigfoot.com
Subject: Re: variable "$foo" will not stay shared
Message-Id: <378795C6.A584251E@mail.cor.epa.gov>
[courtesy cc to poster, since he didn't mung his Reply-to address]
random static wrote:
>
> writing CGI scripts using CGI.pm and CGI::Carp, i'm getting warnings
> saying `Variable "$foo" will not stay shared at...' when calling a
> subroutines in a homebrew module at various places.
>
> what does this warning mean? how do i squash it? what sort of potential
> error behaviour should i be looking out for in the meantime?
Well, if you had looked in perldiag, you would have found
all your answers in a matter of seconds. And if that's
inconvenient, you could put 'use diagnostics;' in you program
and have it look the answer up for you [pretty nifty, huh].
Here's what it says in perldiag:
---------------------------------------------------------------
Variable "%s" will not stay shared
(W) An inner (nested) named subroutine is referencing a lexical variable defined
in an outer subroutine.
When the inner subroutine is called, it will probably see the value of the outer
subroutine's variable as it was before and during the *first* call to the outer
subroutine; in this case, after the first call to the outer subroutine is
complete, the inner and outer subroutines will no longer share a common value
for the variable. In other words, the variable will no longer be shared.
Furthermore, if the outer subroutine is anonymous and references a lexical
variable outside itself, then the outer and inner subroutines will never share
the given variable.
This problem can usually be solved by making the inner subroutine anonymous,
using the sub {} syntax. When inner anonymous subs that reference variables in
outer subroutines are called or referenced, they are automatically rebound to
the current values of such variables.
-----------------------------------------------------------------
So there you are. A complete explanation of your problem, with
a discussion of the consequences, and a solution. I bet you
feel pretty silly now that you know the answer was on your
hard disk all along...
HTH,
David
--
David Cassell, OAO cassell@mail.cor.epa.gov
Senior computing specialist
mathematical statistician
------------------------------
Date: Sat, 10 Jul 1999 14:57:42 -0400
From: rand@localhost.localdomain (random static)
Subject: Re: variable "$foo" will not stay shared
Message-Id: <slrn7of5t6.g8.rand@localhost.localdomain>
brian d foy,
in <brian-1007991310480001@218.chicago-03-04rs.il.dial-access.att.net>:
>In article <slrn7oeq37.p1.rand@localhost.localdomain>,
>random_static@bigfoot.com wrote:
>>writing CGI scripts using CGI.pm and CGI::Carp, i'm getting warnings
>>saying `Variable "$foo" will not stay shared at...' when calling a
>>subroutines in a homebrew module at various places.
>are you using mod_perl?
yes. (and it's a PITA at times, it seems quite fragile to me - but then,
most of my scripts are fairly buggy until i beat them into submission. i
suppose running them under mod_perl improves their quality by forcing me
to kill more bugs.)
------------------------------
Date: 10 Jul 1999 13:29:43 -0700
From: Tom Christiansen <tchrist@mox.perl.com>
Subject: Re: variable "$foo" will not stay shared
Message-Id: <37879f27@cs.colorado.edu>
[courtesy cc of this posting mailed to cited author]
In comp.lang.perl.misc, random_static@bigfoot.com writes:
:>>writing CGI scripts using CGI.pm and CGI::Carp, i'm getting warnings
:>>saying `Variable "$foo" will not stay shared at...' when calling a
:>>subroutines in a homebrew module at various places.
:
:>are you using mod_perl?
:
:yes. (and it's a PITA at times, it seems quite fragile to me - but then,
:most of my scripts are fairly buggy until i beat them into submission. i
:suppose running them under mod_perl improves their quality by forcing me
:to kill more bugs.)
A recipe from _The Perl Cookbook_ in Chapter 11:
Nesting Subroutines
Problem
Your use of nested subroutines is making Perl give warnings
about some variables that will not stay shared.
Solution
Instead of the interior functions being normal subroutines,
make them closures and temporarily assign them to the typeglob
of the right name to create a localized function.
Discussion
If you are accustomed to using nested subroutines in other
programming languages with their own private variables, you'll
have to work at it a bit in Perl. The intuitive coding of this
kind of thing incurs mysterious warnings about ``will not stay
shared''. For example, this won't work:
sub outer {
my $x = $_[0] + 35;
sub inner { return $x * 19 } # WRONG
return $x + inner();
}
A work-around is the following:
sub outer {
my $x = $_[0] + 35;
local *inner = sub { return $x * 19 };
return $x + inner();
}
Now inner() can only be called from within outer(), because of
the temporary assignments of the closure (anonymous
subroutine). But when it does, it has normal access to the
lexical variable $x from the scope of outer().
This has the interesting effect of creating a function local
to another function, something not normally supported in Perl.
This kind of thing is not necessarily going to win you any
awards for clarity of programming unless your program is being
maintained by a Scheme programmer.
See Also
``Matching Multiple Lines'', ``Parsing Dates/Times from
Strings'', ``Constructing Records'', and ``Sorting a Hash''.
--
/* Force them to make up their mind on "@foo". */
--Larry Wall, from toke.c in the v5.0 perl distribution
------------------------------
Date: 1 Jul 99 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 1 Jul 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.
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 V9 Issue 103
*************************************