[17341] in Perl-Users-Digest
Perl-Users Digest, Issue: 4763 Volume: 9
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Oct 30 14:15:48 2000
Date: Mon, 30 Oct 2000 11:15:25 -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: <972933325-v9-i4763@ruby.oce.orst.edu>
Content-Type: text
Perl-Users Digest Mon, 30 Oct 2000 Volume: 9 Number: 4763
Today's topics:
global variables and use strict <crassh@my-deja.com>
Re: global variables and use strict (Tad McClellan)
Re: global variables and use strict <mjcarman@home.com>
Re: global variables and use strict (Tom Christiansen)
Re: global variables and use strict (Randal L. Schwartz)
Re: global variables and use strict <ren.maddox@tivoli.com>
hairy regex - embedded parentheses fantomette@my-deja.com
HELP FILE I/O <r.fraser@student.murdoch.edu.au>
Re: HELP FILE I/O <tony_curtis32@yahoo.com>
Re: HELP FILE I/O <ren.maddox@tivoli.com>
Help needed <puccim2@uofs.edu>
Re: how to print to a certian charcter number nobull@mail.com
Re: how to read text from filehandle only if it's there (bowman)
installing Font-TTF-0.18 under W2k schnurmann@my-deja.com
Re: Is there a better way to get "the element before $x (Anno Siegel)
Digest Administrivia (Last modified: 16 Sep 99) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Mon, 30 Oct 2000 15:12:51 GMT
From: chris wallace <crassh@my-deja.com>
Subject: global variables and use strict
Message-Id: <8tk35b$jae$1@nnrp1.deja.com>
Being a good reader of clpm I always use 'use strict' and declare
private variables with 'my'. To create global variables, that can be
accessed by the whole script, I use one of two methods:
1. enclose entire program in {} and declare them at the very begining,
or
2. create named packages.
I tend to use the second, but my questions are:
- are the above methods the only ways to do this?
- what would be the preferred method of the gurus in this newsgroup
(and why)?
In general, these global variables are things that will remain constant
for the entire program, or while running over a particular block of data
(and getting re-initialised for the next block). There can be rather a
lot and I don't want to be explicitly passing them to each and every
sub-routine.
Many thanks,
Chris.
Sent via Deja.com http://www.deja.com/
Before you buy.
------------------------------
Date: Mon, 30 Oct 2000 10:50:12 -0500
From: tadmc@metronet.com (Tad McClellan)
Subject: Re: global variables and use strict
Message-Id: <slrn8vr65j.4dl.tadmc@magna.metronet.com>
On Mon, 30 Oct 2000 15:12:51 GMT, chris wallace <crassh@my-deja.com> wrote:
>Being a good reader of clpm I always use 'use strict' and declare
>private variables with 'my'.
Bravo!
>To create global variables, that can be
>accessed by the whole script, I use one of two methods:
>1. enclose entire program in {} and declare them at the very begining,
Yuck. I'd forget that approach.
That won't scale to multi-file programs (lexical variables
(or lexically scoped dynamic variables) cannot extend across files).
>or
>2. create named packages.
(or use a named package that you _didn't_ create. i.e. "main")
>I tend to use the second, but my questions are:
>- are the above methods the only ways to do this?
3) let them all be in package "main", the default (using dynamic,
rather than the normally-preferred lexical, variables).
To satisfy "use strict" either
a) use vars qw/$global @global/;
or
b) use fully-qualified variable names, eg. $main::$global
$::global
>- what would be the preferred method of the gurus in this newsgroup
I'm not a guru, but I can program my way out of a wet paper bag,
which is more than enough to be able to post to clpmisc :-(
I vote for 3a)
>(and why)?
1) will work across files ( our() won't )
2) _explicit_ list of which variables I intend to be global.
>In general, these global variables are things that will remain constant
>for the entire program, or while running over a particular block of data
>(and getting re-initialised for the next block). There can be rather a
>lot and I don't want to be explicitly passing them to each and every
>sub-routine.
"Global variables are bad" is not the same as "Never use global variables".
You just need to have a Very Good Reason for using them :-)
I sometimes try to have only a single global hash to encapsulate
global state info that I want to permit wide access to.
use vars qw/%globals/;
$globals{scalarA} = ...
$globals{arrayA} = [ ... ]; # or \@somearray
( of course I would chose a short and inscrutable hash name if
typed a lot (probably %G).
:-)
Then there is only 1 "global variable".
--
Tad McClellan SGML consulting
tadmc@metronet.com Perl programming
Fort Worth, Texas
------------------------------
Date: Mon, 30 Oct 2000 09:55:52 -0600
From: Michael Carman <mjcarman@home.com>
Subject: Re: global variables and use strict
Message-Id: <39FD9A08.42955DA9@home.com>
chris wallace wrote:
>
> Being a good reader of clpm I always use 'use strict' and declare
> private variables with 'my'.
Good. I trust you use -w as well? :)
> To create global variables, that can be accessed by the whole script,
> I use one of two methods:
> 1. enclose entire program in {} and declare them at the very begining,
These aren't really globals, and enclosing your entire script in a block
is not necessary anyway. It's perfectly okay to do this:
#!/usr/local/bin/perl -w
use strict;
my $foo = 'blah';
#...
What this does is create a "file-scoped lexical." As far as you as a
programmer are concerned, it's basically the same as a global because
it's visible throughout your script.
If you want a true global, declare it this way:
use vars qw($FOO);
I don't know that there's a great difference in stand-alone scripts, but
it's important when creating modules. Globals can be exported to other
namespaces. Lexicals are only visible within their enclosing block.
> 2. create named packages.
Sounds like complete overkill for what you're doing.
> I tend to use the second, but my questions are:
> - are the above methods the only ways to do this?
As demonstrated, no.
> - what would be the preferred method of the gurus in this newsgroup
> (and why)?
Make global (via 'use vars') those things which need to be. Use 'my' on
everything else.
> In general, these global variables are things that will remain constant
> for the entire program, or while running over a particular block of
> data (and getting re-initialised for the next block). There can be
> rather a lot and I don't want to be explicitly passing them to each
> and every sub-routine.
It's up to you to decide when to use globals vs. lexicals, and when to
access externals vs. when to pass parameters. A couple rules of thumb
are:
1) Have no more globals than you need (they clutter up the namespace).
Localize everything to the smallest possible enclosing block. This
also helps to minimize memory consumption.
2) Pass parameters into subs instead of accessing externals. This makes
your subs more portable, and thus reusable. It also reduces the
tendancy to write "magic" subs that change things outside of their
own scope. Such code can be difficult to follow and debug.
Of course, these are just guidelines, so exercise some judgement about
when they are and aren't appropriate.
-mjc
------------------------------
Date: 30 Oct 2000 10:05:26 -0700
From: tchrist@perl.com (Tom Christiansen)
Subject: Re: global variables and use strict
Message-Id: <39fdaa56@cs.colorado.edu>
In article <39FD9A08.42955DA9@home.com>,
Michael Carman <mjcarman@home.com> wrote:
>> - what would be the preferred method of the gurus in this newsgroup
>> (and why)?
>
>Make global (via 'use vars') those things which need to be. Use 'my' on
>everything else.
The Camel states that use vars is "in the process of being replaced
by our() declarations. We're not in a terrible hurry to break the
old ways of doing things, but we do think the new ways are prettier."
Aesthetic concerns aside, our() also offers serious technical
advantages as well. I can think of scant reason at best ever to
"use vars" over our(). I suggest that one treat "use vars" as
vaguely deprecated and not encourage it.
--tom
------------------------------
Date: 30 Oct 2000 09:35:35 -0800
From: merlyn@stonehenge.com (Randal L. Schwartz)
Subject: Re: global variables and use strict
Message-Id: <m1em0y6xlk.fsf@halfdome.holdit.com>
>>>>> "Tom" == Tom Christiansen <tchrist@perl.com> writes:
Tom> The Camel states that use vars is "in the process of being replaced
Tom> by our() declarations. We're not in a terrible hurry to break the
Tom> old ways of doing things, but we do think the new ways are prettier."
Tom> Aesthetic concerns aside, our() also offers serious technical
Tom> advantages as well. I can think of scant reason at best ever to
Tom> "use vars" over our(). I suggest that one treat "use vars" as
Tom> vaguely deprecated and not encourage it.
Except to the degree that people are still using 5.5.3 while
(patiently) waiting for 5.6.1 to come out.
There's an awful lot of CPAN modules that won't compile on 5.5.3
simply because someone used "our" instead of "use vars". How
annoying.
--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
------------------------------
Date: 30 Oct 2000 11:08:40 -0600
From: Ren Maddox <ren.maddox@tivoli.com>
Subject: Re: global variables and use strict
Message-Id: <m3n1fml0iv.fsf@dhcp11-177.support.tivoli.com>
chris wallace <crassh@my-deja.com> writes:
> Being a good reader of clpm I always use 'use strict' and declare
> private variables with 'my'. To create global variables, that can be
> accessed by the whole script, I use one of two methods:
> 1. enclose entire program in {} and declare them at the very begining,
> or
Why? A block isn't needed -- the file itself serves the same purpose.
From "perldoc -f my":
A `my' declares the listed variables to be local
(lexically) to the enclosing block, file, or
`eval'.
> 2. create named packages.
You can also use package "main", which can even be omitted:
$main::GLOBAL = 42;
print $::GLOBAL;
> I tend to use the second, but my questions are:
> - are the above methods the only ways to do this?
> - what would be the preferred method of the gurus in this newsgroup
> (and why)?
If everything is in the same file, then lexical variables scoped to
the entire file work fine. If multiple files are involved, then
lexical variables do not work and package variables are needed.
> In general, these global variables are things that will remain constant
> for the entire program, or while running over a particular block of data
> (and getting re-initialised for the next block). There can be rather a
> lot and I don't want to be explicitly passing them to each and every
> sub-routine.
An alternative to having a bunch of global variables is to have a
single hash that contains all of the contains all of the information.
Then, the hash itself can either be global, or can be passed around to
the necessary functions.
--
Ren Maddox
ren@tivoli.com
------------------------------
Date: Mon, 30 Oct 2000 18:53:43 GMT
From: fantomette@my-deja.com
Subject: hairy regex - embedded parentheses
Message-Id: <8tkg3j$vlp$1@nnrp1.deja.com>
hello,
could someone help me decipher the following regex:
s/(<[^>]*?\bhref\s*=)(?:\s*(["'])([^\2]*?)\2|\s*([^\s]*?(?=[\s>])))
/($2?($1 . $2 . &some_function($3) . $2):($1 . &some_function($4)))
/ie;
precisely, i'm not sure what's in $2, $3 and $4 because i get confused
with the embedded parentheses.
thanks for your help,
isabelle
Sent via Deja.com http://www.deja.com/
Before you buy.
------------------------------
Date: Mon, 30 Oct 2000 22:48:14 +0800
From: Fraser <r.fraser@student.murdoch.edu.au>
Subject: HELP FILE I/O
Message-Id: <39FD8A2E.6022FBCC@student.murdoch.edu.au>
Hi just new at Perl;
wanting to write a string in a perl script to a file on my system:-
so far I've got this:
open (FILE,">maina.c") || die "can't open file: $!";
$code = <FILE>; #string to write to file
close (FILE);
open (FILE);
print FILE $code; #write to file????
close (FILE);
The above isn't working, PLEASE HELP!!
Fraser
------------------------------
Date: 30 Oct 2000 08:59:05 -0600
From: Tony Curtis <tony_curtis32@yahoo.com>
Subject: Re: HELP FILE I/O
Message-Id: <87og02l6iu.fsf@limey.hpcc.uh.edu>
>> On Mon, 30 Oct 2000 22:48:14 +0800,
>> Fraser <r.fraser@student.murdoch.edu.au> said:
> Hi just new at Perl; wanting to write a string in a perl
> script to a file on my system:- so far I've got this:
> open (FILE,">maina.c") || die "can't open file: $!";
You open the file for writing (and truncate it in the process).
> $code = <FILE>; #string to write to file close (FILE);
Then you try to read from the file.
hth
t
--
Eih bennek, eih blavek.
------------------------------
Date: 30 Oct 2000 10:34:07 -0600
From: Ren Maddox <ren.maddox@tivoli.com>
Subject: Re: HELP FILE I/O
Message-Id: <m31ywymgow.fsf@dhcp11-177.support.tivoli.com>
Fraser <r.fraser@student.murdoch.edu.au> writes:
> Hi just new at Perl;
> wanting to write a string in a perl script to a file on my system:-
> so far I've got this:
>
You really should enable warnings:
#!/usr/local/bin/perl -w
You should also get into the habit of using the strict pragma -- it
can really help out (though warnings is what's going to help you in
this case):
use strict;
> open (FILE,">maina.c") || die "can't open file: $!";
Opened for writing (and truncated).
> $code = <FILE>; #string to write to file
Attempt to read from filehandle opened for writing. If you had
warnings enabled, you would get:
Filehandle main::FILE opened only for output
Also, with strict enabled, you will want to put a "my" in front of
$code.
> close (FILE);
> open (FILE);
This invokes a fairly seldom-used semantic of open that causes it to
open the file named by the variable $FILE. With warnings, this would
give:
Use of uninitialized value in open
since $FILE is not set.
> print FILE $code; #write to file????
> close (FILE);
Take a look at:
perldoc perlopentut
It will answer your questions and more.
--
Ren Maddox
ren@tivoli.com
------------------------------
Date: Mon, 30 Oct 2000 17:40:00 GMT
From: Michael Pucci <puccim2@uofs.edu>
Subject: Help needed
Message-Id: <8tkbpe$rj5$1@nnrp1.deja.com>
Greetings,
I am trying to set up a site that will required username/password
authenication. Once logged in, they will have access to there info
stored in a database and special things that only members can do.
Also, I would like to set up different triers of password protection
(member and admins). The free web host provider I use is
www.mycgiserver.com they allow full access to cgi-bin but don't support
mysql. I have no idea how to do this and I have searched sites but I
can't find what I exactly need. If anyone can help, PLEASE contact me
at puccim2@uofs.edu. Also, if anyone knows any free web providers that
would be better suited for me please let me know. Thanks so much!
Regards,
Mike
Sent via Deja.com http://www.deja.com/
Before you buy.
------------------------------
Date: 30 Oct 2000 18:12:48 +0000
From: nobull@mail.com
Subject: Re: how to print to a certian charcter number
Message-Id: <u9pukinqov.fsf@wcl-l.bham.ac.uk>
Pavel Hlavnicka <pavel@gingerall.cz> writes upside down, which is a
big clue not to listen to him:
> $myline =~ s/(.{29})N/$1Y/;
> HTH
LOL
BTW, FWIW this changes the first 'N' found in the 30th or subequent
character position to 'Y'.
--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
------------------------------
Date: Mon, 30 Oct 2000 14:24:11 GMT
From: bowman@news.montana.com (bowman)
Subject: Re: how to read text from filehandle only if it's there to read?
Message-Id: <slrn8vr1h5.9vt.bowman@localhost.localdomain>
Kingsley Tart <a@b.c> wrote:
!I'm trying to write some script that reads from a socket but if there's no
!more input, continues round the loop doing other things.
perldoc -f select
Unfortunately, there are two 'select' calls that are not related; you want the
second one in the doc.
------------------------------
Date: Mon, 30 Oct 2000 14:57:16 GMT
From: schnurmann@my-deja.com
Subject: installing Font-TTF-0.18 under W2k
Message-Id: <8tk288$ie1$1@nnrp1.deja.com>
I downloaded the Font-TTF-0.18 file from CPAN. I unzip/untar, do perl
Makefile.PL and nmake. I eventually get this error:
C:\Perl\lib" "-IC:\Perl\lib" C:\Perl\bin\pod2html.bat ].qq[$_>$m{$_}])
==0 or war
n qq(Couldn\047t install $m{$_}\n);" -e "chmod(oct()), $m{$_} or warn
qq(chmod
$m{$_}: $!\n);}" lib/Font/TTF/Vhea.pm
blib/Html/lib/Font/TTF/Vhea.html lib/F
ont/TTF/Manual.pod blib/Html/lib/Font/TTF/Manual.html
lib/Font/TTF/Cmap.pm bl
ib/Html/lib/Font/TTF/Cmap.html lib/Font/TTF/LTSH.pm
blib/Html/lib/Font/TTF/LTS
H.html lib/Font/TTF/Hmtx.pm blib/Html/lib/Font/TTF/Hmtx.html
lib/Font/TTF/Hea
d.pm blib/Html/lib/Font/TTF/Head.html lib/Font/TTF/PCLT.pm
blib/Html/lib/Font
/TTF/PCLT.html lib/Font/TTF/Vmtx.pm blib/Html/lib/Font/TTF/Vmtx.html
lib/Font
/TTF/OldCmap.pm blib/Html/lib/Font/TTF/OldCmap.html
lib/Font/TTF/Table.pm bli
b/Html/lib/Font/TTF/Table.html lib/Font/TTF/Glyf.pm
blib/Html/lib/Font/TTF/Gly
f.html lib/Font/TTF/Hdmx.pm blib/Html/lib/Font/TTF/Hdmx.html
lib/Font/TTF/Pre
p.pm blib/Html/lib/Font/TTF/Prep.html lib/Font/TTF/Post.pm
blib/Html/lib/Font
/TTF/Post.html lib/Font/TTF/Cvt_.pm blib/Html/lib/Font/TTF/Cvt_.html
lib/Font
/TTF/Maxp.pm blib/Html/lib/Font/TTF/Maxp.html lib/Font/TTF/Font.pm
blib/Html/
lib/Font/TTF/Font.html lib/Font/TTF/Ttc.pm
blib/Html/lib/Font/TTF/Ttc.html li
b/Font/TTF/OS_2.pm blib/Html/lib/Font/TTF/OS_2.html
lib/Font/TTF/Name.pm blib
/Html/lib/Font/TTF/Name.html lib/Font/TTF/Kern.pm
blib/Html/lib/Font/TTF/Kern.
html lib/Font/TTF/Glyph.pm blib/Html/lib/Font/TTF/Glyph.html
lib/Font/TTF/Cha
nges blib/Html/lib/Font/TTF/Changes lib/Font/TTF/Utils.pm
blib/Html/lib/Font/
TTF/Utils.html lib/Font/TTF/Fpgm.pm blib/Html/lib/Font/TTF/Fpgm.html
lib/Font
/TTF/Loca.pm blib/Html/lib/Font/TTF/Loca.html lib/Font/TTF/Segarr.pm
blib/Htm
l/lib/Font/TTF/Segarr.html lib/Font/TTF/Hhea.pm
blib/Html/lib/Font/TTF/Hhea.ht
ml' too long
Stop.
It looks like it is trying to send the code via command line. Is there
a way to increase the length of the command line string in W2K? Is
this a bug in the PM's makefile?
Sent via Deja.com http://www.deja.com/
Before you buy.
------------------------------
Date: 30 Oct 2000 14:58:26 -0000
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Is there a better way to get "the element before $x" in an array?
Message-Id: <8tk2ai$f1l$1@lublin.zrz.tu-berlin.de>
Mary Ellen Foster <mef@cogsci.ed.ac.uk> wrote in comp.lang.perl.misc:
>I was just fooling around with a program to do "previous" and "next" links
>on a set of web pages. Basically, I look for the current page in the
>directory list, and then display the page either directly before it or
>directly after it in the listing (the pages are to be shown in alphabetical
>order, conveniently).
>
>Here's how I ended up doing it (pared down from the real script). Note that
>this was just a five-minute hack, but I am still curious if there's a
>better way:
>
> my $cur_letter = "1234.html";
> my @letters = <*.html>;
> for my $i ( 0 .. $#letters ) {
> if( $letters[$i] eq $cur_letter ) {
> my $new_i = $i;
> if( $command eq "prev" && $i > 0 ) {
> $new_i--;
> } elsif( $command eq "next" && $i < $#letters ) {
> $new_i++;
> }
> print "Location: $base_url$letters[$new_i]\n\n";
> exit;
> }
> }
>
>Using $i like that just offends my aesthetic sensibilities somehow... but I
>couldn't see a neat way to use "grep" or something else of that sort on
>this task.
Can't you simply keep the index of the current letter in @letters around?
You could then just increment or decrement it, only checking that the
newly indicated letter actually exists. In a similar vein, you could
use a lookup table that returns the index to a given letter:
my %lookup;
@lookup{ @letters} = ( 0 .. $#letters);.
You may even want to prepare hash tables that give the predecessor and
successor of each item directly:
my ( %succ, %pred);
@succ{ @letters} = @letters[ 1 .. $#letters];
@pred{ @letters} = ( undef, @letters);
Now $succ{ $cur_letter} gives you the successor, or undef if none
exists, and similar for the predecessor.
Anno
------------------------------
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 4763
**************************************