[24758] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 6911 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Aug 25 18:11:03 2004

Date: Wed, 25 Aug 2004 15:10:10 -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, 25 Aug 2004     Volume: 10 Number: 6911

Today's topics:
        Overriding *all* methods <goodcall1@hotmail.dot.com>
    Re: Overriding *all* methods <dmcbride@naboo.to.org.no.spam.for.me>
    Re: Overriding *all* methods <nobull@mail.com>
    Re: Overriding *all* methods <goodcall1@hotmail.dot.com>
    Re: Overriding *all* methods <notvalid@email.com>
        Own module needs loaded module from main script (Bart Van der Donck)
    Re: Own module needs loaded module from main script <noreply@gunnar.cc>
    Re: Parsing FileName for upload <news3@sidestreet.tzo.com>
    Re: Parsing FileName for upload <tore@aursand.no>
    Re: Parsing FileName for upload (Tony McGuire)
        Problem with parsing cmika@-takethisout-25thandclement.com
    Re: Problem with parsing <mritty@gmail.com>
    Re: Problem with parsing <mritty@gmail.com>
    Re: Problem with parsing <tore@aursand.no>
    Re: split question <someone@example.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Wed, 25 Aug 2004 19:36:50 GMT
From: "Jack D" <goodcall1@hotmail.dot.com>
Subject: Overriding *all* methods
Message-Id: <mT5Xc.44063$S55.4545@clgrps12>

I want to subclass a module in order to add some code to *each and every*
method inherited. Is there an easy way to override all methods of a package
without knowing the names of the methods in advance?

Jack




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

Date: Wed, 25 Aug 2004 19:56:17 GMT
From: Darin McBride <dmcbride@naboo.to.org.no.spam.for.me>
Subject: Re: Overriding *all* methods
Message-Id: <B96Xc.205423$M95.63590@pd7tw1no>

Jack D wrote:

> I want to subclass a module in order to add some code to *each and every*
> method inherited. Is there an easy way to override all methods of a
> package without knowing the names of the methods in advance?

I don't think you really want to subclass.  I think you want to embed,
and then you can use AUTOLOAD.  That is, have the parent object as a
piece of data in your object.




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

Date: Wed, 25 Aug 2004 21:02:09 +0100
From: Brian McCauley <nobull@mail.com>
Subject: Re: Overriding *all* methods
Message-Id: <cgir06$i2j$1@sun3.bham.ac.uk>

Jack D wrote:
> I want to subclass a module in order to add some code to *each and every*
> method inherited. Is there an easy way to override all methods of a package
> without knowing the names of the methods in advance?

Don't use inheritance - use AUTOLOAD.

sub AUTOLOAD : lvalue {
    my ($method) = do { our($AUTOLOAD) =~ /(\w+$)/ };
    # added stuff
    $method = "ParentClass::$method";
    shift->$method(@_);
}

Note lvalued AUTOLOAD doesn't work (actually crashes compiler) in 5.6



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

Date: Wed, 25 Aug 2004 21:30:10 GMT
From: "Jack D" <goodcall1@hotmail.dot.com>
Subject: Re: Overriding *all* methods
Message-Id: <Cx7Xc.44081$S55.14029@clgrps12>

"Brian McCauley" <nobull@mail.com> wrote in message
news:cgir06$i2j$1@sun3.bham.ac.uk...
> Jack D wrote:
> > I want to subclass a module in order to add some code to *each and
every*
> > method inherited. Is there an easy way to override all methods of a
package
> > without knowing the names of the methods in advance?
>
> Don't use inheritance - use AUTOLOAD.
>
> sub AUTOLOAD : lvalue {
>     my ($method) = do { our($AUTOLOAD) =~ /(\w+$)/ };
>     # added stuff
>     $method = "ParentClass::$method";
>     shift->$method(@_);
> }
>
> Note lvalued AUTOLOAD doesn't work (actually crashes compiler) in 5.6
>
Ok - I had to do some O'Reilly searching on this one. I think I understand
how this works. However during my reading - I saw this in Programming Perl:

