[31654] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 2917 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Apr 22 06:09:26 2010

Date: Thu, 22 Apr 2010 03:09:11 -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, 22 Apr 2010     Volume: 11 Number: 2917

Today's topics:
    Re: Best way to extract content of SVN target/revision  <davidmichaelkarr@gmail.com>
    Re: Extending a class that uses 'fields' <paduille.4061.mumia.w+nospam@earthlink.net>
    Re: Extending a class that uses 'fields' <fred@resel.fr>
    Re: Extending a class that uses 'fields' <fred@resel.fr>
    Re: Extending a class that uses 'fields' <fperrin@pc-df-203.priv.enst-bretagne.fr>
    Re: FAQ 5.35 How do I close a file descriptor by number <hjp-usenet2@hjp.at>
    Re: FAQ 5.35 How do I close a file descriptor by number <uri@StemSystems.com>
    Re: FAQ 5.35 How do I close a file descriptor by number <ben@morrow.me.uk>
    Re: FAQ 5.35 How do I close a file descriptor by number <derykus@gmail.com>
    Re: FAQ 7.26 How can I find out my current or calling p <ben@morrow.me.uk>
    Re: FAQ 7.26 How can I find out my current or calling p <xhoster@gmail.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Wed, 21 Apr 2010 17:03:21 -0700 (PDT)
From: "david.karr" <davidmichaelkarr@gmail.com>
Subject: Re: Best way to extract content of SVN target/revision for searching?
Message-Id: <03cc49e3-d241-41c4-81d5-0579eef471fb@r1g2000yqb.googlegroups.com>

On Apr 20, 8:34=A0pm, Christian Winter <thepoet_nos...@arcor.de> wrote:
> david.karr wrote:
>
> [...]
>
> > Next, I have to be able to get the text content of the target element
> > at each of those revisions, in order to search for a substring. =A0The
> > only obvious interface in SVN::Client that gives me access to the
> > element content is the "cat()" method, which writes the content to a
> > file descriptor. I suppose I could get this to work, but this seems
> > pretty indirect.
>
> > Assuming I'm not missing a different way to do this with SVN::Client,
> > among the numerous other SVN Perl packages, is there another one that
> > would give me convenient access to the content of an element at a
> > revision?
>
> You can always use Perl's feature of "in memory" files to avoid
> writing to disk and re-reading the contents:
>
> my $memfile;
> open( CURRFILE, ">", \$memfile)
> =A0 =A0or die "Failed to open in-memory file: $!";
> $ctx->cat(\*CURRFILE, $target, $revision, $pool);
>
> -Chris

Ok, I think this basically works, but I'm finding that some revisions
of an element fail with a "Filesystem has no item: File not found:
revision ..." error.  I got the list of revisions to check with
something like this:

$ctx->log([$opt_element], 1, "HEAD", 1, 0, \&addToRevisionsList);

sub addToRevisionsList($$$$$$) {
    my ($changed_paths, $revision, $author, $date, $message) =3D @_;
    print "revision[" . $revision . "]\n";
    push(@revisionsList, $revision);
}

When I later try to iterate through them with something like this:

for my $revision (@revisionsList) {
    print $revision . "\n";
    my $memfile;
    open( CURRFILE, ">", \$memfile)
	or die "Failed to open in-memory file: $!";
    $ctx->cat(\*CURRFILE, $opt_element, $revision);
    print "file[" . $memfile . "]\n";
}

The first few entries in the revisions list fails with that error.  I
tried manually submitting the last revision in the list, and that
works fine.

How can I either only run "cat()" on revisions where I somehow know
it's going to work, or safely fail the "cat()" calls that don't have a
file associated with them?  In the current state of the script, the
first "cat()" that fails kills the script.


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

Date: Wed, 21 Apr 2010 15:15:46 -0500
From: "Mumia W." <paduille.4061.mumia.w+nospam@earthlink.net>
Subject: Re: Extending a class that uses 'fields'
Message-Id: <-PKdnar34pcK_FLWnZ2dnUVZ_umdnZ2d@earthlink.com>

