[32680] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 3957 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu May 30 00:09:25 2013

Date: Wed, 29 May 2013 21:09: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           Wed, 29 May 2013     Volume: 11 Number: 3957

Today's topics:
        An old trick that save me TONES of code <nospam.gravitalsun.noadsplease@hotmail.noads.com>
    Re: An old trick that save me TONES of code (Tim McDaniel)
    Re: An old trick that save me TONES of code <glex_no-spam@qwest-spam-no.invalid>
    Re: An old trick that save me TONES of code <rweikusat@mssgmbh.com>
    Re: An old trick that save me TONES of code <ben@morrow.me.uk>
    Re: An old trick that save me TONES of code <derykus@gmail.com>
    Re: An old trick that save me TONES of code <rweikusat@mssgmbh.com>
    Re: iCalendar module? (hymie!)
    Re: it hurts when I press here <rweikusat@mssgmbh.com>
        perl 5.18.0 <gamo@telecable.es>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Tue, 28 May 2013 11:03:30 +0300
From: George Mpouras <nospam.gravitalsun.noadsplease@hotmail.noads.com>
Subject: An old trick that save me TONES of code
Message-Id: <ko1ob5$rus$1@news.ntua.gr>

# the following trick to create and write dynamic files save me a lot of 
code, thans perl !


my %file = ();


foreach my $user  (qw/george john jim/)  {
foreach my $color (qw/green blue white/) {
$file{$user}{$color} = [ my $fh, "$user.$color.txt" ];
open $file{$user}{$color}->[0] , '>' , $file{$user}{$color}->[1] or die 
"oups $^E\n" } }

print {$file{john}  {white}[0]} "i am in john white\n";
print {$file{george}{blue} [0]} "I am in george blue\n";

foreach my $user  (qw/george john jim/)  {
foreach my $color (qw/green blue white/) {
close $file{$user}{$color}->[0] or die "oups $^E\n" } }




------------------------------

Date: Tue, 28 May 2013 17:06:03 +0000 (UTC)
From: tmcd@panix.com (Tim McDaniel)
Subject: Re: An old trick that save me TONES of code
Message-Id: <ko2o5r$42f$1@reader1.panix.com>

In article <ko1ob5$rus$1@news.ntua.gr>,
George Mpouras  <nospam.gravitalsun.noadsplease@hotmail.noads.com> wrote:
># the following trick to create and write dynamic files save me a lot of 
>code, thans perl !
>
>
>my %file = ();
>
>
>foreach my $user  (qw/george john jim/)  {
>foreach my $color (qw/green blue white/) {
>$file{$user}{$color} = [ my $fh, "$user.$color.txt" ];
>open $file{$user}{$color}->[0] , '>' , $file{$user}{$color}->[1] or die 
>"oups $^E\n" } }
>
>print {$file{john}  {white}[0]} "i am in john white\n";
>print {$file{george}{blue} [0]} "I am in george blue\n";
>
>foreach my $user  (qw/george john jim/)  {
>foreach my $color (qw/green blue white/) {
>close $file{$user}{$color}->[0] or die "oups $^E\n" } }

The thing that's new to me is having a little array to associate the
filehandle and the filename.  That's neat -- thank you.  Is that what
you mean by "trick"?

I haven't tried running the code.  I notice
>open $file{$user}{$color}->[0]
but
>print {$file{john}  {white}[0]} "i am in john white\n";
Do you need the "->" in the first one?  I don't think so, but I don't
usually elide "->".

Does

>$file{$user}{$color} = [ my $fh, "$user.$color.txt" ];
>open $file{$user}{$color}->[0] , '>' , $file{$user}{$color}->[1] or ...

work, or does the first element of the [...] have to be a reference
like "\(my $fh)"?

I do see a possible drawback of this.  Some systems have a constraint
on the number of files that can be open at one time.  But I think that
any system I've used in the past 20 years can handle 9 files, as shown
in this example.

(Oh, and a minor misfeature: I think qw/george john jim/ should have
been qw/george john paul ringo/.)

-- 
Tim McDaniel, tmcd@panix.com


------------------------------

Date: Tue, 28 May 2013 13:25:01 -0500
From: "J. Gleixner" <glex_no-spam@qwest-spam-no.invalid>
Subject: Re: An old trick that save me TONES of code
Message-Id: <51a4f67d$0$75674$815e3792@news.qwest.net>