"After Perl has vainly looked through an object's class package and the
packages of its base classes to find a method, it also checks for an
AUTOLOAD routine in each package before concluding that the method can't be
found."

So now - I'm not sure if this will work because there are other constraints.
i.e. I need to "use The::ParentModule" within my module - so this means that
the methods will be found and AUTOLOAD will not be called. Is this a correct
understanding?

I will describe exactly what I am trying to do.

I am trying to create a composite Tk widget which adds line numbers to a
Tk::Text widget (or for that matter any other widget which uses Tk::Text as
a base). In order to catch all user and programmed events for changes to the
contents - I wish to call a subroutine which updates the line numbers
whenever *any* method is called which is related to Tk::Text or Tk::WhatEver
(where WhatEver is derived from Tk::Text)

Here are some constraints:

1. I must allow the name of any Tk::Text derived module to be passed.
2. This module must be "use" d within my package..

Will AUTOLOAD still work in this case?

I have already released a beta version of this called Tk::LineNumberText to
CPAN - but it actually overwrites (ugh!)the Text-based methods - something
which I do not want to do. Hackery - shame..

Jack




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

Date: Wed, 25 Aug 2004 22:02:02 GMT
From: Ala Qumsieh <notvalid@email.com>
Subject: Re: Overriding *all* methods
Message-Id: <u%7Xc.12211$iT5.273@newssvr29.news.prodigy.com>

Jack D wrote:

> So now - I'm not sure if this will work because there are other constraints.
> i.e. I need to "use The::ParentModule" within my module - so this means that
> the methods will be found and AUTOLOAD will not be called. Is this a correct
> understanding?

No. It will only look for the method in The::ParentModule if that is 
defined as a base for your module via either:

	use base qw/The::ParentModule/;
or
	our @ISA = qw/The::ParentModule/;

If you have Damian Conway's "Object-Oriented Perl", have a look at 
Chapter 3, section 3.3.3 for a similar example. If you don't, then why 
not? :)

--Ala


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

Date: 25 Aug 2004 11:43:05 -0700
From: bart@nijlen.com (Bart Van der Donck)
Subject: Own module needs loaded module from main script
Message-Id: <b5884818.0408251043.1668150c@posting.google.com>

Hello,

My main program calls this module:

  use DBI;

Then it calls another module (one of myself), using this code:

  use lib '/path/to/my/module/';
  use NameOfMyModule;

Inside NameOfModule.pm, I want to execute commands that need the
previously loaded DBI.pm. However it fails, saying that DBI.pm isn't
loaded. Which could be solved quickly by invoking DBI.pm again from
within NameOfModule.pm.

This works fine. But the waste of memory is considerable because I
have tens of own modules like NameOfMyModule that are invoked from the
main program. Most of them need DBI.

I looked at some stuff to solve this, looks as if something like
Exporter.pm is available for this kind of things. But then I would
need to load Exporter from within all my own modules. Which would be
about the same as loading DBI each time.

Any hints?

Thank you,
Bart


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

Date: Wed, 25 Aug 2004 21:07:25 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Own module needs loaded module from main script
Message-Id: <2p46jdFfieo3U1@uni-berlin.de>

Bart Van der Donck wrote:
> My main program calls this module:
> 
>   use DBI;
> 
> Then it calls another module (one of myself), using this code:
> 
>   use lib '/path/to/my/module/';
>   use NameOfMyModule;
> 
> Inside NameOfModule.pm, I want to execute commands that need the 
> previously loaded DBI.pm. However it fails, saying that DBI.pm
> isn't loaded.

Is that really what it says? I doubt it.

You need to distinguish between loading a module and importing
possible symbols into a package. You should be able to access its
functions and possible global variables by calling them with their
fully qualified names.

> Which could be solved quickly by invoking DBI.pm again from within
> NameOfModule.pm.
> 
> This works fine. But the waste of memory is considerable because I 
> have tens of own modules like NameOfMyModule that are invoked from
> the main program. Most of them need DBI.