On 04/21/2010 03:24 AM, Frédéric Perrin wrote:
> Hello,
> 
> I want to subclass Net::SSH::Expect, by adding a couple of fields to
> it.  Using "perldoc fields" as a guide, I did the following:
> 
> ---------------- 8< ----------------
> 
> #!/usr/bin/perl
> [...]

Ben Morrow gave you good advice; however, if inside-out objects are not 
your thing, Class::Struct makes it easy to create accessor methods which 
can be inherited into other classes, e.g.:

#!/usr/bin/perl
use strict;
use warnings;
use feature 'say';
use IO::File;

my $file = NewFile->new;
$file->new_field(91);
say $file->new_field();

package NewFile;
use Class::Struct NewFileBase => { new_field => '$' };
use base 'NewFileBase';
use base 'IO::File';

__HTH__


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

Date: Thu, 22 Apr 2010 11:00:44 +0200
From: =?utf-8?B?RnLDqWTDqXJpYw==?= Perrin  <fred@resel.fr>
Subject: Re: Extending a class that uses 'fields'
Message-Id: <kggvdbjzwsj.fsf@pc-df-203.priv.enst-bretagne.fr>

Ben Morrow <ben@morrow.me.uk> writes:
> Quoth Frédéric Perrin  <fred@resel.fr>:
>                                                                    Use
> an inside-out object instead:
>
>     use Scalar::Util qw/refaddr/;
>
>     my %new_field;
>
>     sub new {
>        my $class = shift;
>        my $self = $class->SUPER::new(...);
>        $new_field{refaddr $self} = "something";
>     }
>
> Be aware that this will fail if your program uses threads. If you can
> depend on 5.10, you can use the core Hash::Util::Fieldhash to fix this
> (and also avoid the 'refaddr'). You can also use Object::InsideOut,
> which works on older perls but is a little heavy for my taste.

Unfortunately, I have to run of RHEL5 with perl 5.8.8. I'm not using
threads, but I don't want to close that door in the future. If I
understand correctly [1], the problem with threads is the refaddr; if
I use an other index that I know is unique between my objects (in my
example, it could be the hostname of the remote server), that will
work, right?

[1] <http://www.perlfoundation.org/perl5/index.cgi?inside_out_object> writes:
> Inside-out objects based on hashes that use the memory address of the
> object as the index are not normally thread safe. In a new thread, an
> object's memory address will be different and thus all old data
> associated with that object will be lost (which also causes a memory
> leak).

Otherwise, I'll be using Object::InsideOut, which does pull in a
number of external dependencies.