On 05/28/13 12:06, Tim McDaniel wrote:
> In article<ko1ob5$rus$1@news.ntua.gr>,
> George Mpouras<nospam.gravitalsun.noadsplease@hotmail.noads.com>  wrote:
>> # the following trick to create and write dynamic files save me a lot of
>> code, thans perl !
>>
>>
>> my %file = ();
>>
>>
>> foreach my $user  (qw/george john jim/)  {
>> foreach my $color (qw/green blue white/) {
>> $file{$user}{$color} = [ my $fh, "$user.$color.txt" ];
>> open $file{$user}{$color}->[0] , '>' , $file{$user}{$color}->[1] or die
>> "oups $^E\n" } }

To simplify it a bit, use a hash for everything. That way you have
a nice label for the data and avoid having to remember what's in [0]
vs [1] months later.

# not really needed, but to keep it similar to your example..
$file{ $user }{ $color }{ 'filename' } = "$user.$color.txt";

open ( $file{ $user }{ $color }{ 'fh' },
	'>' , $file{ $user }{ $color }{ 'filename' } ) or die "...";





------------------------------

Date: Tue, 28 May 2013 19:48:28 +0100
From: Rainer Weikusat <rweikusat@mssgmbh.com>
Subject: Re: An old trick that save me TONES of code
Message-Id: <87obbv9c03.fsf@sapphire.mobileactivedefense.com>

"J. Gleixner" <glex_no-spam@qwest-spam-no.invalid> writes:
> On 05/28/13 12:06, Tim McDaniel wrote:
>> In article<ko1ob5$rus$1@news.ntua.gr>,
>> George Mpouras<nospam.gravitalsun.noadsplease@hotmail.noads.com>  wrote:
>>> # the following trick to create and write dynamic files save me a lot of
>>> code, thans perl !
>>>
>>>
>>> my %file = ();
>>>
>>>
>>> foreach my $user  (qw/george john jim/)  {
>>> foreach my $color (qw/green blue white/) {
>>> $file{$user}{$color} = [ my $fh, "$user.$color.txt" ];
>>> open $file{$user}{$color}->[0] , '>' , $file{$user}{$color}->[1] or die
>>> "oups $^E\n" } }
>
> To simplify it a bit, use a hash for everything. That way you have
> a nice label for the data and avoid having to remember what's in [0]
> vs [1] months later.

'Having a nice label for the data' can also be achieved by declaring
symbolic constants mapping names to the index values, eg

use constant => {
	FH =>	0,
        FN =>	1
};

or

use constant FH =>	0;
use constant FN =>	1;

> # not really needed, but to keep it similar to your example..
> $file{ $user }{ $color }{ 'filename' } = "$user.$color.txt";

The {} in a hash lookup still autoquote everything which 'looks'
like a simple name (matched ^\w+$, according to Ben Morrow).


------------------------------

Date: Tue, 28 May 2013 21:36:15 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: An old trick that save me TONES of code
Message-Id: <v2hf7a-jgu.ln1@anubis.morrow.me.uk>


Quoth tmcd@panix.com:
> 
> I haven't tried running the code.  I notice
> >open $file{$user}{$color}->[0]
> but
> >print {$file{john}  {white}[0]} "i am in john white\n";
> Do you need the "->" in the first one?  I don't think so, but I don't
> usually elide "->".

No, you don't. You do need the braces around the filehandle for print,
though, unless the filehandle is a simple scalar. That is,

    my $fh = $file{john}{white}[0];
    print $fh "i am in john white\n";

works, but

    print $file{john}{white}[0] "i am in john white\n";

does not. (This is to do with perl having to look ahead to notice the
absence of a comma; with anything more complicated than a simple scalar
it 'forgets' it was doing that and just assumes the argument is
something to print.)

> Does
> 
> >$file{$user}{$color} = [ my $fh, "$user.$color.txt" ];
> >open $file{$user}{$color}->[0] , '>' , $file{$user}{$color}->[1] or ...
> 
> work, or does the first element of the [...] have to be a reference
> like "\(my $fh)"?

The 'my $fh' doesn't do anything for you. The scalar lexical is created,
its value (currently undef) is copied into the anon array, and then it
is not referenced again. This makes it equivalent to

    $file{$user}{$color} = [ undef, "$user.$color.txt" ];

Since the lexical was copied into the array, and the array element is
what was passed to open, the filehandle created by open is only placed
in the array element, and $fh will remain undef throughout. If you
wanted the filehandle in a separate variable you would have to open it
before creating the array, or copy it out afterwards.

As a point of style, I generally try to avoid repeating deep derefs like
that, so I would write something like

    my $file = $file{$user}{$color} = [undef, "..."];
    open $$file[0], ">", $$file[1] or ...;

(or write that as $file->[0] if you prefer).

