[28959] in Perl-Users-Digest
Perl-Users Digest, Issue: 203 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Mar 7 18:14:35 2007
Date: Wed, 7 Mar 2007 15:14:27 -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 Wed, 7 Mar 2007 Volume: 11 Number: 203
Today's topics:
Perl tie hash question <ecarlson@vmware.com>
Re: Perl tie hash question <DJStunks@gmail.com>
Re: Perl tie hash question <uri@stemsystems.com>
Re: Regexp for email addresses. <tzz@lifelogs.com>
Re: Splitting a filename <mritty@gmail.com>
Re: Splitting a filename <wahab-mail@gmx.de>
Re: Subroutines and Callbacks in Perl/Tk <ben@morrow.me.uk>
Re: Subroutines and Callbacks in Perl/Tk <ben@morrow.me.uk>
Re: Subroutines and Callbacks in Perl/Tk <ch.l.ngre@online.de>
Re: Tk::DropSite question <g_m@remove-comcast.net>
What is abriviation for CHR(4) <max@xxx.tovle.ct>
Re: What is abriviation for CHR(4) <someone@example.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: 7 Mar 2007 13:21:01 -0800
From: "Eric" <ecarlson@vmware.com>
Subject: Perl tie hash question
Message-Id: <1173302461.767680.57460@p10g2000cwp.googlegroups.com>
We inherited the block of code below, which is used to register a
specific mount point with a threadsafe hash. The input for this code
is sent from a calling function. Note that this sub is supposed to
output to file $mountsDBFile. The file is created, but is not
readable; it is nothing but cryptic characters. None of us are that
experienced (yet) with hash ties. So I have the following questions:
Based on the code below, is is apparent why the output file is not
readable?
Is the code below even supposed to output into a humanly readable
file?
Since the output file needs to be humanly readable, should we abandon
what we've inherited and start from scratch, and if so, should we
abandon the tie hash approach in favor of something else?
I realize I have a lot to learn about ties, and am in the progress of
doing so.
Thanks in advance to all that respond.
Eric
=======================================
sub RequestMountPoint {
my $self = shift;
my $protocol = shift;
my $bldNum = shift;
my $mntPnt = undef;
my $mountsDBFile = $self->Env->HomeDir()."/mountsDB";
my $mntsDB = {};
unless (open SEMAPHORE, "> /tmp/mounts.lock") {
$self->Env->ReleaseMachines();
die "unexpected problem allocating semaphore";
}
flock SEMAPHORE, Fcntl::LOCK_EX;
tie( %$mntsDB, "MLDBM", $mountsDBFile, O_CREAT|O_RDWR, 0666,
$DB_File::DB_BTREE );
my $mountPoint = $self->Config->MountPointCount();
for (my $label = 0; $label < $mountPoint; $label++){
$mntPnt = "xmnt".$label;
unless (defined($mntsDB->{$mntPnt})){
$mntsDB->{$mntPnt} = {
BldNum => $bldNum,
Protocol => $protocol,
RefCnt => 1,
};
last;
}
if (($mntsDB->{$mntPnt}->{BldNum} == $bldNum) and
($mntsDB->{$mntPnt}->{Protocol} eq $protocol)) {
$mntsDB->{$mntPnt}->{RefCnt}++;
last;
}
$mntPnt = undef;
}
my $ref = $mntsDB->{$mntPnt}->{RefCnt};
untie(%$mntsDB);
close(SEMAPHORE);
return $mntPnt, $ref;
}
------------------------------
Date: 7 Mar 2007 13:45:30 -0800
From: "DJ Stunks" <DJStunks@gmail.com>
Subject: Re: Perl tie hash question
Message-Id: <1173303930.028815.314840@s48g2000cws.googlegroups.com>
On Mar 7, 1:21 pm, "Eric" <ecarl...@vmware.com> wrote:
> We inherited the block of code below, which is used to register a
> specific mount point with a threadsafe hash. The input for this code
> is sent from a calling function. Note that this sub is supposed to
> output to file $mountsDBFile. The file is created, but is not
> readable; it is nothing but cryptic characters. None of us are that
> experienced (yet) with hash ties. So I have the following questions:
>
> Based on the code below, is is apparent why the output file is not
> readable?
>
> Is the code below even supposed to output into a humanly readable
> file?
>
> Since the output file needs to be humanly readable, should we abandon
> what we've inherited and start from scratch, and if so, should we
> abandon the tie hash approach in favor of something else?
>
> I realize I have a lot to learn about ties, and am in the progress of
> doing so.
>
> Thanks in advance to all that respond.
>
> Eric
>
> =======================================
>
> sub RequestMountPoint {
> my $self = shift;
> my $protocol = shift;
> my $bldNum = shift;
> my $mntPnt = undef;
> my $mountsDBFile = $self->Env->HomeDir()."/mountsDB";
>
> my $mntsDB = {};
> unless (open SEMAPHORE, "> /tmp/mounts.lock") {
> $self->Env->ReleaseMachines();
> die "unexpected problem allocating semaphore";
> }
> flock SEMAPHORE, Fcntl::LOCK_EX;
>
> tie( %$mntsDB, "MLDBM", $mountsDBFile, O_CREAT|O_RDWR, 0666,
> $DB_File::DB_BTREE );
<snip>
it's not the tying that's causing you problems - it's the fact that
you're using a DBM file type to store your hash. This creates a
binary file to store the key/value pairs.
if you just want to be able to read your DBM file from the shell there
are tools for this or you could write a quick perl script to do so
(just tie it the same way, and then dump the hash). Or, if you want a
human readable output, I'd suggest using the Config::Simple module,
which will let you tie your hash (for read/write) but the output would
be an .ini style config file.
-jp
------------------------------
Date: Wed, 07 Mar 2007 17:00:11 -0500
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: Perl tie hash question
Message-Id: <x77itsbtp0.fsf@mail.sysarch.com>
>>>>> "E" == Eric <ecarlson@vmware.com> writes:
E> Based on the code below, is is apparent why the output file is not
E> readable?
E> Since the output file needs to be humanly readable, should we abandon
E> what we've inherited and start from scratch, and if so, should we
E> abandon the tie hash approach in favor of something else?
in general most db type files are not human readable for speed
reasons. where did you get the idea that MLDBM or others would be human
readable? you can use SQLITE on a flat db text file and get the same
behavior but slower and it will be human readable (records are lines
with fields separated by some char).
this has nothing to do with tie but with the db backend you choose (and
it isn't called mounting which is about file systems so i would change
that name).
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: Wed, 07 Mar 2007 18:08:24 -0500
From: Ted Zlatanov <tzz@lifelogs.com>
Subject: Re: Regexp for email addresses.
Message-Id: <g69vehctzx3.fsf@dhcp-65-162.kendall.corp.akamai.com>
On 14 Feb 2007 21:55:40 GMT Abigail <abigail@abigail.be> wrote:
A> use 5.9.5; # In fact, you need the newest blead.
A> my $email_address = qr {
A> (?(DEFINE)
A> (?<addr_spec> (?&local_part) \@ (?&domain))
...
A> (?&addr_spec)
A> }x;
I have a question (to you or anyone else informed), as I don't follow
the latest Perl 5 changes and features: is this grammar syntax
experimental, or will it definitely remain in Perl 5 at this point? I
don't mind the latest blead, but I don't want to commit my own code to
it if the feature is experimental.
Thanks for the code, it was fascinating.
Ted
------------------------------
Date: 7 Mar 2007 12:21:13 -0800
From: "Paul Lalli" <mritty@gmail.com>
Subject: Re: Splitting a filename
Message-Id: <1173298872.943445.282960@h3g2000cwc.googlegroups.com>
On Mar 7, 1:10 pm, "Noel Sant" <n...@sant.me.uk> wrote:
> I want to split a filename into the name itself and the extension. This:
>
> ($name, $extension) = split /\./, $input_file;
>
> works fine, providing there's only one dot in the filename, but if there are
> more I just get the first two bits of name. I really want to get the last
> bit into $extension and all the rest, including dots, into $name.
>
> I suppose I could use an array on the left-hand side, find out how many
> element there are and just build up $name from all the arrays bar the last,
> but this seems long-winded. I tried using "split /\.$/, ..." but then I got
> evrything in $name, and $extension was undefined. As though it's just
> looking at the end of the string and saying "Nope! no dot there" and not
> going any further back. Obviously I don't understand what $ does.
>
> How do I say "just match on the last dot", please?
The answer to your actual question is to only split on a dot that's
not followed by anything which includes dots:
my ($name, $ext) = split /\.(?!.*\.)/, $file;
(read about lookaheads in `perldoc perlre`)
However, the correct answer to the problem you're actually trying to
solve is to stop reinventing the wheel:
perldoc File::Basename
Paul Lalli
------------------------------
Date: Wed, 07 Mar 2007 21:40:55 +0100
From: Mirco Wahab <wahab-mail@gmx.de>
Subject: Re: Splitting a filename
Message-Id: <esndk2$omi$1@mlucom4.urz.uni-halle.de>
Noel Sant wrote:
> I want to split a filename into the name itself and the extension. This:
>
> ($name, $extension) = split /\./, $input_file;
>
> works fine, providing there's only one dot in the filename, but if there are
> more I just get the first two bits of name. I really want to get the last
> bit into $extension and all the rest, including dots, into $name.
>
> I suppose I could use an array on the left-hand side, find out how many
> element there are and just build up $name from all the arrays bar the last,
> but this seems long-winded. I tried using "split /\.$/, ..." but then I got
> evrything in $name, and $extension was undefined. As though it's just
> looking at the end of the string and saying "Nope! no dot there" and not
> going any further back. Obviously I don't understand what $ does.
>
> How do I say "just match on the last dot", please?
There's a module for it, as Paul and DJS said,
so try to use it if possible.
But, for "learning purpose", you could have
splitted simple filenames by simple regular
expressions.
In case we have the 4 "splendid variants", like:
my @names = qw'
fi.le.ext
file.file.
file
.ext
';
(note the dots). Then we can "split them apart"
by, eg.
my $rg = qr/ (.*) \. (.*) $ | (.*) /x;
In this case, the 'filename component' is in $1 or in $3
($3 => if no dot at all was there), so lets extract that:
print
map +($_->[0] || $_->[2] || '(undef)') ."\t". ($_->[1] || '(undef)') ."\n",
map [ /$rg/g ],
@names;
The second (short) map expression applies the
regular expression and converts ($1,$2,$3) to
a list (reference).
The "complicated looking" first map expression
only serves the purpose to give some fancy
output, in our case:
fi.le ext
file.file (undef)
file (undef)
(undef) ext
we want to see which parts are 'defined'
and which are not.
Regards
M.
------------------------------
Date: Wed, 7 Mar 2007 20:24:07 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Subroutines and Callbacks in Perl/Tk
Message-Id: <7kj3c4-9ga.ln1@osiris.mauzo.dyndns.org>
Quoth Michele Dondi <bik.mido@tiscalinet.it>:
> On Tue, 6 Mar 2007 03:48:20 +0000, Ben Morrow <ben@morrow.me.uk>
> wrote:
>
> >I call the module BMORROW::Tie::TkWatch (I have a convention that
> >modules for my own use are prefixed with my CPAN id); you probably want
>
> Why don't you release it.
I've been half thinking about releasing it, but that means writing
tests/writing documentation/testing it cross-platform/etc. and I don't
really have time right now :).
> It's cool, and certainly useful. Are you a
> PerlMonks user too? If so, then you may also post it in the snippets
> or code sections. If not, may I do so?
No, I'm not; and yes, of course you may :). Could you stick a
Copyright 2007 Ben Morrow
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.8.5
or, at your option, any later version of Perl 5 you may have
available.
section at the bottom? (I should really have done so before posting...)
Ben
--
don't get my sympathy hanging out the 15th floor. you've changed the locks 3
times, he still comes reeling though the door, and soon he'll get to you, teach
you how to get to purest hell. you do it to yourself and that's what really
hurts is you do it to yourself just you, you and noone else ** ben@morrow.me.uk
------------------------------
Date: Wed, 7 Mar 2007 20:25:54 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Subroutines and Callbacks in Perl/Tk
Message-Id: <inj3c4-9ga.ln1@osiris.mauzo.dyndns.org>
Quoth Michele Dondi <bik.mido@tiscalinet.it>:
> On Wed, 7 Mar 2007 01:06:01 +0100, "Petr Vileta"
> <stoupa@practisoft.cz> wrote:
>
> >use Tk;
> >use Tk::Label;
> >use Tk::Button;
>
> AFAIK (not much, really) the latter two are not necessary. (But I
> checked and it seems like it's actually so.)
They aren't strictly necessary: when you create a Button Tk will
autoload Tk::Button for you. But unfortunately it gives a warning, which
would be *very* confusing for users (or, at least, for *my* users :) ).
Ben
--
If you put all the prophets, | You'd have so much more reason
Mystics and saints | Than ever was born
In one room together, | Out of all of the conflicts of time.
ben@morrow.me.uk The Levellers, 'Believers'
------------------------------
Date: Wed, 07 Mar 2007 22:12:05 +0100
From: Ch Lamprecht <ch.l.ngre@online.de>
Subject: Re: Subroutines and Callbacks in Perl/Tk
Message-Id: <esn9r6$23o$2@online.de>
Ben Morrow wrote:
> Quoth Michele Dondi <bik.mido@tiscalinet.it>:
>
>>On Wed, 7 Mar 2007 01:06:01 +0100, "Petr Vileta"
>><stoupa@practisoft.cz> wrote:
>>
>>
>>>use Tk;
>>>use Tk::Label;
>>>use Tk::Button;
>>
>>AFAIK (not much, really) the latter two are not necessary. (But I
>>checked and it seems like it's actually so.)
>
>
> They aren't strictly necessary: when you create a Button Tk will
> autoload Tk::Button for you. But unfortunately it gives a warning, which
> would be *very* confusing for users (or, at least, for *my* users :) ).
With Tk-804 this should not be the case (not with Label and Button):
use strict;
use warnings;
use Tk;
my $mw = MainWindow->new( );
$mw->$_()->pack for qw/Label Button Entry Text/;
print "but:\n";
$mw->ROText()->pack;
MainLoop;
Christoph
--
use Tk;use Tk::GraphItems;$c=tkinit->Canvas->pack;push@i,Tk::GraphItems->
TextBox(text=>$_,canvas=>$c,x=>$x+=70,y=>100)for(Just=>another=>Perl=>Hacker);
Tk::GraphItems->Connector(source=>$i[$_],target=>$i[$_+1])for(0..2);
$c->repeat(30,sub{$_->move(0,4*cos($d+=3.16))for(@i)});MainLoop
------------------------------
Date: Wed, 7 Mar 2007 15:31:55 -0500
From: "~greg" <g_m@remove-comcast.net>
Subject: Re: Tk::DropSite question
Message-Id: <C-edndOLlZ-4vHLYnZ2dnUVZ_qSrnZ2d@comcast.com>
"zentara" > ...
> ~greg >
>>is there any way to know when to call a group-drop-handler,
>>to be called after all the individual calls to OnSourceDrop()
>>are finished after a group drop?
>
> Hi, I waited to see if anyone answered first; but since no one has,
> I suggest you post this to comp.lang.perl.tk or perlmonks.org.
Thank you for your patience about that, zentara.
And for your suggested alternatives.
When I asked the question in this newsgroup, I asked it also
in exactly one other group - comp.lang.perl.modules.
And I got a answer there pretty quick, from a Mr Christoph (Ch) Lamprecht.
He suggested at first a general idea, which might be useful in other situations:
-- namely, setting up a delayed call to the general-handler
from within the particular-handler, delayed by say 50 milliseconds,
but canceling it first, in the particular-handler, so that the general handler
won't be called as long as the particular-handler is being repeatedly
called within 50 msec of itself. And that worked.
But after I told Mr Lamprecht that I had been hoping for something
that didn't depend on timing, he very kindly went through the source
code of Tk::DropSite for me and discovered that the module itself
does in fact provide for exactly what I was asking for.
And he showed me two ways to do it.
His solutions can still be read very near the top of that newsgroup,
(which is very slow, but very sure moving.)
The real problem is that Tk::DropSite has in effect no pod
documentation at all. What it has instead is simply a mention
of the names of 3 attributes (one of them oddly repeated)
and 4 methods. But it says nothing at all about what they're for
or how to use them. And of course no examples.
It'd be a contender for the contest for the worst documented module,
except that I guess that Tk is understood to be documented in TCL references?
As for reading the source, I know that it can always be a useful
adjunct to understanding the official interface to a module.
But I am not very good at it, and in particular
I am not very good at seeing the difference, in source code,
between what's for the interface, and what's just for the
internal implimentaion, unless it's explicitly commented.
And I am afraid of accidentally making anything dependant
on internal implimentation, which could change with
every minor version upgrade of modules.
> Tk Drop is not seen much, and in conjunction with Windows makes
> it pretty esoteric.
> zentara
> http://zentara.net/japh.html
well, ok, but ...
when i clicked on your link, I got an ActiveX warning...
so I know at least you don't mean your comment
as a blanket indictment of Windows. :)
~greg
------------------------------
Date: Wed, 7 Mar 2007 21:48:01 +0100
From: "max" <max@xxx.tovle.ct>
Subject: What is abriviation for CHR(4)
Message-Id: <esn8dm$nfj$1@ss408.t-com.hr>
Problem with making Replacement CHR(4) in something.
CHR(9) is "\t"
I use tr///.
What is abbreviation for CHR(4)
Thanks
Max
------------------------------
Date: Wed, 07 Mar 2007 21:04:41 GMT
From: "John W. Krahn" <someone@example.com>
Subject: Re: What is abriviation for CHR(4)
Message-Id: <JNFHh.35312$cE3.33296@edtnps89>
max wrote:
> Problem with making Replacement CHR(4) in something.
> CHR(9) is "\t"
> I use tr///.
> What is abbreviation for CHR(4)
"\t" could also be represented as "\011" or "\x09" or "\cI" so chr( 4 ) could
be represented as "\04" or "\x04" or "\cD".
John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
------------------------------
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 V11 Issue 203
**************************************