>>     sub new {
>>     my $class = shift;
>>     my $self = fields::new($class);
>>         $self->SUPER::new(host => "server.invalid",
>>                           password => "password");
>
> This is wrong. Net::SSH::Expect->new is a class method, not an object
> method, so you should be calling it on the class.
>
>     my $self = $class->SUPER::new(...);
>
> I believe (though I haven't tested it) that this will fix your original
> problem, as well, since N::S::E->new will now call fields::new with your
> classname.

Corrected. However, $self is still empty according to Dumper $self. I
haven't investigated more than that.

Thanks for your time.

-- 
Fred


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

Date: Thu, 22 Apr 2010 11:04:37 +0200
From: =?utf-8?B?RnLDqWTDqXJpYw==?= Perrin  <fred@resel.fr>
Subject: Re: Extending a class that uses 'fields'
Message-Id: <kggr5m7zwm2.fsf@pc-df-203.priv.enst-bretagne.fr>

"Mumia W." <paduille.4061.mumia.w+nospam@earthlink.net> writes:
> On 04/21/2010 03:24 AM, Frédéric Perrin wrote:
>> I want to subclass Net::SSH::Expect, by adding a couple of fields to
>> it.  Using "perldoc fields" as a guide, I did the following:
>>
>> ---------------- 8< ----------------
>> #!/usr/bin/perl
>> [...]
>
> Ben Morrow gave you good advice; however, if inside-out objects are
> not your thing, Class::Struct makes it easy to create accessor methods
> which can be inherited into other classes, e.g.:

IIUC [1], I can't use that to extend Net::SSH::Expect if I don't want
to touch its code. Do I understand correctly ?

[1] <http://search.cpan.org/~jesse/perl-5.12.0/lib/Class/Struct.pm#The_struct()_function>:
> The class created by struct must not be a subclass of another class
> other than UNIVERSAL.
>
> It can, however, be used as a superclass for other classes. To
> facilitate this, the generated constructor method uses a two-argument
> blessing. Furthermore, if the class is hash-based, the key of each
> element is prefixed with the class name (see Perl Cookbook, Recipe
> 13.12).

-- 
Fred


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

Date: Thu, 22 Apr 2010 11:17:11 +0200
From: PERRIN Frederic   <fperrin@pc-df-203.priv.enst-bretagne.fr>
Subject: Re: Extending a class that uses 'fields'
Message-Id: <kggmxwvzw14.fsf@pc-df-203.priv.enst-bretagne.fr>

Frédéric Perrin  <fred@resel.fr> writes:
> Ben Morrow <ben@morrow.me.uk> writes:
>> Quoth Frédéric Perrin  <fred@resel.fr>:
>>>     sub new {
>>>     my $class = shift;
>>>     my $self = fields::new($class);
>>>         $self->SUPER::new(host => "server.invalid",
>>>                           password => "password");
>>
>> This is wrong. Net::SSH::Expect->new is a class method, not an
>> object method, so you should be calling it on the class.
>>
>>     my $self = $class->SUPER::new(...);
>>
>> I believe (though I haven't tested it) that this will fix your
>> original problem, as well, since N::S::E->new will now call
>> fields::new with your classname.
>
> Corrected. However, $self is still empty according to Dumper $self.
> I haven't investigated more than that.

Stupid me. I had simply replaced "$self->SUPER::new" with
"$class->SUPER::new", without saving the results to $self. Of *course*
$self wasn't going to be modified by some class method... But there is
still the possibility you pointed out earlier, with names clashes.

-- 
Fred


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

Date: Thu, 22 Apr 2010 00:56:21 +0200
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: FAQ 5.35 How do I close a file descriptor by number?
Message-Id: <slrnhsv0kl.1mk.hjp-usenet2@hrunkner.hjp.at>

On 2010-04-21 15:42, Ben Morrow <ben@morrow.me.uk> wrote:
> File::Copy (at least, v2.14, which comes with 5.10.1) uses
>     
>     my $fh = \do { local *FH };
>
> which is how you created a scoped filehandle before 5.6. At least in
> modern perls, these handles will also auto-close on scope exit.

Ah, nice trick. Too bad I learn about it only now that I don't need it
any more :-).

	hp


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

Date: Wed, 21 Apr 2010 19:09:20 -0400
From: "Uri Guttman" <uri@StemSystems.com>
Subject: Re: FAQ 5.35 How do I close a file descriptor by number?
Message-Id: <87aaswpfmn.fsf@quad.sysarch.com>

>>>>> "PJH" == Peter J Holzer <hjp-usenet2@hjp.at> writes:

  PJH> On 2010-04-21 15:42, Ben Morrow <ben@morrow.me.uk> wrote:
  >> File::Copy (at least, v2.14, which comes with 5.10.1) uses
  >> 
  >> my $fh = \do { local *FH };
  >> 
  >> which is how you created a scoped filehandle before 5.6. At least in
  >> modern perls, these handles will also auto-close on scope exit.

  PJH> Ah, nice trick. Too bad I learn about it only now that I don't need it
  PJH> any more :-).

even older or at least the same age is the Symbol module which basically
does the same thing. it first increments a string to create a new
symbol, then allocates that glob and deletes the symbol. you get back
the anon glob which can be used for any handle. the above code does a
similar but shorter trick in temporarily allocating a new glob (the
local) and then getting a reference to it while the local leaves
scope. so no one else has access to the new glob which makes it anon
too.

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------


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

Date: Thu, 22 Apr 2010 00:34:31 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: FAQ 5.35 How do I close a file descriptor by number?
Message-Id: <7le4a7-qj1.ln1@osiris.mauzo.dyndns.org>


Quoth "Peter J. Holzer" <hjp-usenet2@hjp.at>:
> On 2010-04-21 15:42, Ben Morrow <ben@morrow.me.uk> wrote:
> > File::Copy (at least, v2.14, which comes with 5.10.1) uses
> >     
> >     my $fh = \do { local *FH };
> >
> > which is how you created a scoped filehandle before 5.6. At least in
> > modern perls, these handles will also auto-close on scope exit.
> 
> Ah, nice trick. Too bad I learn about it only now that I don't need it
> any more :-).

It's still occasionally useful, for instance with IPC::Open3 which can't
autoviv handles (because it already had a meaning for undef before 5.6).
As Uri says, you can also use the Symbol module; in principle if you're
creating lots of filehandles you could use Symbol::geniosym or

    do { local *FH; *FH{IO} }

to save a bit of memory. (An IO ref ought to work as a filehandle
everywhere a globref does, and you save the GV+GP+SV that you aren't
using.)

Ben



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

Date: Wed, 21 Apr 2010 22:05:40 -0700 (PDT)
From: "C.DeRykus" <derykus@gmail.com>
Subject: Re: FAQ 5.35 How do I close a file descriptor by number?
Message-Id: <f8d27681-10c7-4d0c-a6d4-8b047684e0f1@m25g2000prj.googlegroups.com>

On Apr 21, 11:09=A0am, Vilmos Soti <vil...@soti.ca> wrote:
> "Peter J. Holzer" <hjp-usen...@hjp.at> writes:
>
> > But it looks like you care about Perl handles anyway, not about file
> > descriptors.
>
> Well, yes.
>
> > and then wrap the copy operations into an eval block:
>
> I guess the solution in this case is to use eval, and there no
> sane way to have a list of file descriptors.

Another possibility, although not particularly efficient,
 is just redefine the handler to not interrupt the copy
 and run bye() in an END{}:

 #!/usr/bin/perl

 my $INT =3D 0;
 ...

 my $find_code =3D
    sub {  local $SIG{INT} =3D sub { $INT =3D 1; };
           copy ...;
           exit if $INT
        };
 ...

 END { bye; }

--
Charles DeRykus


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

Date: Wed, 21 Apr 2010 23:27:06 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: FAQ 7.26 How can I find out my current or calling package?
Message-Id: <qma4a7-scj2.ln1@osiris.mauzo.dyndns.org>


Quoth PerlFAQ Server <brian@theperlreview.com>:
> 
>     Most of the time, you shouldn't care what package an object is blessed
>     into, however, as long as it claims to inherit from that class:
> 
>             my $is_right_class = eval { $object->isa( $package ) }; #
> true or false

Should this answer mention ->DOES at this point? AIUI, most of the time
it's a more appropriate test than ->isa, but I must admit I'm not
entirely clear on the difference between the two.

Ben



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

Date: Wed, 21 Apr 2010 19:25:19 -0700
From: Xho Jingleheimerschmidt <xhoster@gmail.com>
Subject: Re: FAQ 7.26 How can I find out my current or calling package?
Message-Id: <4bcfb67f$0$15832$ed362ca5@nr5-q3a.newsreader.com>

Ben Morrow wrote:
> Quoth PerlFAQ Server <brian@theperlreview.com>:
>>     Most of the time, you shouldn't care what package an object is blessed
>>     into, however, as long as it claims to inherit from that class:
>>
>>             my $is_right_class = eval { $object->isa( $package ) }; #
>> true or false
> 
> Should this answer mention ->DOES at this point?

Where is that documented?  I've heard of ->can, but never of ->DOES.

Is this a 5.10 thing?

And shouldn't there be an entry for "can" in perldoc -f?  I know it 
isn't strictly a function, but it is closer to one that "use" or "my".

Xho


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

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


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