> I do see a possible drawback of this.  Some systems have a constraint
> on the number of files that can be open at one time.  But I think that
> any system I've used in the past 20 years can handle 9 files, as shown
> in this example.

POSIX says that OPEN_MAX (the maximum number of files a process can have
open at a time) must be at least 20, though this can be reduced with
setrlimit. Modern systems typically allow hundreds of open files per
process even if they define relatively low static values for OPEN_MAX;
for instance, my machine is currently configured to allow 11095 per
process up to a total limit of 12328 altogether.

Ben



------------------------------

Date: Tue, 28 May 2013 19:31:50 -0700
From: Charles DeRykus <derykus@gmail.com>
Subject: Re: An old trick that save me TONES of code
Message-Id: <ko3pbi$rj4$1@speranza.aioe.org>

On 5/28/2013 10:06 AM, Tim McDaniel wrote:
> In article <ko1ob5$rus$1@news.ntua.gr>,
> George Mpouras  <nospam.gravitalsun.noadsplease@hotmail.noads.com> wrote:
>> # the following trick to create and write dynamic files save me a lot of
>> code, thans perl !
>>
>>
>> my %file = ();
>>
>>
>> foreach my $user  (qw/george john jim/)  {
>> foreach my $color (qw/green blue white/) {
>> $file{$user}{$color} = [ my $fh, "$user.$color.txt" ];
>> open $file{$user}{$color}->[0] , '>' , $file{$user}{$color}->[1] or die
>> "oups $^E\n" } }
>>
>> print {$file{john}  {white}[0]} "i am in john white\n";
>> print {$file{george}{blue} [0]} "I am in george blue\n";
>>
>> foreach my $user  (qw/george john jim/)  {
>> foreach my $color (qw/green blue white/) {
>> close $file{$user}{$color}->[0] or die "oups $^E\n" } }
>
> The thing that's new to me is having a little array to associate the
> filehandle and the filename.  That's neat -- thank you.  Is that what
> you mean by "trick"?
>
> I haven't tried running the code.  I notice
>> open $file{$user}{$color}->[0]
> but
>> print {$file{john}  {white}[0]} "i am in john white\n";
> Do you need the "->" in the first one?  I don't think so, but I don't
> usually elide "->".
>
> Does
>
>> $file{$user}{$color} = [ my $fh, "$user.$color.txt" ];
>> open $file{$user}{$color}->[0] , '>' , $file{$user}{$color}->[1] or ...
>
> work, or does the first element of the [...] have to be a reference
> like "\(my $fh)"?
>
> I do see a possible drawback of this.  Some systems have a constraint
> on the number of files that can be open at one time.  But I think that
> any system I've used in the past 20 years can handle 9 files, as shown
> in this example.
>

The allowable no. of open files on the Unix OS's I'm familiar with is 
always more than a thousand...although a particular host can be and 
often is more restrictive via setrlimit.

If that limit is exceeded, you'll get an EMFILE ("Too many open files").
although the core module FileCache can sometimes be used to get around
the limit...sort of.

-- 
Charles DeRykus


------------------------------

Date: Wed, 29 May 2013 11:12:48 +0100
From: Rainer Weikusat <rweikusat@mssgmbh.com>
Subject: Re: An old trick that save me TONES of code
Message-Id: <87ip22m6vz.fsf@sapphire.mobileactivedefense.com>

tmcd@panix.com (Tim McDaniel) writes:
> George Mpouras  <nospam.gravitalsun.noadsplease@hotmail.noads.com> wrote:

[...]


>>open $file{$user}{$color}->[0] , '>' , $file{$user}{$color}->[1] or die 
>>"oups $^E\n" } }
>>
>>print {$file{john}  {white}[0]} "i am in john white\n";

[...]

> I haven't tried running the code.  I notice
>>open $file{$user}{$color}->[0]
> but
>>print {$file{john}  {white}[0]} "i am in john white\n";
> Do you need the "->" in the first one?  I don't think so, but I don't
> usually elide "->".

According to perlref(1)

	The arrow is optional between brackets subscripts

This makes sense because Perl 'container objects' cannot contain
anything except scalars which means that

	$blah{7}[0]{33}

is unambigious: It must 'mean' $blah{7}->[0]->{33}.


>
> Does
>
>>$file{$user}{$color} = [ my $fh, "$user.$color.txt" ];
>>open $file{$user}{$color}->[0] , '>' , $file{$user}{$color}->[1] or ...
>
> work, or does the first element of the [...] have to be a reference
> like "\(my $fh)"?

Why would it need to be a reference to a scalar? Or anything, for that
matter? An uninitialized array element is as good a scalar whose value
is undef than any other scalar.

------------
my @a;

