[32373] in Perl-Users-Digest
Perl-Users Digest, Issue: 3640 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Mar 14 18:09:30 2012
Date: Wed, 14 Mar 2012 15:09:08 -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 Wed, 14 Mar 2012 Volume: 11 Number: 3640
Today's topics:
Re: Find the oldest files without using ls -tr <oneingray@gmail.com>
Re: Find the oldest files without using ls -tr <ben@morrow.me.uk>
Re: Find the oldest files without using ls -tr <mascheck@email.invalid>
Re: Find the oldest files without using ls -tr <ben@morrow.me.uk>
getting hash reference from a package <wpmccormick@just_about_everywhere.com>
Re: getting hash reference from a package <wpmccormick@just_about_everywhere.com>
Re: getting hash reference from a package (Tim McDaniel)
Re: getting hash reference from a package <wpmccormick@just_about_everywhere.com>
Re: getting hash reference from a package <goodcall1@hotmail.com>
Re: getting hash reference from a package <m@rtij.nl.invlalid>
Re: getting hash reference from a package <ben@morrow.me.uk>
Re: getting hash reference from a package (Tim McDaniel)
Re: getting hash reference from a package (Greg Bacon)
Re: getting hash reference from a package <ben@morrow.me.uk>
Re: getting hash reference from a package <rweikusat@mssgmbh.com>
Re: getting hash reference from a package <rweikusat@mssgmbh.com>
Re: getting hash reference from a package <rweikusat@mssgmbh.com>
Re: getting hash reference from a package <ben@morrow.me.uk>
Re: getting hash reference from a package <rweikusat@mssgmbh.com>
Re: New Science Discovery: Perl Idiots Remain Idiots Af (Seymour J.)
prevayler.org in perl <gavcomedy@gmail.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Wed, 14 Mar 2012 22:43:17 +0700
From: Ivan Shmakov <oneingray@gmail.com>
Subject: Re: Find the oldest files without using ls -tr
Message-Id: <86ty1r89yi.fsf@gray.siamics.net>
>>>>> Paul Branon <paulbranon@googlemail.com> writes:
[Cross-posting to news:comp.lang.perl.misc, for obvious
reasons.]
>> $ find -printf %T@\\t%p\\n \
> Ivan, what version of find are you using?
As usual, GNU's. (Unfortunately, GNU Findutils' documentation
makes no mention that -printf is non-portable.)
For a portable solution, a Perl-based one may fit, like:
$ find -exec perl -we 'foreach my $f (@ARGV) {
my @s = stat ($f)
or next;
printf("%d\t%s\n", $s[9], $f);
}' -- {} + \
But note that one should post-process the output of the above
Perl script even if the "oldest timestamp" logic is built into
it, as there may be more filenames that could be safely passed
to Perl by find(1) at one time.
--
FSF associate member #7257
------------------------------
Date: Wed, 14 Mar 2012 18:05:18 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Find the oldest files without using ls -tr
Message-Id: <u73739-sk21.ln1@anubis.morrow.me.uk>
[f'up set to clpmisc]
Quoth Ivan Shmakov <oneingray@gmail.com>:
>
> $ find -exec perl -we 'foreach my $f (@ARGV) {
> my @s = stat ($f)
> or next;
> printf("%d\t%s\n", $s[9], $f);
> }' -- {} + \
find -exec perl -le'print +(stat)[9], "\t", $_ for @ARGV' -- ...
Better would probably be to use -print0 and perl -n0. -print0 isn't in
POSIX, but it is in BSD find, which may be portable enough.
Depending on what you're doing with this, you may be able to get away
with -M rather than (stat)[9].
> But note that one should post-process the output of the above
> Perl script even if the "oldest timestamp" logic is built into
> it, as there may be more filenames that could be safely passed
> to Perl by find(1) at one time.
So forget the nasty shell utilities and use File::Find::Rule instead :).
Ben
------------------------------
Date: Wed, 14 Mar 2012 18:31:56 +0000 (UTC)
From: Sven Mascheck <mascheck@email.invalid>
Subject: Re: Find the oldest files without using ls -tr
Message-Id: <jjqo6sUn64L1@news.in-ulm.de>
In comp.unix.shell Ben Morrow <ben@morrow.me.uk> wrote:
> [f'up set to clpmisc]
[ignored, shell-only remark]
> Better would probably be to use -print0 and perl -n0. -print0 isn't in
> POSIX, but it is in BSD find, which may be portable enough.
or just "find ... -exec {} +" which is posix and obsoletes xargs
------------------------------
Date: Wed, 14 Mar 2012 19:59:34 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Find the oldest files without using ls -tr
Message-Id: <6u9739-2741.ln1@anubis.morrow.me.uk>
Quoth Sven Mascheck <mascheck@email.invalid>:
> In comp.unix.shell Ben Morrow <ben@morrow.me.uk> wrote:
> > [f'up set to clpmisc]
> [ignored, shell-only remark]
>
> > Better would probably be to use -print0 and perl -n0. -print0 isn't in
> > POSIX, but it is in BSD find, which may be portable enough.
>
> or just "find ... -exec {} +" which is posix and obsoletes xargs
The OP used that. It may obsolete xargs, but it is still subject to
argv-length limitations. Something like
find . -print0 | perl -n0 -le'...'
will only invoke one instance of perl ever, and perl will run in
parallel with find rather than having to wait until all its arguments
have been collected up.
Ben
------------------------------
Date: Tue, 13 Mar 2012 11:28:34 -0500
From: Bill M <wpmccormick@just_about_everywhere.com>
Subject: getting hash reference from a package
Message-Id: <jjnsjo$57f$1@dont-email.me>
What is the correct syntax for accessing a hash reference from a package?
For example:
Package MyPackage;
my $foo = {
animals => ["cat", "dog", "fish"],
people => ["fred", wilma" ]
};
1;
and then (this is what I THINK should work) ...
use MyPackage;
foreach my $animal (@{$MyPackage::$foo->{animals}}) {
print $animal;
}
Thanks!!!
------------------------------
Date: Tue, 13 Mar 2012 11:44:06 -0500
From: Bill M <wpmccormick@just_about_everywhere.com>
Subject: Re: getting hash reference from a package
Message-Id: <jjntgq$an4$1@dont-email.me>
Bill M wrote, On 3/13/2012 11:28 AM:
> What is the correct syntax for accessing a hash reference from a package?
>
> For example:
>
> Package MyPackage;
>
> my $foo = {
> animals => ["cat", "dog", "fish"],
> people => ["fred", wilma" ]
> };
>
> 1;
>
> and then (this is what I THINK should work) ...
>
> use MyPackage;
>
> foreach my $animal (@{$MyPackage::$foo->{animals}}) {
> print $animal;
> }
>
>
> Thanks!!!
Sorry, I meant this:
use MyPackage;
foreach my $animal (@{$MyPackage::foo->{animals}}) {
print $animal;
}
------------------------------
Date: Tue, 13 Mar 2012 17:56:39 +0000 (UTC)
From: tmcd@panix.com (Tim McDaniel)
Subject: Re: getting hash reference from a package
Message-Id: <jjo1on$2l8$1@reader1.panix.com>
In article <jjntgq$an4$1@dont-email.me>,
Bill M <wpmccormick@just_about_everywhere.com> wrote:
>Bill M wrote, On 3/13/2012 11:28 AM:
>> What is the correct syntax for accessing a hash reference from a package?
>>
>> For example:
>>
>> package MyPackage;
>> my $foo = {
>> animals => ["cat", "dog", "fish"],
>> people => ["fred", "wilma" ]
>> };
>>
>> 1;
I added the missing double-quote before wilma and lowercased
"package".
"1;" usually comes at the end of a module file. So I assume that
that's in its own file, named MyPackage.pm.
>use MyPackage;
Yeah, that requires a separate source file named MyPackage.pm or such.
>
>foreach my $animal (@{$MyPackage::foo->{animals}}) {
> print $animal;
>}
Realize that "my" is lexical only.
(1) "my" does not create a package variable. There is no
$MyPackage::foo variable in MyPackage. When you refer to it in the
other file, it creates on that spot a variable set to undef.
(2) It's lexical only, so it exists only inside the file's scope.
It's not visible outside, so trying to refer to just "$foo" would get
you an error "Global symbol "$foo" requires explicit package name".
So replace "my" by "our" in MyPackage and it all works as you expect.
--
Tim McDaniel, tmcd@panix.com
------------------------------
Date: Tue, 13 Mar 2012 13:07:25 -0500
From: Bill M <wpmccormick@just_about_everywhere.com>
Subject: Re: getting hash reference from a package
Message-Id: <jjo2d1$8cj$1@dont-email.me>
Tim McDaniel wrote, On 3/13/2012 12:56 PM:
> In article<jjntgq$an4$1@dont-email.me>,
> Bill M<wpmccormick@just_about_everywhere.com> wrote:
>> Bill M wrote, On 3/13/2012 11:28 AM:
>>> What is the correct syntax for accessing a hash reference from a package?
>>>
>>> For example:
>>>
>>> package MyPackage;
>>> my $foo = {
>>> animals => ["cat", "dog", "fish"],
>>> people => ["fred", "wilma" ]
>>> };
>>>
>>> 1;
>
> I added the missing double-quote before wilma and lowercased
> "package".
>
> "1;" usually comes at the end of a module file. So I assume that
> that's in its own file, named MyPackage.pm.
>
>> use MyPackage;
> Yeah, that requires a separate source file named MyPackage.pm or such.
>>
>> foreach my $animal (@{$MyPackage::foo->{animals}}) {
>> print $animal;
>> }
>
> Realize that "my" is lexical only.
>
> (1) "my" does not create a package variable. There is no
> $MyPackage::foo variable in MyPackage. When you refer to it in the
> other file, it creates on that spot a variable set to undef.
>
> (2) It's lexical only, so it exists only inside the file's scope.
> It's not visible outside, so trying to refer to just "$foo" would get
> you an error "Global symbol "$foo" requires explicit package name".
>
> So replace "my" by "our" in MyPackage and it all works as you expect.
>
Thanks, just figured it out then saw your answer as I was about to reply
to myself.
------------------------------
Date: Wed, 14 Mar 2012 01:57:07 -0600
From: Jack <goodcall1@hotmail.com>
Subject: Re: getting hash reference from a package
Message-Id: <NaY7r.589637$lb1.44216@news.usenetserver.com>
On 13/03/2012 12:07 PM, Bill M wrote:
> Tim McDaniel wrote, On 3/13/2012 12:56 PM:
>> In article<jjntgq$an4$1@dont-email.me>,
>> Bill M<wpmccormick@just_about_everywhere.com> wrote:
>>> Bill M wrote, On 3/13/2012 11:28 AM:
>>>> What is the correct syntax for accessing a hash reference from a
>>>> package?
>>>>
>>>> For example:
>>>>
>>>> package MyPackage;
>>>> my $foo = {
>>>> animals => ["cat", "dog", "fish"],
>>>> people => ["fred", "wilma" ]
>>>> };
>>>>
>>>> 1;
>>
>> I added the missing double-quote before wilma and lowercased
>> "package".
>>
>> "1;" usually comes at the end of a module file. So I assume that
>> that's in its own file, named MyPackage.pm.
>>
>>> use MyPackage;
>> Yeah, that requires a separate source file named MyPackage.pm or such.
>>>
>>> foreach my $animal (@{$MyPackage::foo->{animals}}) {
>>> print $animal;
>>> }
>>
>> Realize that "my" is lexical only.
>>
>> (1) "my" does not create a package variable. There is no
>> $MyPackage::foo variable in MyPackage. When you refer to it in the
>> other file, it creates on that spot a variable set to undef.
>>
>> (2) It's lexical only, so it exists only inside the file's scope.
>> It's not visible outside, so trying to refer to just "$foo" would get
>> you an error "Global symbol "$foo" requires explicit package name".
>>
>> So replace "my" by "our" in MyPackage and it all works as you expect.
>>
> Thanks, just figured it out then saw your answer as I was about to reply
> to myself.
Question: Is it proper to access a package variable directly? Would it
not be better to provide a method to retrieve the reference?
Jack
------------------------------
Date: Wed, 14 Mar 2012 12:06:58 +0100
From: Martijn Lievaart <m@rtij.nl.invlalid>
Subject: Re: getting hash reference from a package
Message-Id: <ina639-3qd.ln1@news.rtij.nl>
On Wed, 14 Mar 2012 01:57:07 -0600, Jack wrote:
> Question: Is it proper to access a package variable directly? Would it
> not be better to provide a method to retrieve the reference?
Even better, provide a getter and a setter (or combine them).
Something like:
my $global_debug_flag;
sub debugging {
$global_debug_flag = $_[0] if defined $_[0];
return $global_debug_flag;
}
M4
------------------------------
Date: Wed, 14 Mar 2012 14:27:11 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: getting hash reference from a package
Message-Id: <vem639-ha01.ln1@anubis.morrow.me.uk>
Quoth Jack <goodcall1@hotmail.com>:
>
> Question: Is it proper to access a package variable directly? Would it
> not be better to provide a method to retrieve the reference?
If the value is truly global then there's very little point wrapping the
variable in a sub. A sub like
sub my_global { \$Foo }
or even
sub set_my_global { $Foo = $_[0] }
isn't hiding anything: no matter what you do later, there can still only
be one value of $Foo across all users of your module, so it might as
well be a package global.
Ben
------------------------------
Date: Wed, 14 Mar 2012 15:37:32 +0000 (UTC)
From: tmcd@panix.com (Tim McDaniel)
Subject: Re: getting hash reference from a package
Message-Id: <jjqdvs$4op$1@reader1.panix.com>
In article <ina639-3qd.ln1@news.rtij.nl>,
Martijn Lievaart <m@rtij.nl.invlalid> wrote:
>On Wed, 14 Mar 2012 01:57:07 -0600, Jack wrote:
>
>> Question: Is it proper to access a package variable directly? Would
>> it not be better to provide a method to retrieve the reference?
>
>Even better, provide a getter and a setter (or combine them).
That's certainly the fashion in object-oriented code. I may be
showing my ignorance, but I'm afraid I don't see the benefit in doing
that for a single uncomplicated global variable. An object with
complicated semantics, or semantics that must be constrained and
checked for validity, of course -- that's one of the points of
object-oriented code, and it's a very useful technique. But a simple
count, say, or a string that's unrelated to anything else? Why not
require getters and setters for that are visible for more than 50
lines, say, or for all variables?
--
Tim McDaniel, tmcd@panix.com
------------------------------
Date: Wed, 14 Mar 2012 12:20:37 -0500
From: gbacon@hiwaay.net (Greg Bacon)
Subject: Re: getting hash reference from a package
Message-Id: <QuudnQV4rJn4Tv3SnZ2dnUVZ_vSdnZ2d@posted.hiwaay2>
Jack wrote:
: Question: Is it proper to access a package variable directly? Would it
: not be better to provide a method to retrieve the reference?
As a rule of thumb, pass parameters to subs and methods rather than
communicating through global variables. A singleton is a global variable
dressed in fancy clothes, so avoid those too.
Be more specific about what you want your code to do. Then we can give
you specific, helpful advice for your situation rather than vague
generalities.
Greg
--
I know different people will have different lists. That's good.
I'll make a list of lists. It'd be nice if I had a good language
for manipulating complex data structures. Oh, wait...
-- Chip Salzenberg in perl6-internals
------------------------------
Date: Wed, 14 Mar 2012 17:54:09 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: getting hash reference from a package
Message-Id: <1j2739-sk21.ln1@anubis.morrow.me.uk>
Quoth tmcd@panix.com:
>
> That's certainly the fashion in object-oriented code.
Good practice in OO code would be to make something like that a method
on the object. That way if you choose to make the debugging level (or
whatever it is) per-object later on, you can.
Wrapping a simple variable like that in a plain function, or in a class
method, I agree seems pointless.
> Why not require getters and setters for [?variables] that are visible
> for more than 50 lines, say, or for all variables?
If you've got a (not file-scoped) lexical that's visible for more than
50 lines you need to refactor.
Ben
------------------------------
Date: Wed, 14 Mar 2012 18:14:52 +0000
From: Rainer Weikusat <rweikusat@mssgmbh.com>
Subject: Re: getting hash reference from a package
Message-Id: <871uovcan7.fsf@sapphire.mobileactivedefense.com>
gbacon@hiwaay.net (Greg Bacon) writes:
> Jack wrote:
> : Question: Is it proper to access a package variable directly? Would it
> : not be better to provide a method to retrieve the reference?
>
> As a rule of thumb, pass parameters to subs and methods rather than
> communicating through global variables. A singleton is a global variable
> dressed in fancy clothes, so avoid those too.
A singleton is the Java programmer's workaround for the fact that
Java requires everything to be an instance of some class, that is,
a general blueprint for construction objects of certain kinds, but
that many real-world problems are easier dealt with by systems
constructed of stateful modules ('subsystems') providing some kind of
identifiable, useful functionality for solving a part of the original
problem (that's similar to complex real machines which are also
composed of cooperating 'sub-machines' dealing with 'subtasks' of the
problem supposed to be solved).
Apart from that, an object instance is a set of variables shared by
some set of cooperating subroutines and this is nothing but 'a global
variable dressed in fancy clothes'. Some people are convinced that
complex objects whose state changes over time are Evil[tm] and should
be avoided. Except when the result can be eaten, I presume ...
------------------------------
Date: Wed, 14 Mar 2012 18:28:37 +0000
From: Rainer Weikusat <rweikusat@mssgmbh.com>
Subject: Re: getting hash reference from a package
Message-Id: <87wr6navfu.fsf@sapphire.mobileactivedefense.com>
Ben Morrow <ben@morrow.me.uk> writes:
> Quoth Jack <goodcall1@hotmail.com>:
>>
>> Question: Is it proper to access a package variable directly? Would it
>> not be better to provide a method to retrieve the reference?
>
> If the value is truly global then there's very little point wrapping the
> variable in a sub. A sub like
>
> sub my_global { \$Foo }
>
> or even
>
> sub set_my_global { $Foo = $_[0] }
>
> isn't hiding anything:
There might actually be an advantage in doing so: It helps with
debugging if all accesses to some object also go through a certain
piece of code, because this provides a location where debugging
statements or debugger breakpoints can be added.
------------------------------
Date: Wed, 14 Mar 2012 20:52:34 +0000
From: Rainer Weikusat <rweikusat@mssgmbh.com>
Subject: Re: getting hash reference from a package
Message-Id: <87fwdac3cd.fsf@sapphire.mobileactivedefense.com>
Jack <goodcall1@hotmail.com> writes:
[...]
> Question: Is it proper to access a package variable directly? Would it
> not be better to provide a method to retrieve the reference?
What do you mean by 'proper'? Eg, the typical example for something
where anything but enabling direct access to 'a package variable'
makes little sense would be some kind of 'debugging level' variable
which can be used to control the amount of diagnostics a program will
print: That's typically modified in one or two location (the code
which deal with command-line arguments and something like a signal
handler which can be used to change the value for a running program)
and read-only referenced from everywhere else in the code. This really
depends on the purpose of the variable.
------------------------------
Date: Wed, 14 Mar 2012 21:08:56 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: getting hash reference from a package
Message-Id: <80e739-3451.ln1@anubis.morrow.me.uk>
Quoth Rainer Weikusat <rweikusat@mssgmbh.com>:
> Ben Morrow <ben@morrow.me.uk> writes:
> > Quoth Jack <goodcall1@hotmail.com>:
> >>
> >> Question: Is it proper to access a package variable directly? Would it
> >> not be better to provide a method to retrieve the reference?
> >
> > If the value is truly global then there's very little point wrapping the
> > variable in a sub. A sub like
> >
> > sub my_global { \$Foo }
> >
> > or even
> >
> > sub set_my_global { $Foo = $_[0] }
> >
> > isn't hiding anything:
>
> There might actually be an advantage in doing so: It helps with
> debugging if all accesses to some object also go through a certain
> piece of code, because this provides a location where debugging
> statements or debugger breakpoints can be added.
perldebug:
| w expr Add a global watch-expression. Whenever a watched global
| changes the debugger will stop and display the old and new
| values.
Ben
------------------------------
Date: Wed, 14 Mar 2012 21:23:17 +0000
From: Rainer Weikusat <rweikusat@mssgmbh.com>
Subject: Re: getting hash reference from a package
Message-Id: <877gymc1x6.fsf@sapphire.mobileactivedefense.com>
Ben Morrow <ben@morrow.me.uk> writes:
> Quoth Rainer Weikusat <rweikusat@mssgmbh.com>:
>> Ben Morrow <ben@morrow.me.uk> writes:
>> > Quoth Jack <goodcall1@hotmail.com>:
>> >>
>> >> Question: Is it proper to access a package variable directly? Would it
>> >> not be better to provide a method to retrieve the reference?
>> >
>> > If the value is truly global then there's very little point wrapping the
>> > variable in a sub. A sub like
>> >
>> > sub my_global { \$Foo }
>> >
>> > or even
>> >
>> > sub set_my_global { $Foo = $_[0] }
>> >
>> > isn't hiding anything:
>>
>> There might actually be an advantage in doing so: It helps with
>> debugging if all accesses to some object also go through a certain
>> piece of code, because this provides a location where debugging
>> statements or debugger breakpoints can be added.
>
> perldebug:
> | w expr Add a global watch-expression. Whenever a watched global
> | changes the debugger will stop and display the old and new
> | values.
This is notoriously unreliable for 'non-managed code' (not applicable
to perl) and only the lesser half of the possible advantage. Also, I
wrote 'their might' for a reason: This depends on what debugging tools
can be used in a given situation and what their capabilities are.
------------------------------
Date: Mon, 12 Mar 2012 16:20:22 -0400
From: Shmuel (Seymour J.) Metz <spamtrap@library.lspace.org.invalid>
Subject: Re: New Science Discovery: Perl Idiots Remain Idiots After A Decade!New Science Discovery: Perl Idiots Remain Idiots Af
Message-Id: <4f5e5a86$10$fuzhry+tra$mr2ice@news.patriot.net>
In <m0scsu.8xu@spenarnc.xs4all.nl>, on 03/12/2012
at 07:00 PM, Albert van der Horst <albert@spenarnc.xs4all.nl> said:
>I know, but what the mathematicians do make so much more sense:
Not really; Mathematical notation is a matter of convention, and the
conventions owe as much to History as they do to logical necessity.
The conventions aren't even the same from author to author, e.g.,
whether "field" implies Abelian.
--
Shmuel (Seymour J.) Metz, SysProg and JOAT <http://patriot.net/~shmuel>
Unsolicited bulk E-mail subject to legal action. I reserve the
right to publicly post or ridicule any abusive E-mail. Reply to
domain Patriot dot net user shmuel+news to contact me. Do not
reply to spamtrap@library.lspace.org
------------------------------
Date: Tue, 13 Mar 2012 14:54:46 -0700 (PDT)
From: quiet_lad <gavcomedy@gmail.com>
Subject: prevayler.org in perl
Message-Id: <9872968.2851.1331675686996.JavaMail.geo-discussion-forums@vbtf26>
has anyone porrted prevayler to perl?
------------------------------
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:
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests.
#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 3640
***************************************