Misconception. What happens if you include "use DBI;" in
NameOfModule.pm as well is that you import possible symbols that are
exported by default. DBI.pm is not compiled once again.

Please study "perldoc perlmod".

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl


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

Date: Wed, 25 Aug 2004 18:41:03 GMT
From: Trey Waters <news3@sidestreet.tzo.com>
Subject: Re: Parsing FileName for upload
Message-Id: <Pine.LNX.4.58.0408251356140.24578@tj.fvqrfgerrg.gmb.pbz>

On Wed, 25 Aug 2004, Tore Aursand wrote:

> On Tue, 24 Aug 2004 14:49:59 -0700, Tony McGuire wrote:
> > I've been going batty trying to figure out a routine that will detect
> > when there is a full path sent and parse the file name from that path,
> > and when there is only a file name sent.
> 
> Use the File::Basename module.  With that module, you can easily extract
> the various parts of a filename (i.e. path and the filename itself), and
> then compare it to the original string.
> 
> BTW: Why do you need to know _if_ there is a full path present?
> 

Assuming the OP is on a *nix system, just laying down the file with the 
filename given by the browser would create a file with a name similar to:

"C:\Documents and Settings\Administrator\Desktop\MyFilename.blah"

Instead of what one expect as: "MyFilename.blah"


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

Date: Wed, 25 Aug 2004 21:13:16 +0200
From: Tore Aursand <tore@aursand.no>
Subject: Re: Parsing FileName for upload
Message-Id: <pan.2004.08.25.19.13.15.616117@aursand.no>

On Wed, 25 Aug 2004 18:41:03 +0000, Trey Waters wrote:
>>> I've been going batty trying to figure out a routine that will detect
>>> when there is a full path sent and parse the file name from that path,
>>> and when there is only a file name sent.

>> Use the File::Basename module.  With that module, you can easily
>> extract the various parts of a filename (i.e. path and the filename
>> itself), and then compare it to the original string.
>> 
>> BTW: Why do you need to know _if_ there is a full path present?

> Assuming the OP is on a *nix system, just laying down the file with the
> filename given by the browser would create a file with a name similar
> to:
> 
> "C:\Documents and Settings\Administrator\Desktop\MyFilename.blah"
> 
> Instead of what one expect as: "MyFilename.blah"

I'm fully aware of that, but that wasn't my question, was it?

  "Why do you need to know _if_ there is a full path present?"

Maybe it's my English that's bad, but I think that the OP really wants a
way to extract only the filename from a full path.

My suggestion was to use File::Basename; it is easy to deal with, cross
platform, and (if I remember correctly) comes with the standard Perl
distribution.

In other words:  No need to reinvent the wheel.


-- 
Tore Aursand <tore@aursand.no>
"There are three kinds of lies: lies, damn lies, and statistics."
 (Benjamin Disraeli)


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

Date: 25 Aug 2004 14:08:40 -0700
From: tony@paradoxcommunity.com (Tony McGuire)
Subject: Re: Parsing FileName for upload
Message-Id: <f896a829.0408251308.2303dba5@posting.google.com>

Tore Aursand <tore@aursand.no> 
> BTW: Why do you need to know _if_ there is a full path present?

The examples I've found expect to see either '/' or '\'.  Then they
grab the last portion as the file name.

A user with Opera on Linux selects a file on their system, and
something like '/home/name/file.jpg' is entered into the text block of
which file to upload.

On the server, all that arrives is 'file.jpg'.

If you parse out and take the second portion based on '/' or '\', you
get a blank.  Which means the system sees only the directory you were
going to place the file in, and prevents writing to a file name that
is the directory.

I've even tried replacing the variable I'm using as the file name with
the full value received.  And this also fails; this portion could be
an error on my part but I've gone over it many times and haven't found
where I'm doing anything wrong.


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