open($a[0], '<', '/etc/services');
print while defined($_ = readline($a[0]));
------------

NB: Using <> for I/O doesn't work here, cf

	If what's within the angle brackets is neither a filehandle
	nor a simple scalar variable containing a filehandle name,
	typeglob, or typeglob reference, it is interpreted
        as a filename pattern to be globbed
        [perlop(1)]

In the given context, I regard this as a misfeature.


------------------------------

Date: 28 May 2013 15:07:13 GMT
From: hymie@lactose.homelinux.net (hymie!)
Subject: Re: iCalendar module?
Message-Id: <51a4c821$0$3386$a8266bb1@newsreader.readnews.com>

In our last episode, the evil Dr. Lacto had captured our hero,
  "Peter J. Holzer" <hjp-usenet3@hjp.at>, who said:
>On 2013-05-23 19:50, hymie! <hymie@lactose.homelinux.net> wrote:
>> I found this ruby script
>>     cals = Icalendar.parse($<)
>>     cals.each do |cal|
>>       cal.events.each do |event|
>>         puts "Organizer: #{event.organizer}"
>>         puts "Event:     #{event.summary}"
>>         puts "Starts:    #{event.dtstart.myformat} local time"
>[...]
>>
>> and I was hoping to write something similar in perl.
>>
>I have used Text::vFile::asData to convert iCal MIME parts to something
>I can read. It handles all the v-type formats (vCalendar, vCard, ...),
>
>...
>my $data = Text::vFile::asData->new->parse(\*STDIN);
>
>my $vcalendar = $data->{objects}[0];
>die "unexpected type $vcalendar->{type}" unless $vcalendar->{type} eq
>'VCALENDAR';
>
>my $vevent;
>for (@{ $vcalendar->{objects} }) {
>    if ($_->{type} eq 'VEVENT') {
>        $vevent = $_; 
>        last; # there can be only one (or so we hope)
>    }   
>}
>die "no VEVENT found" unless $vevent;
>
>print "Summary  : $vevent->{properties}{SUMMARY}[0]{value}\n";
>print "\n";
>print "Begin    : ", fmttime($vevent->{properties}{DTSTART}[0]{value}), "\n";
>print "End      : ", fmttime($vevent->{properties}{DTEND}[0]{value}), "\n";
>print "\n";

Perfect!  Thank you very much.

--hymie!    http://lactose.homelinux.net/~hymie    hymie@lactose.homelinux.net
-------------------------------------------------------------------------------


------------------------------

Date: Tue, 28 May 2013 14:09:48 +0100
From: Rainer Weikusat <rweikusat@mssgmbh.com>
Subject: Re: it hurts when I press here
Message-Id: <87a9nfb68z.fsf@sapphire.mobileactivedefense.com>

Rainer Weikusat <rweikusat@mssgmbh.com> writes:

[...]

> I also don't quite understand why one would localize $@ and want to
> propagate errors out of the scope of the local at the same time. The
> easy way to accomplish that would be not localizing $@ to begin with.

I'd like to add that I've meanwhile found a reason for doing this: I
tend to do 'stuff' from DESTROY methods, eg, using a class to
represent some kind of 'externally visible event' and 'sending' that
'event' (usually in form of a message sent via some socket) from the
class destructor. Because of this, code running from a destructor
might run into supposed-to-be-fatal runtime errors caused by something
which is external to perl, eg, the kernel. This implies that $@ should
be localized in such a destructor to avoid clobbering an exception in
the process of being thrown and that any 'other exception' caused by
code running from the destructor should also affect the program in
case execution continues. It normally wouldn't because perl will eat
any runtime error occuring during destructor exection. My present idea
for dealing with this looks like this:

sub DESTROY
{
    local $@;

    eval {
        $_[0]->XDESTROY();
    };

    x_push($@) if $@;
}

this being a 'DESTROY' method classes using this facility can
import. It will then invoke the actual class destructor (somewhat
uncreatively named XDESTROY) and push 'an exception which happened
while doing that' onto an array maintained by the 'run loop'
module. Should processing again end up in the top-level event loop,
the most recently added exception in this array will be thrown and so
forth, until the program either terminates or the exception array is
again empty.


------------------------------

Date: Tue, 28 May 2013 22:30:26 +0200 (CEST)
From: gamo <gamo@telecable.es>
Subject: perl 5.18.0
Message-Id: <ko3452$9jq$1@speranza.aioe.org>

This version introduces randomized hashes. That no supose a speed
 penalty?

Thanks

-- 
posted by mobile device


------------------------------

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 3957
***************************************


home help back first fref pref prev next nref lref last post