[21748] in Perl-Users-Digest
Perl-Users Digest, Issue: 3952 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Oct 11 00:05:53 2002
Date: Thu, 10 Oct 2002 21:05:09 -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 Thu, 10 Oct 2002 Volume: 10 Number: 3952
Today's topics:
Re: Flag processing <bongie@gmx.net>
flock doesn't _exclusively_ lock everyone from accessin <mike_constant@yahoo.com>
Re: flock related issue ctcgag@hotmail.com
Re: Hello World doesn't work in 5.8?? <pkent77tea@yahoo.com.tea>
Re: ithreads, perl 5.8 and shared objects <troc@netrus.net>
Re: join lines in @array <No_Mail_Address@cox.net>
Re: join lines in @array <uri@stemsystems.com>
Re: join lines in @array <bongie@gmx.net>
Linked Scripts w/ Different Behavior <davebutlerREMOVE@hotmail.com>
Re: Linked Scripts w/ Different Behavior <jurgenex@hotmail.com>
Re: Linked Scripts w/ Different Behavior <ak@freeshell.org.REMOVE>
Re: Multiple Pings/Second <troc@netrus.net>
Re: Need to convert from seconds since Unix epoch to GM <krahnj@acm.org>
Re: Need to convert from seconds since Unix epoch to GM (Tad McClellan)
Re: Re-entrant parser <joe+usenet@sunstarsys.com>
Re: Re-entrant parser <dd@4pro.net>
Re: Re-entrant parser <joe+usenet@sunstarsys.com>
Re: Re-entrant parser <dd@4pro.net>
Returning hashrefs <heather710101@yahoo.com>
Returning hashrefs (Sean McAfee)
Re: Returning hashrefs <jkeen@concentric.net>
Re: Returning hashrefs <krahnj@acm.org>
Re: Returning hashrefs <heather710101@yahoo.com>
Re: Returning hashrefs (Sean McAfee)
Re: Returning hashrefs (Jay Tilton)
Re: Should -i eat my files? <krahnj@acm.org>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Fri, 11 Oct 2002 01:18:50 +0200
From: "Harald H.-J. Bongartz" <bongie@gmx.net>
Subject: Re: Flag processing
Message-Id: <7005705.XKfbHzU9zL@nyoga.dubu.de>
Benjamin Goldberg wrote:
> Harald H.-J. Bongartz wrote:
> [snip]
>> Regexps are compiled at compile-time if they don't contain variables.
>> If they do, Perl compiles them every time they are used, *unless* you
>> tell Perl with the /o modifier that you're absolutely sure that the
>> variable's value won't change over run-time, so Perl can treat the
>> regex like a constant one. So the /o modifier won't do anything
>> useful with regexps that don't contain variables. (It won't do harm
>> either, but it's irritating.)
>
> Actually, on modern perls, perl will cache the compiled regex, and the
> next time the regex is run, it checks if the interpolated string has
> changed, and if not, uses the cached version.
Thanks for the correction, Benjamin. Apparently my knowledge is partly
outdated. ;-)
Ciao,
Harald
--
Harald H.-J. Bongartz <bongie@gmx.net>
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
A World Without Strings Is Chaos.
-- R. Smuntz
------------------------------
Date: Thu, 10 Oct 2002 15:59:26 -0700
From: "Newbie" <mike_constant@yahoo.com>
Subject: flock doesn't _exclusively_ lock everyone from accessing files.
Message-Id: <ao50op$j8dto$1@ID-161864.news.dfncis.de>
"Matt Eberts" <zoips@eesoft.org> wrote in message
news:yijp9.20123$lV3.1948513@newsread1.prod.itd.earthlink.net...
> I'm trying to figure out if this is a bug in ActiveState Perl, or if
> it's because on Linux Perl is ignoring the file lock.
>
> Am I misunderstanding some basic concept of how flock works, or is this
> really a bug in ActiveState's Perl implementation?
using flock doesn't mean to lock the file from other processes who aren't
flocking the same file from reading/writing to it.
flock is merely advisory that means it only locks the processes that are
flocking the same file. Do you know if "copy" uses flock?
------------------------------
Date: 11 Oct 2002 00:52:45 GMT
From: ctcgag@hotmail.com
Subject: Re: flock related issue
Message-Id: <20021010205245.865$h2@newsreader.com>
Matt Eberts <zoips@eesoft.org> wrote:
> I'm trying to figure out if this is a bug in ActiveState Perl, or if
> it's because on Linux Perl is ignoring the file lock.
>
> Here is the simplest test case. On Win2k running ActiveState Perl 5.6.1,
> the script will die attempting to copy test.xml.temp back over test.xml.
Die for what reason? Does windows and/or File::Copy not set
the $? and/or $! variables ?
>
> Am I misunderstanding some basic concept of how flock works, or is this
> really a bug in ActiveState's Perl implementation?
Perhaps windows implements mandatory rather than advisory locking.
Xho
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service
------------------------------
Date: Thu, 10 Oct 2002 23:49:42 GMT
From: pkent <pkent77tea@yahoo.com.tea>
Subject: Re: Hello World doesn't work in 5.8??
Message-Id: <pkent77tea-AA3BD4.00494211102002@news-text.blueyonder.co.uk>
In article <90d82e70.0210091643.2ee7a5f3@posting.google.com>,
milkfilk@yahoo.com (milkfilk) wrote:
> [milkfilk@computer scripts]$ perl -e 'print "hi";'
> [milkfilk@computer scripts]$
>
> Ok ... maybe my shell is screwy.
Probably that's it. The output from perl is being overwritten by your
prompt. Append \n to the string. You can see how easily it's happening
if you look at this output:
dev/area/tmp/perl-5.8.0 > perl -e 'print "hi";'
hidev/area/tmp/perl-5.8.0 > ./perl -e 'print "hi";'
hidev/area/tmp/perl-5.8.0 > ./perl -v
This is perl, v5.8.0 built for ppc-linux
...
I guess your prompt is going back to column #1.
P
--
pkent 77 at yahoo dot, er... what's the last bit, oh yes, com
Remove the tea to reply
------------------------------
Date: Thu, 10 Oct 2002 23:57:43 -0000
From: Rocco Caputo <troc@netrus.net>
Subject: Re: ithreads, perl 5.8 and shared objects
Message-Id: <slrnaqc4tl.8ma.troc@eyrie.homenet>
On Thu, 10 Oct 2002 07:49:19 +0000 (UTC), Ilya Zakharevich wrote:
> This is exactly my point. POE does not scale to more than one
> "extravagant" client. This makes it a toy only. 640K should be
> enough for everything mentality.
>
> The sad think is that I thought about this, and I saw no way to design
> a framework a subclassable event loop - even with best support from
> most advanced OSes... I saw several solutions which are pretty close
> to be implementable, but I was pretty sure that on legacy systems they
> will just testsuites for kernel bugs...
>
> The problem: there is a dispatcher X which knows how to multiplex
> events of types A,B,C; there is another dispatcher Y which knows how
> to multiplex events of types A,D,E. Design the API which allows them
> to cooperate with the dispatch kernel so that the kernel+X+Y can
> multiplex A,B,C,D,E. X and Y know everything about the kernel; X and
> Y do not know anything about each other; kernel know only the things
> X and Y pass it via the API.
Your premise and point are wrong, your Bill Gates quote is insulting,
and while I contend that your "problem" is so contrived as to only
rarely if ever occur in nature, I will still show that it is solvable.
The POE::Loop::* classes are "bridges" in the _Design Patterns_ sense.
They hide the specifics of each event loop behind a single consistent
interface. The rest of POE exists blissfully ignorant of whatever
ugliness they encapsulate.
The bridges combine with the rest of POE through composition. That
is, they export their functions into the package that contains most of
POE's public interface. Bridges may use this fact to publish new
interfaces for their unique features.
Consider a hypothetical POE::Loop::OS2. It would undoubtedly be
written in C and use native OS/2 Control Programming interfaces. It
might also implement an event driven shared memory subsystem. The
subsystem would be useless without a public interface, which the
bridge would also include.
But what of your sad, contrived example that uses two loops? Well,
first you would find a way to run both event loops at once. Did you
use threads? Human sacrifice? Magic? Sleight-of-hand? Hamsters
running in wheels? Was it only possible through the grace of God and
the miracle of prayer? It doesn't matter as long as it works and
implements the bridge spec.
Second, you would create POE::Loop::IlyasContrivedLoopBridge that uses
both event loops together. Finally you would publish a proper CPAN
distribution out of it, with documentation and tests. The end.
I can already hear your plaintive cry: But I wanted to use them as
separate classes and combine them in different ways!
To which I would reply: Fine, then. Although your demands are
arbitrary and capricious, and while they lead to silliness and baroque
software, POE will not stand in your way. Create two partial bridges:
POE::Loop::IlyasContrivedLoopXBridgePiece
POE::Loop::IlyasContrivedLoopYBridgePiece
and ensure that they will work together in the same program (see the
different means listed above). Just be sure that the full bridge
specification is satisfied, and that thread 0 isn't blocked, all
should go well.
And remember: Just because you can't make a thing work doesn't mean
it's impossible.
Thank you for your consideration.
-- Rocco Caputo / troc@pobox.com / poe.perl.org / poe.sf.net
------------------------------
Date: Thu, 10 Oct 2002 22:40:22 GMT
From: Fred <No_Mail_Address@cox.net>
Subject: Re: join lines in @array
Message-Id: <3DA601FF.74C918CE@cox.net>
Uri Guttman wrote:
>
> >>>>> "F" == Fred <No_Mail_Address@cox.net> writes:
>
> F> OK - I used the .= operator on the individual lines, but still no joy
> F> (same result as before):
>
> F> while (<IN>) {
>
> F> if (m/^From -.*?\s/i .. /^$/) {
>
> F> next unless /^(?:From:.+|Date:.+|X-Mailer|From -)/i ;
> F> if (s/^(From:.+|Date:.+|X-Mailer:.+)/$1 == /i ) {
>
> don't do a test and then a s/// on the same thing. you can test s/// for
> success
>
> next unless s/^(From:.+|Date:.+|X-Mailer:.+)/$1 == /i ;
>
> F> chomp ;
> F> $mylist .= $_ ;
>
> that looks fine. you don't show what the input and output are so it is
> hard to figure out what is wrong.
>
> 'same result as before' is not helpful.
>
> F> }
>
> F> push (@bigarray, $mylist) ;
> F> $mylist = "" ;
> F> }
> F> }
>
> there are modules which mung standard mail headers. you should look into
> them as they do all you want and are easy to use. search cpan for them.
>
> uri
>
Sorry - my testing method was wrong, I used:
foreach (@bigarray) {print OUT $_,"\n" };
instead of
print OUT @bigarray ;
So actually it now works fine.....
Modules are OK, but I learn more from using code.
--
Fred
------------------------------
Date: Thu, 10 Oct 2002 23:35:06 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: join lines in @array
Message-Id: <x7wuopn9zq.fsf@mail.sysarch.com>
>>>>> "F" == Fred <No_Mail_Address@cox.net> writes:
F> So actually it now works fine.....
F> Modules are OK, but I learn more from using code.
and you learn more from using modules and solving up your needs instead
of reinventing the wheel.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
----- Stem and Perl Development, Systems Architecture, Design and Coding ----
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
------------------------------
Date: Fri, 11 Oct 2002 02:25:12 +0200
From: "Harald H.-J. Bongartz" <bongie@gmx.net>
Subject: Re: join lines in @array
Message-Id: <1204728.irj0H7XLPO@nyoga.dubu.de>
Fred wrote:
> So actually it now works fine.....
> Modules are OK, but I learn more from using code.
On the other hand, you maybe want to take a look at one of these modules
to find out things your code might miss. (E.g., mail header fields may
span multiple lines.)
Ciao,
Harald
--
Harald H.-J. Bongartz <bongie@gmx.net>
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Sometimes I lie awake at night, and I ask, "Where have I gone wrong?"
Then a voice says to me, "This is going to take more than one night."
-- Charlie Brown, "Peanuts"
------------------------------
Date: Fri, 11 Oct 2002 01:33:19 GMT
From: "Dave Butler" <davebutlerREMOVE@hotmail.com>
Subject: Linked Scripts w/ Different Behavior
Message-Id: <zTpp9.64960$w63.968229@twister.rdc-kc.rr.com>
I have 3 scripts to write which are 90% identical in the code. Rather than
"copy and modify" I was wondering if one script could serve for all 3. It
would have to know which mode it is functioning in and do the appropriate
things for the 10% of the code that is different.
I started thinking of this approach after noticing that gunzip is really
just a link to gzip. The following is from my Cygwin session:
lrwxrwxrwx 1 dave None 19 Sep 19 20:00 gunzip.exe ->
gzip.exe
-rwxr-xr-x 1 dave None 59392 Aug 4 13:47 gzip.exe
Gzip must be able to detect when it is functioning as gunzip. Is this same
approach possible with shell or Perl scripts?
Thanks,
Dave
------------------------------
Date: Thu, 10 Oct 2002 18:43:23 -0700
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: Linked Scripts w/ Different Behavior
Message-Id: <3da62cc2$1@news.microsoft.com>
Dave Butler wrote:
> I have 3 scripts to write which are 90% identical in the code. Rather
> than "copy and modify" I was wondering if one script could serve for
> all 3. It would have to know which mode it is functioning in and do
> the appropriate things for the 10% of the code that is different.
>
> I started thinking of this approach after noticing that gunzip is
> really just a link to gzip. The following is from my Cygwin session:
>
> lrwxrwxrwx 1 dave None 19 Sep 19 20:00 gunzip.exe ->
> gzip.exe
> -rwxr-xr-x 1 dave None 59392 Aug 4 13:47 gzip.exe
>
> Gzip must be able to detect when it is functioning as gunzip. Is this
> same approach possible with shell or Perl scripts?
From "perldoc perlvar":
$0 Contains the name of the program being executed. [...]
jue
------------------------------
Date: Fri, 11 Oct 2002 01:45:15 -0000
From: Andreas =?iso-8859-1?Q?K=E4h=E4ri?= <ak@freeshell.org.REMOVE>
Subject: Re: Linked Scripts w/ Different Behavior
Message-Id: <slrnaqcb9b.ip5.ak@otaku.freeshell.org>
Submitted by "Dave Butler" to comp.unix.shell:
> I have 3 scripts to write which are 90% identical in the code. Rather than
> "copy and modify" I was wondering if one script could serve for all 3. It
> would have to know which mode it is functioning in and do the appropriate
> things for the 10% of the code that is different.
>
> I started thinking of this approach after noticing that gunzip is really
> just a link to gzip. The following is from my Cygwin session:
>
> lrwxrwxrwx 1 dave None 19 Sep 19 20:00 gunzip.exe ->
> gzip.exe
> -rwxr-xr-x 1 dave None 59392 Aug 4 13:47 gzip.exe
>
> Gzip must be able to detect when it is functioning as gunzip. Is this same
> approach possible with shell or Perl scripts?
>
> Thanks,
>
> Dave
>
>
Hmmm... a soft link. That's a bit unusual, isn't it? I thought
hard links were more commonly used for this sort of situations.
Never mind.
Just check $0:
#!/bin/sh
echo "Invoked as " `basename $0`
$ ./thing.sh
Invoked as thing.sh
$ ln thing.sh bar.sh
$ ln thing.sh foo.sh
$ ./bar.sh
Invoked as bar.sh
$ ./foo.sh
Invoked as foo.sh
--
Andreas Kähäri @ New Zealand +------ Have a Unix: netbsd.org
-----------------------------+------ This post ends with :wq
------------------------------
Date: Fri, 11 Oct 2002 02:59:55 -0000
From: Rocco Caputo <troc@netrus.net>
Subject: Re: Multiple Pings/Second
Message-Id: <slrnaqcfks.8ma.troc@eyrie.homenet>
On Thu, 10 Oct 2002 16:51:02 -0400, Benjamin Goldberg wrote:
> Rocco Caputo wrote:
>> I didn't know for sure what he was doing, so I presented this as just
>> one option among others. After all I've seen on IRC, I can't casually
>> dismiss the possibility someone might ping 1500 discrete addresses
>> just to discover hosts on a few corporate subnets.
>
> Uhh, is it even possible to *ping* a broadcast address? And if you
> *could,* the response packet's "sender" surely would be from the machine
> which sent the response, not the broadcast address.
Yes, if it's enabled in your kernel and your routers don't drop them.
And yes, the response will come back with the host address rather than
the broadcast address.
> The logic for testing if ping packet is really one we care about would
> need significant improvement if it's possible to ping one address and
> get the corresponding response from a different address.
It's more interesting than that. A single multicast echo request can
return one reply for each machine that answers. So if you're pinging
broadcast addresses, it's important to wait the full timeout period
even if you get a response.
POE::Component::Client::Ping supports this by matching replies with
requests according to their sequence number.
Another interesting bit: Since pingers all listen on socket 0, they
can hear each other's responses. To avoid collisions, the identifier
field (the 16 bits before the sequence number) usually holds a hash of
the pinger's process ID. Anything that doesn't match is discarded.
-- Rocco Caputo / troc@pobox.com / poe.perl.org / poe.sf.net
------------------------------
Date: Thu, 10 Oct 2002 23:14:42 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: Need to convert from seconds since Unix epoch to GMT
Message-Id: <3DA609C7.23B2735@acm.org>
J Hall wrote:
>
> Dear Perl users,
> Can someone easily tell me how to convert from seconds since
> the Unix epoch (midnight, Jan 1, 1970) to GMT? Seems straightforward,
> but all the docs I keep seeing address other problems. Thanks.
perldoc -f gmtime
John
--
use Perl;
program
fulfillment
------------------------------
Date: Thu, 10 Oct 2002 17:09:36 -0500
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: Need to convert from seconds since Unix epoch to GMT
Message-Id: <slrnaqbul0.4a0.tadmc@magna.augustmail.com>
J Hall <gmnotyet@yahoo.com> wrote:
> Can someone easily tell me how to convert from seconds since
> the Unix epoch (midnight, Jan 1, 1970) to GMT?
perldoc -f gmtime
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: 10 Oct 2002 19:22:37 -0400
From: Joe Schaefer <joe+usenet@sunstarsys.com>
Subject: Re: Re-entrant parser
Message-Id: <m3heftx4jm.fsf@mumonkan.sunstarsys.com>
"Domizio Demichelis" <dd@4pro.net> writes:
> The "process" sub should extract the content between a {a} and {/a}
> block (labels excluded) and pass it to a sub that will return a
> modified copy of the block content itself. Outer blocks should be
> processed first, and inner last.
> If any {a} without the end {/a} is encountered, the sub should be
> called anyway and it should receive a undef content. (See below the
> sequences of values that the "return_changed_content" sub should
> receive. )
[...]
> SEQUENCE that should be passed to "return_changed_content" sub
>
> 1: undef # no block defined
> 2: ' xxx {a} yyy {/a} xxx ' # outer block first
> 3: ' yyy ' # inner block last
> 4: undef # no block defined
> 5: ' eee ' # just one block
>
> 1 2<------3<----->3------->2 4 5<----->5
> aaa {a} bbb {a} xxx {a} yyy {/a} xxx {/a} ccc {a} ddd {a} eee {/a}
This produces the desired output (return_changed_content is the
anonymous sub below).
#!/usr/bin/perl -w
use strict;
use 5.006;
bless my $obj = { count => 0 };
my $string = <<EOT;
aaa {a} bbb {a} xxx {a} yyy {/a} xxx {/a} ccc {a} ddd {a} eee {/a}
EOT
print $obj->parse( $string => sub {
no warnings 'uninitialized';
my $self = shift;
my $count = ++ $self->count;
return "<$count" . $self->parse(@_) . "$count>";
} );
BEGIN {
my $re;
$re = qr[ # $re lifted from perlre
{(\w+)} # open tag block, capture tag
((?> # capture content, no backtracking
(?: [^{]+ | (??{ $re }) ) # scan for nested block
)*) # repeat scan
{/\1} # look for closing tag block
]x;
sub count :lvalue { shift->{count} }
sub parse {
my ($self, $string, $action) = @_;
my $i = 0;
return unless defined $string;
for ($i = index $string, "{a}", $i;
$i>= 0;
$i = index $string, "{a}", $i)
{
my $data = (substr($string, $i) =~ /^$re/)[-1];
my $j = 3 + (defined $data and 4 + length $data);
substr( $string, $i, $j ) = $action->($self,$data,$action);
$i += $j-1;
}
return $string;
}
}
__END__
--
Joe Schaefer "A foolish consistency is the hobgoblin of little minds, adored
by little statesmen and philosophers and divines."
-- Ralph Waldo Emerson
------------------------------
Date: Thu, 10 Oct 2002 20:24:49 -0400
From: "Domizio Demichelis" <dd@4pro.net>
Subject: Re: Re-entrant parser
Message-Id: <ao55pm$jbkm8$1@ID-159100.news.dfncis.de>
I'm very impressed, your code makes me scratch my head! :-)
Just let me a couple of day to digest everything you wrote, for a full
understanding (I hope so :-), then - if it's possible - I will try to
semplify something :-))))
THANK YOU VERRRRY MUCH!
--
-.. --- -- .. --.. .. ---
-.. . -- .. -.-. .... . .-.. .. ...
------------------------------
Date: 10 Oct 2002 21:53:52 -0400
From: Joe Schaefer <joe+usenet@sunstarsys.com>
Subject: Re: Re-entrant parser
Message-Id: <m38z15wxjj.fsf@mumonkan.sunstarsys.com>
"Domizio Demichelis" <dd@4pro.net> writes:
> I'm very impressed, your code makes me scratch my head! :-)
> Just let me a couple of day to digest everything you wrote, for a full
> understanding (I hope so :-), then - if it's possible - I will try to
> semplify something :-))))
>
> THANK YOU VERRRRY MUCH!
You're very welcome. The regexp holds all of the parsing magic;
see the section on (??{code}) expressions in
% perldoc perlre
for help. Here's a simpler version of the code's BEGIN {}
block. The only difference in the regexp is an additional
(?: )? to let s///ge do what the original index/substr loop did.
BEGIN {
my $re;
$re = qr[
{(\w+)}
(?:
((?>
(?: [^{]+ | (??{ $re }) )
)*)
{/\1} )?
]x;
sub count :lvalue { shift->{count} }
sub parse {
my ($self, $string, $action) = @_;
return unless defined $string;
$string =~ s/$re/$action->($self,$2,$action)/ge;
return $string;
}
}
I hope this helps (I also hope it's not a homework problem :).
--
Joe Schaefer "Sacred cows make the best hamburger."
--Mark Twain
------------------------------
Date: Thu, 10 Oct 2002 22:10:01 -0400
From: "Domizio Demichelis" <dd@4pro.net>
Subject: Re: Re-entrant parser
Message-Id: <ao5bv0$jg5e0$1@ID-159100.news.dfncis.de>
and this is a semplified version of your code ;-)
====== CODE ======
#!/usr/bin/perl -w
use strict;
use 5.006;
my $string = 'aaa {a} bbb {a} xxx {a} yyy {/a} xxx {/a} ccc {a} ddd {a} eee
{/a}';
$::re = qr[
{(\w+)}
(?:
((?>
(?: [^{]+ | (??{ $re }) )
)*)
{/\1} )?
]x;
sub parse
{
my $string = shift || return;
$string =~ s/$::re/my_sub($2)/ge;
return $string;
}
sub my_sub
{
no warnings 'uninitialized';
my $count = ++$::c ;
"<$count" . parse( shift() ) . "$count>";
}
print parse( $string );
====== END CODE =======
--
-.. --- -- .. --.. .. ---
-.. . -- .. -.-. .... . .-.. .. ...
------------------------------
Date: Thu, 10 Oct 2002 22:27:05 +0000 (UTC)
From: Da Witch <heather710101@yahoo.com>
Subject: Returning hashrefs
Message-Id: <ao4urp$muh$1@reader1.panix.com>
Just when I think I'm beginning to understand Perl, it throws me a
curve...
sub foo { {1=>2} }
sub bar { {3=>4} }
sub baz { { %{&foo}, %{&bar} } }
sub baz2 { my $t = { %{&foo}, %{&bar} } ; $t }
my ($y, $z) = (&baz, &baz2);
print ">>", ref $y, "<<\n";
print ">>", ref $z, "<<\n";
The output is:
>><<
>>HASH<<
The only difference between baz and baz2 is that in baz2 the return
value is first assigned to a lexical and it is the lexical that is
returned.
What's going on here? Why doesn't ref (&baz) == ref (&baz2) ??
Thanks,
h
------------------------------
Date: Thu, 10 Oct 2002 23:36:05 GMT
From: mcafee@artemis.transmeta.com (Sean McAfee)
Subject: Returning hashrefs
Message-Id: <1034292970.772726@palladium.transmeta.com>
In article <ao4urp$muh$1@reader1.panix.com>,
Da Witch <heather710101@yahoo.com> wrote:
>Just when I think I'm beginning to understand Perl, it throws me a
>curve...
>
>sub foo { {1=>2} }
>sub bar { {3=>4} }
>sub baz { { %{&foo}, %{&bar} } }
>sub baz2 { my $t = { %{&foo}, %{&bar} } ; $t }
>
>my ($y, $z) = (&baz, &baz2);
>print ">>", ref $y, "<<\n";
>print ">>", ref $z, "<<\n";
>
>The output is:
>>><<
>>>HASH<<
Wow, this is tricky! I think I've got it figured out, though.
Since braces can delimit anonymous hash references and code blocks,
Perl has to apply some heuristics to determine which is meant. It looks
like Perl decided that the outermost braces in subroutines foo, bar,
and baz were meant to delimit code blocks, not references as you intended.
So:
&foo returns the list (1, 2). ("=>" is just a fancy comma.)
&bar returns the list (3, 4).
Thus, &baz evaluates the expression (%{1,2}, %{3,4}). The commas in this
expression are in scalar context, so this is the same as (%{2}, %{4}).
Neither %2 nor %4 exist, so both %2 and %4 evaluate to the empty list.
&baz returns a list containing two empty lists, which is the same as a
single empty list. ((),()) is the same as ().
&baz2 returns a single hash reference as expected, since the "my $t = "
lets Perl know that the following braces must be meant to introduce an
anonymous hash constructor, not a code block. (The hash which is
constructed has no elements, though, because (%{&foo}, %{&baz}) evaluates
to the empty list, as described above.)
So, finally, the expression (&baz, &baz2) evaluates to a single-element
list containing the single hash reference returned by &baz2. This hash
reference gets assigned to $y, and $z gets undef.
To get the behavior you expect, you have to disambiguate the blocks in
foo, bar, and baz, either by prepending a '+' to them:
sub foo { +{1=>2} }
...or with a "return":
sub foo { return {1=>2} }
--
Sean McAfee mcafee@transmeta.com
print eval eval eval eval eval eval eval eval eval eval eval eval eval eval
q!q@q#q$q%q^q&q*q-q=q+q|q~q:q? Just Another Perl Hacker ?:~|+=-*&^%$#@!
------------------------------
Date: 10 Oct 2002 22:36:39 GMT
From: "James E Keenan" <jkeen@concentric.net>
Subject: Re: Returning hashrefs
Message-Id: <ao4vdn$irh@dispatch.concentric.net>
"Da Witch" <heather710101@yahoo.com> wrote in message
news:ao4urp$muh$1@reader1.panix.com...
>
> Just when I think I'm beginning to understand Perl, it throws me a
> curve...
>
> sub foo { {1=>2} }
> sub bar { {3=>4} }
> sub baz { { %{&foo}, %{&bar} } }
> sub baz2 { my $t = { %{&foo}, %{&bar} } ; $t }
>
> my ($y, $z) = (&baz, &baz2);
> print ">>", ref $y, "<<\n";
> print ">>", ref $z, "<<\n";
>
> The output is:
> >><<
> >>HASH<<
>
That's not the output I got. I simply copied-and-pasted your code and added
'use strict' and 'use warnings' and what I got was:
>><<
>><<
------------------------------
Date: Thu, 10 Oct 2002 23:20:54 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: Returning hashrefs
Message-Id: <3DA60B3A.35981808@acm.org>
Da Witch wrote:
>
> Just when I think I'm beginning to understand Perl, it throws me a
> curve...
>
> sub foo { {1=>2} }
> sub bar { {3=>4} }
> sub baz { { %{&foo}, %{&bar} } }
> sub baz2 { my $t = { %{&foo}, %{&bar} } ; $t }
>
> my ($y, $z) = (&baz, &baz2);
> print ">>", ref $y, "<<\n";
> print ">>", ref $z, "<<\n";
>
> The output is:
> >><<
> >>HASH<<
>
> The only difference between baz and baz2 is that in baz2 the return
> value is first assigned to a lexical and it is the lexical that is
> returned.
>
> What's going on here? Why doesn't ref (&baz) == ref (&baz2) ??
$ perl -e'
sub foo { {1=>2} }
sub bar { {3=>4} }
sub baz { { %{&foo}, %{&bar} } }
sub baz2 { my $t = { %{&foo}, %{&bar} } ; $t }
my ($y, $z) = (&baz, &baz2);
print ">>", ref $y, "<<\n";
print ">>", ref $z, "<<\n";
'
>><<
>><<
What version of Perl are you using? OS?
John
--
use Perl;
program
fulfillment
------------------------------
Date: Thu, 10 Oct 2002 23:27:52 +0000 (UTC)
From: Da Witch <heather710101@yahoo.com>
Subject: Re: Returning hashrefs
Message-Id: <ao52do$o6k$1@reader1.panix.com>
In <ao4vdn$irh@dispatch.concentric.net> "James E Keenan" <jkeen@concentric.net> writes:
>"Da Witch" <heather710101@yahoo.com> wrote in message
>news:ao4urp$muh$1@reader1.panix.com...
>>
>> Just when I think I'm beginning to understand Perl, it throws me a
>> curve...
>>
>> sub foo { {1=>2} }
>> sub bar { {3=>4} }
>> sub baz { { %{&foo}, %{&bar} } }
>> sub baz2 { my $t = { %{&foo}, %{&bar} } ; $t }
>>
>> my ($y, $z) = (&baz, &baz2);
>> print ">>", ref $y, "<<\n";
>> print ">>", ref $z, "<<\n";
>>
>> The output is:
>> >><<
>> >>HASH<<
>>
>That's not the output I got. I simply copied-and-pasted your code and added
>'use strict' and 'use warnings' and what I got was:
>>><<
>>><<
Hmm. That's another curve from Perl! You'll get the results I
originally posted if you replace the assignment to ($x, $y) with
my $x = &baz;
my $y = &baz2;
That's what I originally had in my code, but I made what I thought
would be an innocuous edit when I posted it. I never expected this
change would affect the results so dramatically.
(Will wonders ever cease????)
Sorry about that.
h
------------------------------
Date: Thu, 10 Oct 2002 23:32:04 GMT
From: mcafee@artemis.transmeta.com (Sean McAfee)
Subject: Re: Returning hashrefs
Message-Id: <1034292730.88868@palladium.transmeta.com>
In article <1034292004.752511@palladium.transmeta.com>, I wrote:
>In article <ao4urp$muh$1@reader1.panix.com>,
>Da Witch <heather710101@yahoo.com> wrote:
>>sub foo { {1=>2} }
>>sub bar { {3=>4} }
>>sub baz { { %{&foo}, %{&bar} } }
>>sub baz2 { my $t = { %{&foo}, %{&bar} } ; $t }
>>
>>my ($y, $z) = (&baz, &baz2);
>>print ">>", ref $y, "<<\n";
>>print ">>", ref $z, "<<\n";
>>
>>The output is:
>>>><<
>>>>HASH<<
<my explanation snipped>
Oops. I didn't realize that the output I got from running your code
differs from yours. This is what I get:
>>HASH<<
>><<
...which is the opposite of what you reported, rendering my whole analysis
moot.
Unless you misreported your output, or made an error in pasting or
transcribing your code into your newsreader, then I'm stumped.
--
Sean McAfee mcafee@transmeta.com
print eval eval eval eval eval eval eval eval eval eval eval eval eval eval
q!q@q#q$q%q^q&q*q-q=q+q|q~q:q? Just Another Perl Hacker ?:~|+=-*&^%$#@!
------------------------------
Date: Fri, 11 Oct 2002 01:32:10 GMT
From: tiltonj@erols.com (Jay Tilton)
Subject: Re: Returning hashrefs
Message-Id: <3da628f0.268706402@news.erols.com>
Da Witch <heather710101@yahoo.com> wrote:
| In <ao4vdn$irh@dispatch.concentric.net> "James E Keenan" <jkeen@concentric.net> writes:
|
| >"Da Witch" <heather710101@yahoo.com> wrote in message
| >news:ao4urp$muh$1@reader1.panix.com...
| >>
| >> Just when I think I'm beginning to understand Perl, it throws me a
| >> curve...
| >>
| >> sub foo { {1=>2} }
| >> sub bar { {3=>4} }
| >> sub baz { { %{&foo}, %{&bar} } }
| >> sub baz2 { my $t = { %{&foo}, %{&bar} } ; $t }
| >>
| >> my ($y, $z) = (&baz, &baz2);
| >> print ">>", ref $y, "<<\n";
| >> print ">>", ref $z, "<<\n";
| >>
| >> The output is:
| >> >><<
| >> >>HASH<<
| >>
| >That's not the output I got. I simply copied-and-pasted your code and added
| >'use strict' and 'use warnings' and what I got was:
|
| >>><<
| >>><<
|
| Hmm. That's another curve from Perl! You'll get the results I
| originally posted if you replace the assignment to ($x, $y) with
|
| my $x = &baz;
| my $y = &baz2;
|
| That's what I originally had in my code, but I made what I thought
| would be an innocuous edit when I posted it. I never expected this
| change would affect the results so dramatically.
That was a big clue to what's different between baz() and baz2().
baz()'s return list must contain more than one scalar.
sub baz { { %{&foo}, %{&bar} } }
^^^^^^^^^^^^^^^^^^^^
That's a block, not an anonymous hash.
baz() returns a the two hashes, flattened into one list.
------------------------------
Date: Thu, 10 Oct 2002 23:02:16 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: Should -i eat my files?
Message-Id: <3DA606DD.EEA0B29C@acm.org>
Johannes Fürnkranz wrote:
>
> $ perl -v
>
> This is perl, v5.6.1 built for cygwin-multi
^^^^^^^^^^^^
^^^^^^^^^^^^
> $ ls -l
> total 4
> -rw-r--r-- 1 juffi Kein 1 Oct 10 18:01 a
> -rw-r--r-- 1 juffi Kein 1 Oct 10 18:01 b
> -rw-r--r-- 1 juffi Kein 1 Oct 10 18:01 c
> -rw-r--r-- 1 juffi Kein 1 Oct 10 18:01 d
>
> $ perl -i -pe 's/x/y/g' *
> Can't do inplace edit on a: Permission denied.
> Can't do inplace edit on b: Permission denied.
> Can't do inplace edit on c: Permission denied.
> Can't do inplace edit on d: Permission denied.
>
> $ ls -l
> total 0
>
> Is this normal?
>
> (it works fine if I do -i.bak, but real men don't need backups, right? :-) )
perldoc perldiag
[snip]
Can't do inplace edit on %s: %s
(S inplace) The creation of the new file failed for
the indicated reason.
Can't do inplace edit without backup
(F) You're on a system such as MS-DOS that gets con
fused if you try reading from a deleted (but still
opened) file. You have to say `-i.bak', or some such.
John
--
use Perl;
program
fulfillment
------------------------------
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.
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 3952
***************************************