[28042] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 9406 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Jun 29 14:06:14 2006

Date: Thu, 29 Jun 2006 11:05:06 -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, 29 Jun 2006     Volume: 10 Number: 9406

Today's topics:
    Re: accessing object variables from callback function anno4000@zrz.tu-berlin.de
    Re: accessing object variables from callback function <nobull67@gmail.com>
    Re: Date formatting <bart@nijlen.com>
    Re: Debugging Memory consumption for Perl Program <bart@nijlen.com>
        deflate parts of a file <sridhara@yahoo.com>
    Re: deflate parts of a file <Paul.Marquess@btinternet.com>
    Re: deflate parts of a file <sridhara@yahoo.com>
    Re: How to ignore 1st line in a file when reading <rvtol+news@isolution.nl>
    Re: How to ignore 1st line in a file when reading anno4000@zrz.tu-berlin.de
    Re: How to ignore 1st line in a file when reading <rvtol+news@isolution.nl>
    Re: Is this a Hash and how can I test a value? <qbert@comcast.net>
        Perl Object Creation problem... <howachen@gmail.com>
    Re: Perl Object Creation problem... <aukjan@gmail.com>
    Re: Perl Object Creation problem... <ced@blv-sam-01.ca.boeing.com>
    Re: Perl Object Creation problem... <howachen@gmail.com>
    Re: Perl Object Creation problem... <nobull67@gmail.com>
    Re: Perl Object Creation problem... <howachen@gmail.com>
    Re: Perl Object Creation problem... lawrence@hummer.not-here.net
    Re: Perl Object Creation problem... <howachen@gmail.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: 29 Jun 2006 10:07:09 GMT
From: anno4000@zrz.tu-berlin.de
Subject: Re: accessing object variables from callback function
Message-Id: <4ghn2dF1lg9b9U1@news.dfncis.de>

kaisung@gmail.com <kaisung@gmail.com> wrote in comp.lang.perl.misc:
> Hi,
> 
> I'm trying to write a Class which uses XML::Parser.  XML::Parser uses
> callback functions.  If I use an object method as the callback
> function, how can I access object variables from within that callback
> function if it doesn't get passed a reference to $self?

Then don't use the object method, but use a closure that calls the
object method.  You're not saying of what class the object method
is supposed to belong and what $self is going to be.  I'll assume
XML::Parser and $parser respectively.  See below (code untested).

> Here's what I
> have so far:
> 
> -----BEGIN PERL-----
> package Test;
> 
> use XML::Parser;
> 
> sub new {
>   my $invocant = shift;
>   my $class    = ref($invocant) || $invocant;
>   my $self     = { @_ };
>   bless($self, $class);
>   return $self;
> }
> 
> sub load {
>   my $parser = new XML::Parser(ErrorContext => 2);
>   $parser->setHandlers(Start => \&_start_handler);

Use a closure that encloses the object you want to call _start_handler()
with:

    $parser->setHandlers(Start => sub { $parser->_start_handler });

>   $parser->parsefile("file.xml");
> }
>
> sub _start_handler {
>   # In this function, I want to set $self->{var}, however don't have
>   # access to $self
> }

You do if it is called from the closure.

Anno


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

Date: 29 Jun 2006 07:36:11 -0700
From: "Brian McCauley" <nobull67@gmail.com>
Subject: Re: accessing object variables from callback function
Message-Id: <1151591771.666590.39950@m73g2000cwd.googlegroups.com>


anno4000@zrz.tu-berlin.de wrote:
> kaisung@gmail.com <kaisung@gmail.com> wrote in comp.lang.perl.misc:

> > I'm trying to write a Class which uses XML::Parser.  XML::Parser uses
> > callback functions.  If I use an object method as the callback
> > function, how can I access object variables from within that callback
> > function if it doesn't get passed a reference to $self?
>
> Then don't use the object method, but use a closure that calls the
> object method.  You're not saying of what class the object method
> is supposed to belong and what $self is going to be.  I'll assume
> XML::Parser and $parser respectively.  See below (code untested).

Actually it's more likely that the OP intended Test::load to be a
method.

> > -----BEGIN PERL-----
> > package Test;

