[24159] in Perl-Users-Digest
Perl-Users Digest, Issue: 6353 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Apr 1 21:07:10 2004
Date: Thu, 1 Apr 2004 18:05:08 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Thu, 1 Apr 2004 Volume: 10 Number: 6353
Today's topics:
Re: "which perl": Different for root and users <footnipple@indiatimes.com>
Re: "which perl": Different for root and users <krahnj@acm.org>
Re: "which perl": Different for root and users (David Efflandt)
Re: Boxed Perl or ActiveState Perl? <gnari@simnet.is>
Re: DBI:mysql and huge tables ctcgag@hotmail.com
free source for bbs <webmaster@infusedlight.net>
Re: free source for bbs <noreply@gunnar.cc>
Re: free source for bbs <glex_nospam@qwest.invalid>
Re: free source for bbs <tadmc@augustmail.com>
Re: free source for bbs <tadmc@augustmail.com>
Re: free source for bbs <uri.guttman@fmr.com>
Re: free source for bbs <noreply@gunnar.cc>
Re: free source for bbs <matternc@comcast.net>
Re: free source for bbs <uri@stemsystems.com>
Re: free source for bbs <uri@stemsystems.com>
Re: free source for bbs <xxala_qumsiehxx@xxyahooxx.com>
IO::Socket on Solaris9 <mkur@poczta.gazeta.pl>
Re: Looking for Perl regexp powered Editor <doom@kzsu.stanford.edu>
multidimensional hash copy problem <nospam@nospam.org>
Re: multidimensional hash copy problem <ittyspam@yahoo.com>
Re: multidimensional hash copy problem <emschwar@pobox.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Thu, 01 Apr 2004 20:17:41 GMT
From: "sdfgsd" <footnipple@indiatimes.com>
Subject: Re: "which perl": Different for root and users
Message-Id: <FN_ac.390797$jH.5517563@twister.tampabay.rr.com>
"Anno Siegel" <anno4000@lublin.zrz.tu-berlin.de> wrote in message
news:c4hjaj$h48$2@mamenchi.zrz.TU-Berlin.DE...
> sdfgsd <footnipple@indiatimes.com> wrote in comp.lang.perl.misc:
> > Hello again,
> >
> > Just solved one set of problems with a 5.8.0 => 5.8.3 upgrade and now I
have
> > another:
> >
> > When I type 'which perl' I get '/usr/local/bin/perl' for users and
> > '/usr/bin/perl' for root! Is this a Suse/Linux issue or a Perl issue?
I've
> > googled, perldoc'ed and still can't figure this out.
>
> It's a Unix issue.
>
> Presumably users have /usr/local/bin before /usr/bin on their path, and
> root hasn't. This is as it should be. In fact, it isn't a bad idea to
> keep root's perl separate and not have it hop along with every version
> change.
>
> > Incidently, my new Perl executable (compiled as root) is in /usr/bin and
> > user scripts still want /usr/local/bin/perl even when I place
> > '#!/usr/bin/perl' at the top of the script.
>
> I don't understand what you are saying.
Scripts run by users don't execute because there is no longer a Perl
executable in /usr/local/bin. In order for me to run the scripts, I need to
be root!
It does sound like a Linux path issue and I will pursue that line, but if
you have any add'l thoughts on this, I'd sure like to hear them. Thanks for
your reply.
------------------------------
Date: Thu, 01 Apr 2004 22:49:30 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: "which perl": Different for root and users
Message-Id: <406C9C00.EC9884C0@acm.org>
sdfgsd wrote:
>
> "Anno Siegel" <anno4000@lublin.zrz.tu-berlin.de> wrote in message
> >
> > It's a Unix issue.
> >
> > Presumably users have /usr/local/bin before /usr/bin on their path, and
> > root hasn't. This is as it should be. In fact, it isn't a bad idea to
> > keep root's perl separate and not have it hop along with every version
> > change.
>
> Scripts run by users don't execute because there is no longer a Perl
> executable in /usr/local/bin. In order for me to run the scripts, I need to
> be root!
>
> It does sound like a Linux path issue and I will pursue that line, but if
> you have any add'l thoughts on this, I'd sure like to hear them. Thanks for
> your reply.
Make /usr/local/bin/perl a symlink to the perl executable you want the
users to use.
John
--
use Perl;
program
fulfillment
------------------------------
Date: Fri, 2 Apr 2004 01:22:54 +0000 (UTC)
From: efflandt@xnet.com (David Efflandt)
Subject: Re: "which perl": Different for root and users
Message-Id: <slrnc6pg3e.8vn.efflandt@typhoon.xnet.com>
On Thu, 01 Apr 2004 22:49:30 GMT, John W. Krahn <krahnj@acm.org> wrote:
> sdfgsd wrote:
>>
>> "Anno Siegel" <anno4000@lublin.zrz.tu-berlin.de> wrote in message
>> >
>> > It's a Unix issue.
>> >
>> > Presumably users have /usr/local/bin before /usr/bin on their path, and
>> > root hasn't. This is as it should be. In fact, it isn't a bad idea to
>> > keep root's perl separate and not have it hop along with every version
>> > change.
>>
>> Scripts run by users don't execute because there is no longer a Perl
>> executable in /usr/local/bin. In order for me to run the scripts, I need to
>> be root!
>>
>> It does sound like a Linux path issue and I will pursue that line, but if
>> you have any add'l thoughts on this, I'd sure like to hear them. Thanks for
>> your reply.
>
> Make /usr/local/bin/perl a symlink to the perl executable you want the
> users to use.
I originally created that symlink in Linux (which by default is
/usr/bin/perl) so scripts written for a Solaris server at my ISP would
work (default /usr/local/bin/perl). But the path to Perl should be in the
shebang line anyway (first line of script) so it is correct for the
system it is running on. This was especially true when systems had perl4
and Perl5 (and true at my ISP which now has a newer 5 version in
/usr/bin). I also have to remember to change the shebang line for NetBSD
to /usr/pkg/bin/perl.
--
David Efflandt - All spam ignored http://www.de-srv.com/
------------------------------
Date: Thu, 1 Apr 2004 18:30:33 -0000
From: "gnari" <gnari@simnet.is>
Subject: Re: Boxed Perl or ActiveState Perl?
Message-Id: <c4hn1q$4vb$1@news.simnet.is>
"Vittorio" <victor@nospam.invalid> wrote in message
news:3hSac.965$hc5.16660@news3.tin.it...
> On my dual-boot (linux debian testing & windows xp) box under linux I
> have installed debian testing and the deb perl 5.6 packages boxed with
> the debian distribution.
>
> Now, using perl both under linux and win xp I've installed activestate
> perl 5.8 under M$ win and it looks great!
>
> The same activestate site offer a debianized perl-5.8 package which is
> installed under usr/local/bin.
>
> In the opinion of more experienced users than I am is there any
> advantage for me to install activestate perl instead of the standard deb
> packages?
I believe that debian testing is using perl 5.8, so probably the easiest
would be
# apt-get install perl
which will upgrade your perl to the current testing version along with
all dependencies
>
> Can both versions of perl coexist?
yes
>
> If I install activestate perl, could I eliminate the boxed debian perl
> packages (to avoid duplication, of course) or they're essential for the
> installation?
perl is used by many debian administration tools, but I have no idea if
they would happily use a /usr/local one instead of the debian one.
but as one of debian's strong points is its package system and
painless upgrading process, I would stick with their packages.
gnari
------------------------------
Date: 01 Apr 2004 22:28:40 GMT
From: ctcgag@hotmail.com
Subject: Re: DBI:mysql and huge tables
Message-Id: <20040401172840.319$n8@newsreader.com>
Tore Aursand <tore@aursand.no> wrote:
> On Thu, 01 Apr 2004 16:54:11 +0000, ctcgag wrote:
> >> which is not what I want. What could I do to speed up things and
> >> become less memory hungry?
>
> > set the mysql_use_result attribute after prepare but before execute.
>
> Ah. Excellent. This teaches me to read the DBD::mysql documentation,
> not only the DBI documentation.
I had actually just read about it in DuBois's book, but didn't understand
what it meant until I read that question here. Then it all made sense.
>
> One suggestion, though: It's possible to set this "flag" on a per-query
> basis, so that only a few queries are affected;
>
> my $sth = $dbh->prepare('SELECT lots_of_data
> FROM huge',
> {'mysql_use_result' => 1});
I always knew that that second argument to prepare (and selectall*) had to
be good for something! I like the way this code looks better than mine.
However, it is behaviorally the same. When you set the value on the handle
after the prepare, it also only effects that handle. There doesn't seem
to be a way to change the default for an entire connection.
> $sth->execute();
> ...
> $sth->finish();
>
> One should have this in mind, though:
>
> "...the 'mysql_use_result' attribute: This forces the driver to
> use 'mysql_use_result' rather than 'mysql_store_result'. The
> former is faster and less memory consuming, but tends to block
> other processes."
>
> In other words: As long as you're running this script quite stand-alone,
> you could very well go fo the 'mysql_use_result' solution.
It's even a little more flexible than that. As long as you don't
dilly-dally around between the time you execute and the time you read the
last row (or the time you call finish, if you never read through to the
end), it will be fine. Or at least, it will be as good as it gets.
The real problem occurs if you never read to the end and never call finish,
and the handle never falls out of scope. Then your handle is holding a
read lock on the mysql table, blocking all write access to the table. And
once a writer is in queue behind the read lock, all new readers are also
blocked.
Xho
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
------------------------------
Date: Thu, 1 Apr 2004 12:55:02 -0800
From: "Robin" <webmaster@infusedlight.net>
Subject: free source for bbs
Message-Id: <c4i0ng$c8r$1@reader2.nmix.net>
My latest bbs script written in perl is at www.infusedlight.net/bbs - please
feel free to email me if you need help installing it or if you'd like to
make a comment. Also, a working example is at
www.infusedlight.net/design/bbs/bbs.pl
--
Regards,
-Robin
--
webmaster@infusedlight.net
------------------------------
Date: Thu, 01 Apr 2004 23:13:38 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: free source for bbs
Message-Id: <c4i0qm$2japf5$1@ID-184292.news.uni-berlin.de>
Robin wrote:
> My latest bbs script written in perl is at www.infusedlight.net/bbs
> - please feel free to email me if you need help installing it or if
> you'd like to make a comment. Also, a working example is at
> www.infusedlight.net/design/bbs/bbs.pl
Thanks for letting us know.
I noticed at that example page that the posters' email addresses are
displayed in clear-text. That's good. It means that you automatically
gets many new email-friends by using your script.
Since I also know from various threads in this group that you are
well-known for writing robust, secure Perl programs, I'm indeed
tempted to start using the script.
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
------------------------------
Date: Thu, 01 Apr 2004 16:08:01 -0600
From: "J. Gleixner" <glex_nospam@qwest.invalid>
Subject: Re: free source for bbs
Message-Id: <5p0bc.132$D12.45293@news.uswest.net>
Gunnar Hjalmarsson wrote:
> Robin wrote:
>
>> My latest bbs script written in perl is at [No thanks]
>> - please feel free to email me if you need help installing it or if
>> you'd like to make a comment. Also, a working example is at
>
>
> Thanks for letting us know.
>
> I noticed at that example page that the posters' email addresses are
> displayed in clear-text. That's good. It means that you automatically
> gets many new email-friends by using your script.
>
> Since I also know from various threads in this group that you are
> well-known for writing robust, secure Perl programs, I'm indeed
> tempted to start using the script.
>
Especially tempting after seeing:
" 4. I'm hoping to use CGI.pm for my next version, but I still don't
quite understand how to work the thing."
It's also good to see "BBS 2 - Version 1.0.6 (final)".. hopefully this
IS the final version and this person moves on to some other newsgroup
and stops posting pathetic pleas to use the site.
:-)
Another entry in the kill file!
------------------------------
Date: Thu, 1 Apr 2004 16:40:33 -0600
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: free source for bbs
Message-Id: <slrnc6p6j1.44t.tadmc@magna.augustmail.com>
Robin <webmaster@infusedlight.net> wrote:
> My latest bbs script written in perl is at
[snip]
Perl 4 style (ie. 8 year old Perl style). Every variable is a
global variable. No file locking. Hand-rolled form parsing,
complete with security problems as usual.
Please do not post announcements for "hobbiest" code, programs
such as yours hurt more than they help.
Folks can get _good_ code for a bbs already, how is yours "better"?
http://sourceforge.net/projects/nms-cgi/
> feel free to email me if you need help installing it or if you'd like to
> make a comment.
If anyone posts here about your program, I'll be sure to give
them your address.
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Thu, 1 Apr 2004 16:42:41 -0600
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: free source for bbs
Message-Id: <slrnc6p6n1.44t.tadmc@magna.augustmail.com>
Robin <webmaster@infusedlight.net> wrote:
> My latest bbs script written in perl
contains this:
my ($filename, $delimeter) = @_;
> if you'd like to
> make a comment.
I like the comment that Uri made once:
delimeter: noun, scale used to weigh and price cold cuts.
also the unit of length for salamis.
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: 01 Apr 2004 16:38:14 -0500
From: Uri Guttman <uri.guttman@fmr.com>
Subject: Re: free source for bbs
Message-Id: <siscwu4ztm49.fsf@tripoli.fmr.com>
>>>>> "GH" == Gunnar Hjalmarsson <noreply@gunnar.cc> writes:
GH> Since I also know from various threads in this group that you are
GH> well-known for writing robust, secure Perl programs, I'm indeed
GH> tempted to start using the script.
i like these two subs in lib.pl. try them out. do they print more than
one char?
sub out_ln
{
my ( $amt ) = @_;
if ( ! ( $amt ) )
{ $amt = 1; }
print ( "\n" ) x $amt;
}
sub out_tabs
{
my ( $amt ) = @_;
if ( ! ( $amt ) )
{ $amt = 1; }
print ( "\t" ) x $amt;
}
the rest of the code is so funny. it makes matt wright's look good!
redundant and bad cgi parsing. a complex mix of globals and my vars and
strict and warnings are never enables. strange formatting (look a the
subs above. ever seen white spacing like that?). no knowledge of any
perl beyond perl4 stuff it seems.
here is a fun sub. try to not laugh at it. i love the reading back of
the file just written. note the globals again.
sub getfoot
{
my ($footer) = @_;
if (-e "$footer")
{
open (FOOT, "$footer");
@footer = <FOOT>;
close (FOOT);
return (@footer);
}
else
{
open (FOOT, ">$footer");
print FOOT <<END;
</body>
</html>
END
close (FOOT);
open (FOOT, "$footer");
@footer = <FOOT>;
return (@footer);
}
}
i will have to make this code the subject of a talk one day, "how to not
write perl".
uri
------------------------------
Date: Fri, 02 Apr 2004 02:01:48 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: free source for bbs
Message-Id: <c4iaoo$2j1ch7$1@ID-184292.news.uni-berlin.de>
Uri Guttman wrote:
> i like these two subs in lib.pl. try them out. do they print more
> than one char?
>
> sub out_ln
> {
> my ( $amt ) = @_;
> if ( ! ( $amt ) )
> { $amt = 1; }
> print ( "\n" ) x $amt;
> }
>
> sub out_tabs
> {
> my ( $amt ) = @_;
> if ( ! ( $amt ) )
> { $amt = 1; }
> print ( "\t" ) x $amt;
> }
No. But since I'm sure that the author had warnings enabled while
developing the program, the resulting hint generated by Perl has most
certainly been carefully considered. No need to worry.
> the rest of the code is so funny. it makes matt wright's look good!
> redundant and bad cgi parsing. a complex mix of globals and my
> vars and strict and warnings are never enables. strange formatting
> (look a the subs above. ever seen white spacing like that?).
Yeah. The code does have a personal touch. Isn't that charming?
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
------------------------------
Date: Thu, 01 Apr 2004 19:34:31 -0500
From: Chris Mattern <matternc@comcast.net>
Subject: Re: free source for bbs
Message-Id: <Fr6dnTSwrKCKKPHdRVn-jA@comcast.com>
Robin wrote:
<snip>
You can't trick me, I know an April Fool's post when
I see one!
--
Christopher Mattern
"Which one you figure tracked us?"
"The ugly one, sir."
"...Could you be more specific?"
------------------------------
Date: Fri, 02 Apr 2004 00:45:46 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: free source for bbs
Message-Id: <x7n05vky12.fsf@mail.sysarch.com>
>>>>> "GH" == Gunnar Hjalmarsson <noreply@gunnar.cc> writes:
GH> No. But since I'm sure that the author had warnings enabled while
GH> developing the program, the resulting hint generated by Perl has most
GH> certainly been carefully considered. No need to worry.
and it generates 2 warnings for each sub as well! that takes effort and skill!
>> the rest of the code is so funny. it makes matt wright's look good!
>> redundant and bad cgi parsing. a complex mix of globals and my
>> vars and strict and warnings are never enables. strange formatting
>> (look a the subs above. ever seen white spacing like that?).
GH> Yeah. The code does have a personal touch. Isn't that charming?
the code and coder are most definitely touched.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
------------------------------
Date: Fri, 02 Apr 2004 00:48:31 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: free source for bbs
Message-Id: <x7k70zkxwh.fsf@mail.sysarch.com>
>>>>> "TM" == Tad McClellan <tadmc@augustmail.com> writes:
TM> contains this:
TM> my ($filename, $delimeter) = @_;
TM> I like the comment that Uri made once:
TM> delimeter: noun, scale used to weigh and price cold cuts.
TM> also the unit of length for salamis.
i had forgotten about that one. i need to publish a dictionary :)
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
------------------------------
Date: Fri, 02 Apr 2004 01:18:04 GMT
From: "Ala Qumsieh" <xxala_qumsiehxx@xxyahooxx.com>
Subject: Re: free source for bbs
Message-Id: <gb3bc.45087$%x7.21847@newssvr25.news.prodigy.com>
"Uri Guttman" <uri.guttman@fmr.com> wrote in message
news:siscwu4ztm49.fsf@tripoli.fmr.com...
...
> strange formatting (look a the subs above. ever seen
> white spacing like that?).
Python?
:-)
--Ala
------------------------------
Date: Thu, 1 Apr 2004 22:09:24 +0000 (UTC)
From: =?ISO-8859-2?Q?Micha=B3?= Kurowski <mkur@poczta.gazeta.pl>
Subject: IO::Socket on Solaris9
Message-Id: <c4i3uk$1us7$1@news.atman.pl>
[Copy and paste from compl.lang.perl.moderated. Apologies for cross-posting]
I've got a serious trouble with my Solaris 9 (sparc) install. It is
related to ldap config but I will leave out details ;-)
What could be a reason for errors like:
lwp-download: 500 Can't connect to xxx (Bad hostname 'xxx').
generated by LWP software but really coming from IO::Socket::INET ?
I'm sure some strange Solaris wisdom is involved. System resolves
hostnames properly most of the time but some software has got problems
with "getprotobyname" functions. For example:
perl -MIO::Socket::INET -e '$sock = IO::Socket::INET->new(PeerAddr =>
"localhost:smtp(25)"); print 1 . "\n" if($sock->connected);'
works OK but lwp-download returns just these "Bad hostname" errors ...
Your thoughts highly appreciated.
--
Michal Kurowski
<mkur@poczta.gazeta.pl>
------------------------------
Date: 01 Apr 2004 15:27:14 -0800
From: Joseph Brenner <doom@kzsu.stanford.edu>
Subject: Re: Looking for Perl regexp powered Editor
Message-Id: <m33c7n46ul.fsf@crack.nonagon.org>
"Jürgen Exner" <jurgenex@hotmail.com> writes:
> Loup Blanc wrote:
> > I'm looking for a text editor or a word processor that allows the use
> > of perl regexp commands. e.g. s/ / / and the like. The more perl
> > features available, the best it is.
>
> It should be fairly simple to write an elisp extension for emacs which calls
> Perl on the mini-buffer or the edited text.
Indeed. Here's a quote from my .emacs. For me, doing an "Alt-o Alt-f"
run's Damien Conway's "Text::Autoformat" on the selected text
(i.e. "the region"):
(defun doom-run-text-autoformat-on-region (start end)
(interactive "r")
(let ( (command
(format "perl -MText::Autoformat -e'autoformat {right=> %d, all=>1}'"
fill-column)) )
(shell-command-on-region start end command nil t "*error*")
))
(global-set-key "\M-o\M-f" 'doom-run-text-autoformat-on-region)
(Note, this feeds the emacs setting of the "fill-column" -- aka the
right margin -- into the perl command at the location of the %d place
holder.)
There's also another technique out there called "EPL" I believe, that
let's emacs and perl talk to each other using IPC. I haven't played
with it yet.
But as for emacs's regexps... they're quite powerful, but they have
a really irritating syntax:
http://www.grin.net/~mirthles/devnotes/whinery/elisp-regexps.html
------------------------------
Date: Thu, 1 Apr 2004 15:30:48 -0500
From: "Christian Caron" <nospam@nospam.org>
Subject: multidimensional hash copy problem
Message-Id: <c4hu5o$4fj1@nrn2.NRCan.gc.ca>
Hi all,
I need to copy a multidimensional hash to work on the copy without modifying
the original:
#########
#!/usr/local/bin/perl
use strict;
my %zones;
$zones{'zone1'}{'type'} = 'master';
my %ext_zones = %zones;
$ext_zones{'zone1'}{'type'} = 'slave';
print "$zones{'zone1'}{'type'}\n"; # should print 'master'
print "$ext_zones{'zone1'}{'type'}\n"; # should print 'slave'
###########
It prints:
slave
slave
it should have printed:
master
slave
When I try the same thing with a simple hash ($zones{'type'} = master), it
works ok:
###########
#!/usr/local/bin/perl
use strict;
my %zones;
$zones{'type'} = 'master';
my %ext_zones = %zones;
$ext_zones{'type'} = 'slave';
print "$zones{'type'}\n"; # should print 'master'
print "$ext_zones{'type'}\n"; # should print 'slave'
#########
It prints:
master
slave
Anyone has an idea? Is it a Perl bug or feature?
Thanks!
Christian
------------------------------
Date: Thu, 1 Apr 2004 16:22:53 -0500
From: Paul Lalli <ittyspam@yahoo.com>
Subject: Re: multidimensional hash copy problem
Message-Id: <20040401162056.P19862@dishwasher.cs.rpi.edu>
On Thu, 1 Apr 2004, Christian Caron wrote:
> Hi all,
>
> I need to copy a multidimensional hash to work on the copy without modifying
> the original:
>
> #########
>
> #!/usr/local/bin/perl
>
> use strict;
>
> my %zones;
> $zones{'zone1'}{'type'} = 'master';
> my %ext_zones = %zones;
> $ext_zones{'zone1'}{'type'} = 'slave';
> print "$zones{'zone1'}{'type'}\n"; # should print 'master'
> print "$ext_zones{'zone1'}{'type'}\n"; # should print 'slave'
>
Did you try reading the FAQ?
perldoc -q copy
"How do I print out or copy a recursive data structure?"
> Anyone has an idea? Is it a Perl bug or feature?
I'd call it a 'consquence', in that the way Perl creates multidimensional
structures is by using references to the inner structures. Copying a
reference creates a new reference to the same data, so you get the results
you saw.
Paul Lalli
------------------------------
Date: Thu, 01 Apr 2004 14:33:41 -0700
From: Eric Schwartz <emschwar@pobox.com>
Subject: Re: multidimensional hash copy problem
Message-Id: <etoekr7mlhm.fsf@fc.hp.com>
"Christian Caron" <nospam@nospam.org> writes:
> I need to copy a multidimensional hash to work on the copy without modifying
> the original:
Hie thee quickly to perlreftut, and search for 'copy'.
> #########
>
> #!/usr/local/bin/perl
>
> use strict;
>
> my %zones;
> $zones{'zone1'}{'type'} = 'master';
> my %ext_zones = %zones;
This is perhaps a tad subtle if you're not clear on how references
work (and to cure that, see perlreftut and perlref). What's going on
here is that $zones{'zone1'} contains a reference to an anonymous hash
which has one key, 'type'. After you assign %zones to %ext_zones,
%ext_zones has a completely different reference to the same anonymous
hash. Thus, when you change data in that anonymous hash later, both
%zones and %ext_zones reflect it, because they're both pointing to the
same data.
This is known as a 'shallow copy'-- copying only the reference, and
not the data it references. Randal Schwartz (no relation) has a
column on the subject at:
<URL:http://www.stonehenge.com/merlyn/UnixReview/col30.html>.
Here's an ASCII art picture of what your data structure looks like now:
[------------] (key)
[ %zones ]---{'zone1'}-
[------------] \ [----------------] (key)
---[ anonymous hash ]---{'type'}
[------------] (key) / [----------------]
[ %ext_zones ]---{'zone1'}-
[------------]
What you want to do instead is a 'deep copy', which copies not only
the references, but the data they point to. See Randal's article for
a generic deep copy routine that will do what you want for simple
hashes like this.
> When I try the same thing with a simple hash ($zones{'type'} = master), it
> works ok:
Of course it does. In both cases, it copies only the top-level hash.
In the previous example, it shows that the top-level hash references
an anonymous hash, and when you copy it, both copies reference the
same hash. In this example, since both are simple hashes, a shallow
copy is sufficient.
> Anyone has an idea? Is it a Perl bug or feature?
Something my freshman CS profs always drilled into me: if you think
you have found a bug in the {compiler,interpreter,language}, it's
probably your code. Sure, every now and then you'll find a real one
(I've found far too many for my own comfort), but the way to bet is
almost always that you're wrong and the {compiler,interpreter,language}
is right.
Read Randal's article, and perlreftut, and come back if you still have
any questions.
-=Eric
--
Come to think of it, there are already a million monkeys on a million
typewriters, and Usenet is NOTHING like Shakespeare.
-- Blair Houghton.
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>
Administrivia:
#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc. For subscription or unsubscription requests, send
#the single line:
#
# subscribe perl-users
#or:
# unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.
NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.
#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V10 Issue 6353
***************************************