Date: Wed, 25 Aug 2004 11:02:06 -0700
From: cmika@-takethisout-25thandclement.com
Subject: Problem with parsing
Message-Id: <up0vv1-t6e.ln1@wilbur.25thandClement.com>

Problem: Mozilla mailbox is way to big (800MBs).
Solution: Split it up.

#!/usr/bin/perl

############
# Splits Mozilla style Inboxes into smaller ones (max size = maxMailboxSize)
# use: ./splitMozilla <mailbox name>
# output: mailbox0 [mailbox1] [mailbox2] ...
############

$maxMailboxSize = 104857600;
$mailboxNumber = 0;
open OUTPUTFILE, ">mailbox$mailboxNumber";

while(<>) {
        if(/^From /) {
                if((stat(OUTPUTFILE))[7] > $maxMailboxSize) {
                        close OUTPUTFILE;
                        $mailboxNumber ++;
                        open OUTPUTFILE, ">Inbox$mailboxNumber";
                }
        }
        print OUTPUTFILE $_;
}
 
Problem: It's not working. It's creating mailbox0, writes about 1.9MBs, then
the system runs out of memory, which shouldn't happen. What I'm basically
trying to do is search for a new message, check if the outputfile is over
100MBs, if it is, create a new one, if it's not, then write the message to
outputfile. Any ideas?

-Chris Mika
cmika@-takethisout-25thandclement.com


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

Date: Wed, 25 Aug 2004 18:27:34 GMT
From: "Paul Lalli" <mritty@gmail.com>
Subject: Re: Problem with parsing
Message-Id: <qS4Xc.4254$1M3.2636@trndny01>

<cmika@-takethisout-25thandclement.com> wrote in message
news:up0vv1-t6e.ln1@wilbur.25thandClement.com...

> $maxMailboxSize = 104857600;
> $mailboxNumber = 0;
> open OUTPUTFILE, ">mailbox$mailboxNumber";
>
> while(<>) {
>         if(/^From /) {
>                 if((stat(OUTPUTFILE))[7] > $maxMailboxSize) {
>                         close OUTPUTFILE;
>                         $mailboxNumber ++;
>                         open OUTPUTFILE, ">Inbox$mailboxNumber";
>                 }
>         }
>         print OUTPUTFILE $_;
> }
>
> Problem: It's not working. It's creating mailbox0, writes about 1.9MBs,
then
> the system runs out of memory, which shouldn't happen. What I'm basically
> trying to do is search for a new message, check if the outputfile is over
> 100MBs, if it is, create a new one, if it's not, then write the message to
> outputfile. Any ideas?

The following code:

open $f, '>out.txt';
while ( <>) {
    print((stat $f)[7], "\n");
    print $f $_;
}

shows that stat will continue to report a size of 0 no matter how many
prints have been made to the file handle.  Modifying it thusly:

open $f, '>out.txt';
while (<>) {
    print((stat $f)[7], "\n");
    print $f $_;
    close $f;
    open $f, '>>out.txt';
}

shows that stat will report the new size after the file has been closed and
reopened.

I don't think this is particularly the best solution, but it is *a*
solution.

Paul Lalli





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

Date: Wed, 25 Aug 2004 18:33:20 GMT
From: "Paul Lalli" <mritty@gmail.com>
Subject: Re: Problem with parsing
Message-Id: <QX4Xc.9815$Nn2.9653@trndny05>

"Paul Lalli" <mritty@gmail.com> wrote in message
news:qS4Xc.4254$1M3.2636@trndny01...
>
> The following code:
>
> open $f, '>out.txt';
> while ( <>) {
>     print((stat $f)[7], "\n");
>     print $f $_;
> }
>
> shows that stat will continue to report a size of 0 no matter how many
> prints have been made to the file handle.  Modifying it thusly:
>
> open $f, '>out.txt';
> while (<>) {
>     print((stat $f)[7], "\n");
>     print $f $_;
>     close $f;
>     open $f, '>>out.txt';
> }
>
> shows that stat will report the new size after the file has been closed
and
> reopened.
>
> I don't think this is particularly the best solution, but it is *a*
> solution.