> > sub load {
> >   my $parser = new XML::Parser(ErrorContext => 2);
> >   $parser->setHandlers(Start => \&_start_handler);
>
> Use a closure that encloses the object you want to call _start_handler()
> with:
>
>     $parser->setHandlers(Start => sub { $parser->_start_handler });

sub load {
   my $self = shift;
   my $parser = new XML::Parser(ErrorContext => 2);
   $parser->setHandlers(Start => sub { $self->start_handler } );
}



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

Date: 29 Jun 2006 04:26:55 -0700
From: "Bart Van der Donck" <bart@nijlen.com>
Subject: Re: Date formatting
Message-Id: <1151580415.690711.119230@x69g2000cwx.googlegroups.com>

Ted wrote:

> I am trying to use Date::Manip, and have most of what I need working.
>
> The final peice, though, is eluding me.  The following statement works
> fine:
>
> $market_date = DateCalc($file_time,ParseDateDelta("- 1 business
> days"));
>
> I does precisely what I need, WRT computation.  However, I have yet to
> figure out how to tell it I want the date part only, and that the time
> part can be discarded.
>
> I may have just missed the relevant part of the documentation that
> tells me this, but I haven't found, in the Date::Manip documentation,
> how to get only the date part of the return value from DateCalc.  How
> do I do this?

One way is to manually cut out the part you need. The first 8
characters from your output string should always hold the date in
YYYYMMDD-format:

 #!perl
 use strict;
 use warnings;
 use Date::Manip;
 print substr(DateCalc('2006-05-29 08:00:00','+3 business days'),0,8);
  
Hope this helps,

-- 
 Bart



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

Date: 29 Jun 2006 04:45:56 -0700
From: "Bart Van der Donck" <bart@nijlen.com>
Subject: Re: Debugging Memory consumption for Perl Program
Message-Id: <1151581556.553475.317430@i40g2000cwc.googlegroups.com>

janicehwang1325@yahoo.com wrote:

> I would like to know is there any way to debug the memory consumption
> of a perl program running? For example, I have a program running and i
> would like to keep tract of the memory consumption of this program so
> that I can retart the program if the memory consumption is going to
> high. Thank you very much.

Yes, there are a number of techniques for that. See:

http://perldoc.perl.org/perldebguts.html#Debugging-Perl-memory-usage

-- 
 Bart



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

Date: 29 Jun 2006 09:03:19 -0700
From: "sid" <sridhara@yahoo.com>
Subject: deflate parts of a file
Message-Id: <1151596999.818114.126060@y41g2000cwy.googlegroups.com>

I need to compress parts of a file, say some sections of an xml file
while leaving others as ASCII on the ouput stream. I took the deflate
example from cpan and started gathering all lines of a section in a
string and then doing one deflate, ouput, flush and output the
compressed section. But this doesn't seem to be working. I am seeing
"use of uninitialized values" warnings and the final output file
doesn't look right. I would see some compressed data then a whole bunch
of xml without any compressed data in between and so forth. The
compress method has a horrible performance. It actually made the file
much bigger. Hence I only want to use the deflate method. It works fine
on the whole file. But I want to use it on sections of a file. Are
there examples that someone can point me to?

thanks
SS



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

Date: Thu, 29 Jun 2006 17:39:15 +0100
From: "Paul Marquess" <Paul.Marquess@btinternet.com>
Subject: Re: deflate parts of a file
Message-Id: <44a40251$0$11539$4d4eb98e@read.news.uk.uu.net>

"sid" <sridhara@yahoo.com> wrote in message 
news:1151596999.818114.126060@y41g2000cwy.googlegroups.com...
>I need to compress parts of a file, say some sections of an xml file
> while leaving others as ASCII on the ouput stream. I took the deflate
> example from cpan

Which deflate example are you referring to?

>and started gathering all lines of a section in a
> string and then doing one deflate, ouput, flush and output the
> compressed section. But this doesn't seem to be working. I am seeing
> "use of uninitialized values" warnings and the final output file
> doesn't look right.
> I would see some compressed data then a whole bunch
> of xml without any compressed data in between and so forth.

Can you post some code please?

> The
> compress method has a horrible performance. It actually made the file
> much bigger.
> Hence I only want to use the deflate method. It works fine
> on the whole file.

Compression isn't worth the effort on small strings. Ending up with 
something bigger is one of the main reasons for not doing it.

> But I want to use it on sections of a file. Are
> there examples that someone can point me to?

Paul 




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

Date: 29 Jun 2006 10:16:44 -0700
From: "sid" <sridhara@yahoo.com>
Subject: Re: deflate parts of a file
Message-Id: <1151601404.346322.71780@m73g2000cwd.googlegroups.com>

use Compress::Zlib ;
    binmode STDIN;
    binmode STDOUT;
    $x = deflateInit()
       or die "Cannot create a deflation stream\n" ;
    while (<>)
    {
        ($output, $status) = $x->deflate($_) ;


        $status == Z_OK
            or die "deflation failed\n" ;
        print $output ;
    }
    ($output, $status) = $x->flush() ;
    $status == Z_OK
        or die "deflation failed\n" ;
    print $output ;
--------------------------------------------------------------
My code is based on this:
while(<>){
if(begin_xmltag)then set flag
if(flag){$string.=$_}
else {print $_}
if(end_xmltag) {
set output to compressed string using deflate procedre
print $output
do a flush on the deflate stream
set string to null
}
}


Paul Marquess wrote:
> "sid" <sridhara@yahoo.com> wrote in message
> news:1151596999.818114.126060@y41g2000cwy.googlegroups.com...
> >I need to compress parts of a file, say some sections of an xml file
> > while leaving others as ASCII on the ouput stream. I took the deflate
> > example from cpan
>
> Which deflate example are you referring to?
>
> >and started gathering all lines of a section in a
> > string and then doing one deflate, ouput, flush and output the
> > compressed section. But this doesn't seem to be working. I am seeing
> > "use of uninitialized values" warnings and the final output file
> > doesn't look right.
> > I would see some compressed data then a whole bunch
> > of xml without any compressed data in between and so forth.
>
> Can you post some code please?
>
> > The
> > compress method has a horrible performance. It actually made the file
> > much bigger.
> > Hence I only want to use the deflate method. It works fine
> > on the whole file.
>
> Compression isn't worth the effort on small strings. Ending up with
> something bigger is one of the main reasons for not doing it.
>
> > But I want to use it on sections of a file. Are
> > there examples that someone can point me to?
> 
> Paul



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

Date: Thu, 29 Jun 2006 13:38:26 +0200
From: "Dr.Ruud" <rvtol+news@isolution.nl>
Subject: Re: How to ignore 1st line in a file when reading
Message-Id: <e80lbt.1d4.1@news.isolution.nl>

Dr.Ruud schreef:

>   @array = <$fh> ;
>   shift @array ;

But don't do it that way if your first line can be multi-MB.

-- 
Affijn, Ruud

"Gewoon is een tijger."




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

Date: 29 Jun 2006 12:16:32 GMT
From: anno4000@zrz.tu-berlin.de
Subject: Re: How to ignore 1st line in a file when reading
Message-Id: <4ghul0F1nb2baU1@news.dfncis.de>

Dr.Ruud <rvtol+news@isolution.nl> wrote in comp.lang.perl.misc:
> Deepu schreef:
> 
> > I would like to know when reading a file which has around 10 - 20
> > lines in it. How can it be done to ignore 1st line and start reading
> > from 2nd till last.

[...]

>   @array = <$fh> ;
>   shift @array ;

    ( undef, @array) = <$fh>;

Anno


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

Date: Thu, 29 Jun 2006 18:03:22 +0200
From: "Dr.Ruud" <rvtol+news@isolution.nl>
Subject: Re: How to ignore 1st line in a file when reading
Message-Id: <e814rg.16c.1@news.isolution.nl>

anno4000@zrz.tu-berlin.de schreef:
> Dr.Ruud:
>> Deepu:

>>> I would like to know when reading a file which has around
>>> 10 - 20 lines in it. How can it be done to ignore 1st line
>>> and start reading from 2nd till last.
>>
>>   @array = <$fh> ;
>>   shift @array ;
>
>     ( undef, @array) = <$fh>;

Yes, good correction.

-- 
Affijn, Ruud

"Gewoon is een tijger."




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

Date: Thu, 29 Jun 2006 12:43:01 -0400
From: "Jockser" <qbert@comcast.net>
Subject: Re: Is this a Hash and how can I test a value?
Message-Id: <BP-dnf2iAeSRnjnZnZ2dnUVZ_oadnZ2d@comcast.com>

Thanks for the responses worked out good. 




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

Date: 29 Jun 2006 03:31:29 -0700
From: "howa" <howachen@gmail.com>
Subject: Perl Object Creation problem...
Message-Id: <1151577089.344510.63220@d56g2000cwd.googlegroups.com>

Hi,

I encountered a problem when using perl's OOP feature"


Directory Layout:
----------------------
c:\temp\perl\test.pl
c:\temp\perl\class\user.pm


user.pm
----------------------
package class::user;

sub new {
    my ($class) = @_;
    my $objref = {};

    bless $objref, $class;
}

sub print_me {
    my ($self) = @_;

    print "is me!";
}

1;



----------------------
test.pl
----------------------
use lib "C:\\temp\\perl";

use class::user;

$u = new user();
----------------------


and it returns :

Can't locate object method "new" via package "user" (perhaps you forgot
to load
"user"?) at test.pl line 5.

what's wrong with the codes?

thanks...



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

Date: Thu, 29 Jun 2006 13:31:10 +0200
From: Aukjan van Belkum <aukjan@gmail.com>
Subject: Re: Perl Object Creation problem...
Message-Id: <206bf$44a3b9b9$c2abfc64$27572@news2.tudelft.nl>

howa wrote:

> use lib "C:\\temp\\perl";
> 
> use class::user;
> 
> $u = new user();
> ----------------------
> 

This should be:
	$u = new class::user;    (or pref.: my $u = new class::user;)
or
	$u = class::user->new(); (or pref.: my $u = class::user->new();)


Aukjan.


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

Date: Thu, 29 Jun 2006 13:02:44 GMT
From: Charles DeRykus <ced@blv-sam-01.ca.boeing.com>
Subject: Re: Perl Object Creation problem...
Message-Id: <J1MHKL.K3w@news.boeing.com>

howa wrote:
> Hi,
> 
> I encountered a problem when using perl's OOP feature"
> 
> 
> Directory Layout:
> ----------------------
> c:\temp\perl\test.pl
> c:\temp\perl\class\user.pm
> 
> 
> user.pm
> ----------------------
> package class::user;
> 
> sub new {
>     my ($class) = @_;
>     my $objref = {};
> 
>     bless $objref, $class;
> }
> 
> sub print_me {
>     my ($self) = @_;
> 
>     print "is me!";
> }
> 
> 1;
> 
> 
> 
> ----------------------
> test.pl
> ----------------------
> use lib "C:\\temp\\perl";
> 
> use class::user;
> 
> $u = new user();
> ----------------------
> 
> 
> and it returns :
> 
> Can't locate object method "new" via package "user" (perhaps you forgot
> to load
> "user"?) at test.pl line 5.
> 
> what's wrong with the codes?

I'm not sure what you intended with the parens but `$u = new user()`
gets parsed as 'user->new'. However, your class is 'class::user' not
'user' so you'll need:  $u = new class::user which is better expressed 
as user::class->new.  Customarily, package names start with an upper 
case letter too, eg, Class::User. 'Class' is usually not an appropriate
choice for the name either.

You might want to review the OO documentation for more info:

perlboot            Perl OO tutorial for beginners
perltoot            Perl OO tutorial, part 1
perltootc           Perl OO tutorial, part 2
perlobj             Perl objects
perlbot             Perl OO tricks and examples

hth,
-- 
Charles DeRykus


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

Date: 29 Jun 2006 07:19:14 -0700
From: "howa" <howachen@gmail.com>
Subject: Re: Perl Object Creation problem...
Message-Id: <1151590754.424368.10660@x69g2000cwx.googlegroups.com>


Aukjan van Belkum =E5=AF=AB=E9=81=93=EF=BC=9A

> howa wrote:
>
> > use lib "C:\\temp\\perl";
> >
> > use class::user;
> >
> > $u =3D new user();
> > ----------------------
> >
>
> This should be:
> 	$u =3D new class::user;    (or pref.: my $u =3D new class::user;)
> or
> 	$u =3D class::user->new(); (or pref.: my $u =3D class::user->new();)
>
>
> Aukjan.

thx first....

but is it possible to use

$u =3D new user;

just like in c++, i.e. using namespace std;

sorry as i am really new to perl...



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

Date: 29 Jun 2006 07:44:25 -0700
From: "Brian McCauley" <nobull67@gmail.com>
Subject: Re: Perl Object Creation problem...
Message-Id: <1151592265.708375.12870@d56g2000cwd.googlegroups.com>


howa wrote:
> Aukjan van Belkum =E5=AF=AB=E9=81=93=EF=BC=9A
>
> > howa wrote:
> >
> > > use lib "C:\\temp\\perl";
> > >
> > > use class::user;
> > >
> > > $u =3D new user();
> > > ----------------------
> > >
> >
> > This should be:
> > 	$u =3D new class::user;    (or pref.: my $u =3D new class::user;)
> > or
> > 	$u =3D class::user->new(); (or pref.: my $u =3D class::user->new();)
> but is it possible to use
>
> $u =3D new user;
>
> just like in c++, i.e. using namespace std;

No, there is no such mechanism builtin[1] to Perl5.  All class names
must be specified absolutely.

[1] I say builtin because it's just the sort of thing you might expect
to see in an Acme::* modules. Note Acme::* modules are only intended as
jokes.



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

Date: 29 Jun 2006 08:40:18 -0700
From: "howa" <howachen@gmail.com>
Subject: Re: Perl Object Creation problem...
Message-Id: <1151595618.294715.113300@m73g2000cwd.googlegroups.com>


Brian McCauley =E5=AF=AB=E9=81=93=EF=BC=9A

> howa wrote:
> > Aukjan van Belkum =E5=AF=AB=E9=81=93=EF=BC=9A
> >
> > > howa wrote:
> > >
> > > > use lib "C:\\temp\\perl";
> > > >
> > > > use class::user;
> > > >
> > > > $u =3D new user();
> > > > ----------------------
> > > >
> > >
> > > This should be:
> > > 	$u =3D new class::user;    (or pref.: my $u =3D new class::user;)
> > > or
> > > 	$u =3D class::user->new(); (or pref.: my $u =3D class::user->new();)
> > but is it possible to use
> >
> > $u =3D new user;
> >
> > just like in c++, i.e. using namespace std;
>
> No, there is no such mechanism builtin[1] to Perl5.  All class names
> must be specified absolutely.
>
> [1] I say builtin because it's just the sort of thing you might expect
> to see in an Acme::* modules. Note Acme::* modules are only intended as
> jokes.

it seems a little stupid if  the class name must be the package full
name...

anyway, thanks!



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

Date: 29 Jun 2006 09:54:48 -0700
From: lawrence@hummer.not-here.net
Subject: Re: Perl Object Creation problem...
Message-Id: <87slln6gx3.fsf@hummer.i-did-not-set--mail-host-address--so-shoot-me>

"howa" <howachen@gmail.com> writes:
> 
> it seems a little stupid if  the class name must be the package full
> name...
> 

Perhaps this will offer enlightenement:

Suppose I have two classes  Road::Apple and Fruit::Apple 

#/usr/local/bin/howas-private-perl-like-language
use Road::Apple;
use Fruit::Apple;

my $thing = new Apple;

What kind of apple whould $thing be?  One is delicious, the other not
so much.

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
	Lawrence Statton - lawrenabae@abaluon.abaom s/aba/c/g
Computer  software  consists of  only  two  components: ones  and
zeros, in roughly equal proportions.   All that is required is to
sort them into the correct order.


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

Date: 29 Jun 2006 10:14:59 -0700
From: "howa" <howachen@gmail.com>
Subject: Re: Perl Object Creation problem...
Message-Id: <1151601299.426528.285910@p79g2000cwp.googlegroups.com>


lawre...@hummer.not-here.net =E5=AF=AB=E9=81=93=EF=BC=9A

> "howa" <howachen@gmail.com> writes:
> >
> > it seems a little stupid if  the class name must be the package full
> > name...
> >
>
> Perhaps this will offer enlightenement:
>
> Suppose I have two classes  Road::Apple and Fruit::Apple
>
> #/usr/local/bin/howas-private-perl-like-language
> use Road::Apple;
> use Fruit::Apple;
>
> my $thing =3D new Apple;
>
> What kind of apple whould $thing be?  One is delicious, the other not
> so much.
>
> -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
> 	Lawrence Statton - lawrenabae@abaluon.abaom s/aba/c/g
> Computer  software  consists of  only  two  components: ones  and
> zeros, in roughly equal proportions.   All that is required is to
> sort them into the correct order.

in other language, you also have this problem, java or c.

in fact, this is not a problem at all, if you want to specifiy, you can
still use the full path if needed.

the namespace and the class name are not the same thing.



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

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 V10 Issue 9406
***************************************


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