Whoops.  Accidentally hit 'send' before saying what I do think the better
solution is....

Instead of calling 'stat' on the file over and over, why not simply keep
track of how much you've written to the new file.  When this amount exceeds
your limit, then make the switch to a new file:

$maxMailboxSize = 104857600;
$mailboxNumber = 0;
open OUTPUTFILE, ">mailbox$mailboxNumber";

my $written = 0;
while(<>) {
        if(/^From / && $written > $maxMailboxSize) {
                close OUTPUTFILE;
                $mailboxNumber ++;
                open OUTPUTFILE, ">Inbox$mailboxNumber";
                $written = 0;
         }
         print OUTPUTFILE;
         $written += length;
 }

Paul Lalli




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

Date: Wed, 25 Aug 2004 21:21:05 +0200
From: Tore Aursand <tore@aursand.no>
Subject: Re: Problem with parsing
Message-Id: <pan.2004.08.25.19.21.04.38535@aursand.no>

On Wed, 25 Aug 2004 11:02:06 -0700, cmika wrote:
> #!/usr/bin/perl

  use strict;
  use warnings;

> $maxMailboxSize = 104857600;
> $mailboxNumber = 0;

  my $maxMailboxSize = 104_857_600;
  my $mailboxNumber  = 0;

> open OUTPUTFILE, ">mailbox$mailboxNumber";
> 
> while(<>) {
>         if(/^From /) {
>                 if((stat(OUTPUTFILE))[7] > $maxMailboxSize) {
>                         close OUTPUTFILE;
>                         $mailboxNumber ++;
>                         open OUTPUTFILE, ">Inbox$mailboxNumber";
>                 }
>         }
>         print OUTPUTFILE $_;
> }

No need to use 'stat' to check for the filesize;  It think it's better to
keep track of how much you've written to the file;

  open( OUT, '>', 'mailbox' . $mailboxNumber ) or die "$!\n";
  my $bytesWritten = 0;
  while ( <> ) {
      if ( /^From / && $bytesWritten > $maxMailboxSize ) {
          close( OUT );
          $mailboxNumber++;
          $bytesWritten = 0;
          open( OUT, '>', 'mailbox' . $mailboxNumber ) or die "$!\n";
      }
      print OUT $_;
  }
  close( OUT );

Totally untested.


-- 
Tore Aursand <tore@aursand.no>
"There are three kinds of lies: lies, damn lies, and statistics."
 (Benjamin Disraeli)


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

Date: Wed, 25 Aug 2004 21:28:07 GMT
From: "John W. Krahn" <someone@example.com>
Subject: Re: split question
Message-Id: <Hv7Xc.55685$X12.26759@edtnps84>

Ben Morrow wrote:
> Quoth "Mike Lepore" <lepor5e@bestweb.net>:
> 
>>The number of characters and fields can't be hardcoded.
>>Example:
>>$Variable = "abc def ghi jkl";
>>$Variable1 become "abc" and $Variable2  becomes "def ghi jkl"
> 
> my ($Variable1, $Variable2) = split / /, $Variable, 2;
> 
> Now, which part of perldoc -f split didn't you understand?

Are you sure that *you* understand?  split( / / ) splits on a single space 
(ASCII 32) character which is different from split( ' ' ) which splits on 
multiple whitespace characters like split( /\s+/ ) does.

$ perl -le'$_ = q/  one   two  /; print "/$_/" for split / /'
//
//
/one/
//
//
/two/
$ perl -le'$_ = q/  one   two  /; print "/$_/" for split q/ /'
/one/
/two/

With the HT character (ASCII 9):

$ perl -le'$_ = q/       one     two     /; print "/$_/" for split / /'
/       one     two     /
$ perl -le'$_ = q/       one     two     /; print "/$_/" for split q/ /'
/one/
/two/



John
-- 
use Perl;
program
fulfillment


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

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


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