[25642] in Perl-Users-Digest
Perl-Users Digest, Issue: 7884 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Mar 15 18:05:26 2005
Date: Tue, 15 Mar 2005 15:05:12 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Tue, 15 Mar 2005 Volume: 10 Number: 7884
Today's topics:
Re: [Q]: How to sort first abc, then 123? <dha@panix.com>
Re: Can this be more efficient? <notvalid@email.com>
Re: Can this be more efficient? <Colin@chaplin.me.uk>
Re: Can this be more efficient? <tadmc@augustmail.com>
Re: Can this be more efficient? <news@chaos-net.de>
Re: Can this be more efficient? <pilkowsk@informatik.uni-marburg.de>
Re: Can this be more efficient? <someone@example.com>
DBI DBM Slow - Is a ramdisk the answer? burlo.stumproot@gmail.com
Exporting Symbols via Exporter <cpryce@nospam.pryce.net>
Re: Exporting Symbols via Exporter <1usa@llenroc.ude.invalid>
Re: Exporting Symbols via Exporter <1usa@llenroc.ude.invalid>
Re: Exporting Symbols via Exporter <cpryce@nospam.pryce.net>
Re: How to make a blessable anonymous scalar ref? (Anno Siegel)
perl thumbnail automatic gallery <infos@leocharre.com>
Re: perl thumbnail automatic gallery <tadmc@augustmail.com>
Re: perl thumbnail automatic gallery <1usa@llenroc.ude.invalid>
Re: problem writing to stdin of child process <No_4@dsl.pipex.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Tue, 15 Mar 2005 20:55:52 +0000 (UTC)
From: "David H. Adler" <dha@panix.com>
Subject: Re: [Q]: How to sort first abc, then 123?
Message-Id: <slrnd3eiup.o65.dha@panix2.panix.com>
On 2005-03-12, HEB <NO.SPAM_please@BJH.net> wrote:
> I want the sorted output like: (first grouped aphabetically, then
> sorted by number)
Perhaps the Sort::Naturally module
(http://search.cpan.org/~sburke/Sort-Naturally-1.02/lib/Sort/Naturally.pm)
does what you want.
dha
--
David H. Adler - <dha@panix.com> - http://www.panix.com/~dha/
And you know, you can have all the good audio gear in the world, but
unless you make a point of asking the informant to not eat a Snickers
while he's talking, it's all for naught. - TorgoX, #perl
------------------------------
Date: Tue, 15 Mar 2005 22:15:34 GMT
From: Ala Qumsieh <notvalid@email.com>
Subject: Re: Can this be more efficient?
Message-Id: <a8JZd.10981$C47.783@newssvr14.news.prodigy.com>
Colin chaplin wrote:
> &opendir (".");
In general, it's not a good idea to use & when calling a subroutine.
This was necessary for Perl4 and before, but that is at least 10 years
old. Now, they are not needed, and using them has some side effects
which are described in perlsub.
> $DOMS{'thisdom'}=12;
>
> sub opendir
> {
> # Recursively goes through directories and process all mailsweeper files
As suggested by another poster, it's better to use File::Find in this case.
> my $dir=$_[0];
> my $name;
> opendir(DIRHANDLE, $dir) || die "Cannot opendir $dir: $!";
> foreach $name (sort readdir(DIRHANDLE))
> {
> if (($name eq '.') || ($name eq '..') || (lc($name) eq lc($currentlog)))
> {
>
>
>
> }
An empty if() body is never a nice thing to look at. I would change this to:
next if $name eq '.'
|| $name eq '..'
|| lc($name) eq lc($currentlog);
This also allows you to save one level of indentation, which can make
the code a bit more aesthetically pleasing. But that's subjective.
> else
> {
>
> # Only open the file if it looks like a MSW Logfile
> if ($name =~ /\Aopr[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]\.log/i)
[0-9] is identical to \d. And you can match a specific number of \d's by
using curly braces:
if ($name =~ /\Aopr\d{8}\.log/i) {
> {
>
> &processFile($dir . "\\" . $name)
Perl allows you to use unix-style dir hierarchy delimiters, even on
Windows. This is more readable (IMO):
processFile("$dir/$name");
> }
> if (-d $dir . "\\" . $name)
That should've been an 'elsif' .. but I don't think it will improve much
in your case.
[snip]
> open (INFILE,$filename) || die ("EK $!: $filename");
> while (<INFILE>)
> {
> push(@filetext,$_);
> }
The following is more efficient:
@filetext = <INFILE>;
But you don't need to do that ... see below.
> close (INFILE);
> foreach (@filetext)
Why not execute this loop while reading the file in the first place?
while (<INFILE>) {
> {
> if (/RCPT\sTO\:/i)
> {
> #print "[$_]\n";
>
> if (/(\<.*?\>)/)
> {
>
> $bits=$1;
> $bits =~ s/\<//g;
> $bits =~ s/\>//g;
> $bits =~ s/RCPT TO\://g;
> $bits =~ s/\s//g;
> $bits = lc ($bits);
All of the above can probably be optimized to this:
if (/RCPT TO:\s*<(.*?)\>/) {
my $bits = lc $1;
[more snips]
> sub valid_domain
> {
> #decide if this is a xxxxx doman
>
> my $dom=$_[0];
> ($tat,$dom)=split(/\@/,$dom);
> if ($dom =~ (/thisdomain\.co\.uk/)) {return 1;}
> if ($dom =~(/thatdomain.co\.uk/)) {return 1;}
> if ($dom =~(/otherdomain\.com/)) {return 1;}
If you are matching literal strings, using 'eq' is faster than a regexp:
return 1 if $dom eq 'thisdomain.co.uk'
|| $dom eq 'thatdomain.co.uk'
|| $dom eq 'otherdomain.co.uk';
> return (0);
> }
--Ala
------------------------------
Date: Tue, 15 Mar 2005 19:48:35 -0000
From: "Colin chaplin" <Colin@chaplin.me.uk>
Subject: Re: Can this be more efficient?
Message-Id: <d17e6p$6ov$1$830fa795@news.demon.co.uk>
"Ala Qumsieh" <notvalid@email.com> wrote in message
news:g4GZd.21999$OU1.21108@newssvr21.news.prodigy.com...
> Colin chaplin wrote:
> > Id post it here but I'd probably only get slated for coding
> > style >:-)
>
> What you're saying is akin to me going to a car mechanic and telling her
> "my car is broken. how do I fix it?" without me allowing her to see the
car.
>
> You won't get any useful replies like that. Plus, a little critique of
> your coding style should be a Good Thing (tm).
Ok, here goes, warts and all (I removed names to protect the guilty). The
program reads through Mailsweeper log files and spits out how many emails
for certain domains are recorded in the logs, and also a list of recipients
and number of emails they receive. The log is a trimmed version of an SMTP
communication.
Please note:
* I'm not a programmer, be gentle
* This isnt a production program
* Clever regular expressions make my head explode when I look at them a
while after writing the code
* I'm barely a techy these days
* Don't run this scissors
In answer to another post, I had the program setup so that it read line by
line, and changed it to gobble the entire file at once to see if would have
a performance impact
Thanks to all for taking the time!
&opendir (".");
$DOMS{'thisdom'}=12;
sub opendir
{
# Recursively goes through directories and process all mailsweeper files
my $dir=$_[0];
my $name;
opendir(DIRHANDLE, $dir) || die "Cannot opendir $dir: $!";
foreach $name (sort readdir(DIRHANDLE))
{
if (($name eq '.') || ($name eq '..') || (lc($name) eq lc($currentlog)))
{
}
else
{
# Only open the file if it looks like a MSW Logfile
if ($name =~ /\Aopr[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]\.log/i)
{
&processFile($dir . "\\" . $name)
}
if (-d $dir . "\\" . $name)
{
print "opening " .$dir . "\\" . $name ."\n";
&opendir($dir . "\\" . $name);
}
}
}
closedir(DIRHANDLE);
}
sub processFile
{
my $filename=$_[0];
my @filetext;
print " Opening $filename\n";
open (INFILE,$filename) || die ("EK $!: $filename");
while (<INFILE>)
{
push(@filetext,$_);
}
close (INFILE);
foreach (@filetext)
{
if (/RCPT\sTO\:/i)
{
#print "[$_]\n";
if (/(\<.*?\>)/)
{
$bits=$1;
$bits =~ s/\<//g;
$bits =~ s/\>//g;
$bits =~ s/RCPT TO\://g;
$bits =~ s/\s//g;
$bits = lc ($bits);
if (&valid_domain($bits))
{
$DOMS{$bits}++;
}
# print $bits;
}
}
# else {print "NO: $_\n";}
}
}
$DOMMER{'testdom'}=5;
open (OUTFILE,">allemails.csv") || die ("eeK:$!");
open (OUTF,">alldoms.csv") || die ("yikes:$!");
foreach $key ( sort by_this keys %DOMS )
{
($pre,$aft) = split(/\@/,$key);
$DOMMER{$aft}=$DOMMER{$aft}+1;
print OUTFILE "\"$key\",\"$DOMS{$key}\"\n";
}
foreach $key ( sort by_this keys %DOMMER )
{
print OUTF "\"$key\",\"$DOMMER{$key}\"\n";
}
sub by_this
{
($tat,$abit)=split (/\@/,$a);
($tat,$bbit)=split (/\@/,$b);
($abit cmp $bbit) || ($a cmp $b);
}
sub valid_domain
{
#decide if this is a xxxxx doman
my $dom=$_[0];
($tat,$dom)=split(/\@/,$dom);
if ($dom =~ (/thisdomain\.co\.uk/)) {return 1;}
if ($dom =~(/thatdomain.co\.uk/)) {return 1;}
if ($dom =~(/otherdomain\.com/)) {return 1;}
return (0);
}
------------------------------
Date: Tue, 15 Mar 2005 16:22:17 -0600
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: Can this be more efficient?
Message-Id: <slrnd3eo0p.7r1.tadmc@magna.augustmail.com>
Colin chaplin <Colin@chaplin.me.uk> wrote:
> Please note:
> * I'm not a programmer, be gentle
> * This isnt a production program
Then there is no need to optimize it. :-)
> * Clever regular expressions make my head explode when I look at them a
> while after writing the code
Join the club.
> sub opendir
[snip]
> if (($name eq '.') || ($name eq '..') || (lc($name) eq lc($currentlog)))
> {
>
>
>
> }
An empty block should be a red flag.
You can use next to eliminate an entire level of indent:
next if $name eq '.' or $name eq '..' or lc $name eq lc $currentlog;
> if ($name =~ /\Aopr[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]\.log/i)
if ( $name =~ /\Aopr\d{8}\.log/i)
Now we can see that is expecting 8 digit chars without getting
fingerprints on the screen counting them.
> sub processFile
[snip]
> while (<INFILE>)
> {
> push(@filetext,$_);
> }
You can replace all of that with:
@filetext = <INFILE>;
> if (/(\<.*?\>)/)
Angle brackets are not special in regexes, there is no need to backslash them:
if (/(<.*?>)/)
> $bits =~ s/\<//g;
> $bits =~ s/\>//g;
tr/// will always be faster that s///g, so:
$bits =~ tr/<>//d;
> sub by_this
That is a horridly poor choice of sub name...
> {
> ($tat,$abit)=split (/\@/,$a);
> ($tat,$bbit)=split (/\@/,$b);
> ($abit cmp $bbit) || ($a cmp $b);
> }
That can be sped up with a Schwartzian Transform, as described
in one of the sorting FAQs:
perldoc -q sort
> if ($dom =~ (/thisdomain\.co\.uk/)) {return 1;}
Pattern matching is not the Right Tool when what you want to match
is a constant string rather than a pattern:
if ( index $dom, 'thisdomain.co.uk' >= 0 ) { return 1 }
index() will be faster than m// too.
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Tue, 15 Mar 2005 21:43:18 +0100
From: Martin Kissner <news@chaos-net.de>
Subject: Re: Can this be more efficient?
Message-Id: <slrnd3ei76.n7o.news@maki.homeunix.net>
Colin chaplin wrote :
> I was being a little brief with my description, there's quite a lot of
> pattern matching going on there that I use a function for.
The regexes might be the reason, why the script takes such a long time
to run.
I am pretty new to perl and I am reading the Camel Book.
In the Chapter 5 Pattern-Matching there are examples which are said to
take years (actually millions of years) because of backtracking.
So one approach to improve the performance of your script might be the
optimization of you regxes.
HTH
Martin
--
perl -e '$S=[[73,116,114,115,31,96],[108,109,114,102,99,112],
[29,77,98,111,105,29],[100,93,95,103,97,110]];
for(0..3){for$s(0..5){print(chr($S->[$_]->[$s]+$_+1))}}'
------------------------------
Date: Tue, 15 Mar 2005 22:29:34 +0100
From: Fabian Pilkowski <pilkowsk@informatik.uni-marburg.de>
Subject: Re: Can this be more efficient?
Message-Id: <MPG.1ca1722d99a85f0d9898dc@news.individual.de>
* Colin chaplin wrote:
>=20
> Please note:
> * I'm not a programmer, be gentle
... but learning something new cannot be wrong, isn't?
> * This isnt a production program
... but speeding up a program is mostly interesting.
> * Clever regular expressions make my head explode when I look at them a
> while after writing the code
... when clever regexes would be necessary I'll took them (and write a=20
short comment aside to remember me after a while).
> * I'm barely a techy these days
> * Don't run this scissors
>=20
> In answer to another post, I had the program setup so that it read line b=
y
> line, and changed it to gobble the entire file at once to see if would ha=
ve
> a performance impact
>=20
> Thanks to all for taking the time!
>=20
> &opendir (".");
> $DOMS{'thisdom'}=3D12;
>=20
> sub opendir {
> # Recursively goes through directories and process all mailsweeper files
[...]
> }
For me it looks smarter, doing such a recursive directory traversal with=20
a standard perl module like File::Find. Your sub could look like:
use File::Find;
sub my_opendir {
my $dir =3D shift;
find( sub {
# $_ contains the current filename only, while
# $File::Find::name contains the complete pathname
if ( $_ =3D~ /opr\d{8}\.log$/i ) {
processFile( $File::Find::name )
}
}, $dir );
}
Btw, it's always a bad idea to name your own functions like perl ones.=20
Hence I named your sub =BBmy_opendir=AB instead of =BBopendir=AB.
>=20
> sub processFile {
> my $filename=3D$_[0];
> my @filetext;
> print " Opening $filename\n";
> open (INFILE,$filename) || die ("EK $!: $filename");
> while (<INFILE>){
> push(@filetext,$_);
> }
> close (INFILE);
Eh, this will read the complete file into the array @filetext. That can=20
be made easier by =BBmy @filetext =3D <INFILE>;=AB -- but that's not what s=
ome=20
others in this thread meaning with "use a while loop". See below.
> foreach (@filetext){
> if (/RCPT\sTO\:/i){
> #print "[$_]\n";
> if (/(\<.*?\>)/){
> $bits=3D$1;
> $bits =3D~ s/\<//g;
> $bits =3D~ s/\>//g;
If you don't catch those angle brackets in $1 you haven't delete them.
> $bits =3D~ s/RCPT TO\://g;
> $bits =3D~ s/\s//g;
> $bits =3D lc ($bits);
The search pattern is modified by =BBi=AB previously, i.e. case-insensitive=
.=20
Here you delete all uppercased occurrences of "RCPT TO:" but not the=20
lowercased ones. Perhaps you should do the lc() before.
> if (&valid_domain($bits)){
> $DOMS{$bits}++;
> }
> # print $bits;
> }
> }
> # else {print "NO: $_\n";}
> }
> }
Using a while loop to read in a file line by line:
sub processFile {
my $filename =3D shift;
print " Opening $filename\n";
open INFILE, $filename or die "EK $!: $filename";
while ( <INFILE> ) {
if ( /RCPT\sTO\:/i and /<(.*?)>/ ) {
my $bits =3D lc $1;
$bits =3D~ s/RCPT TO\://g;
$bits =3D~ s/\s//g;
$DOMS{$bits}++ if valid_domain($bits);
}
}
close INFILE;
}
>=20
> $DOMMER{'testdom'}=3D5;
>=20
> open (OUTFILE,">allemails.csv") || die ("eeK:$!");
> open (OUTF,">alldoms.csv") || die ("yikes:$!");
> foreach $key ( sort by_this keys %DOMS )
> {
>=20
> ($pre,$aft) =3D split(/\@/,$key);
> $DOMMER{$aft}=3D$DOMMER{$aft}+1;
> print OUTFILE "\"$key\",\"$DOMS{$key}\"\n";
Before using such backslashed quotes, learn more about perl's quoting=20
operators like q// and qq//.
print OUTFILE qq{"$key","$DOMS{$key}"\n};
>=20
> }
>=20
> foreach $key ( sort by_this keys %DOMMER )
> {
> print OUTF "\"$key\",\"$DOMMER{$key}\"\n";
>=20
> }
>=20
>=20
> sub by_this
> {
> ($tat,$abit)=3Dsplit (/\@/,$a);
> ($tat,$bbit)=3Dsplit (/\@/,$b);
> ($abit cmp $bbit) || ($a cmp $b);
> }
If a function returns values which aren't interesting this moment, just=20
throw them away:
( undef, $abit ) =3D split /@/, $a;
>=20
> sub valid_domain
> {
> #decide if this is a xxxxx doman
>=20
> my $dom=3D$_[0];
> ($tat,$dom)=3Dsplit(/\@/,$dom);
See above and throw $tat away ;-)
>=20
> if ($dom =3D~ (/thisdomain\.co\.uk/)) {return 1;}
> if ($dom =3D~(/thatdomain.co\.uk/)) {return 1;}
> if ($dom =3D~(/otherdomain\.com/)) {return 1;}
In Perl you can note if-statements behind. In cases like this I think=20
it's more readable due to fewer parenthesis ;-)
return 1 if $dom =3D~ /thisdomain\.co\.uk/;
return 1 if $dom =3D~ /thatdomain\.co\.uk/;
return 1 if $dom =3D~ /otherdomain\.com/;
You could do it with =BBor=AB also:
return 1 if $dom =3D~ /thisdomain\.co\.uk/
or $dom =3D~ /thatdomain\.co\.uk/
or $dom =3D~ /otherdomain\.com/;
I hope you like my suggestions.
regards,
fabian
------------------------------
Date: Tue, 15 Mar 2005 23:02:54 GMT
From: "John W. Krahn" <someone@example.com>
Subject: Re: Can this be more efficient?
Message-Id: <yQJZd.43912$KI2.43600@clgrps12>
Colin chaplin wrote:
> "Ala Qumsieh" <notvalid@email.com> wrote in message
> news:g4GZd.21999$OU1.21108@newssvr21.news.prodigy.com...
>
>>Colin chaplin wrote:
>>
>>>Id post it here but I'd probably only get slated for coding
>>>style >:-)
>>
>>What you're saying is akin to me going to a car mechanic and telling her
>>"my car is broken. how do I fix it?" without me allowing her to see the
>
> car.
>
>>You won't get any useful replies like that. Plus, a little critique of
>>your coding style should be a Good Thing (tm).
>
> Ok, here goes, warts and all (I removed names to protect the guilty). The
> program reads through Mailsweeper log files and spits out how many emails
> for certain domains are recorded in the logs, and also a list of recipients
> and number of emails they receive. The log is a trimmed version of an SMTP
> communication.
>
>
> Please note:
> * I'm not a programmer, be gentle
> * This isnt a production program
> * Clever regular expressions make my head explode when I look at them a
> while after writing the code
> * I'm barely a techy these days
> * Don't run this scissors
>
> In answer to another post, I had the program setup so that it read line by
> line, and changed it to gobble the entire file at once to see if would have
> a performance impact
>
> Thanks to all for taking the time!
>
> [snip code]
>
Perhaps this will work better: (UNTESTED)
use warnings;
use strict;
use File::Find;
my @valid_domains = qw(
thisdomain.co.uk
thatdomain.co.uk
otherdomain.com
);
my %DOMS = ( thisdom => 12 );
find( sub {
return unless /\Aopr\d{8}\.log\z/i
print " Opening $_\n";
open INFILE, '<', $_ or die "EK $!: $_";
while ( my $line = <INFILE> ) {
next unless $line =~ /RCPT\sTO:/i and $line =~ /</ and $line =~ />/;
for my $valid_domain ( @valid_domains ) {
if ( $line =~ /<(\S+\@\S*\Q$valid_domain\E)>/ ) {
$DOMS{ lc $1 }++;
}
}
}
close INFILE;
}, '.' );
sub by_this {
( split /\@/, $a )[ 1 ] cmp ( split /\@/, $b )[ 1 ] || $a cmp $b
}
open OUTFILE, '>', 'allemails.csv' or die "eeK:$!";
open OUTF, '>', 'alldoms.csv' or die "yikes:$!";
my %DOMMER = ( testdom => 5 );
for my $key ( sort by_this keys %DOMS ) {
$DOMMER{ ( split /\@/, $key )[ 1 ] }++;
print OUTFILE qq("$key","$DOMS{$key}"\n);
}
for my $key ( sort by_this keys %DOMMER ) {
print OUTF qq("$key","$DOMMER{$key}"\n);
}
__END__
John
--
use Perl;
program
fulfillment
------------------------------
Date: Tue, 15 Mar 2005 21:46:23 GMT
From: burlo.stumproot@gmail.com
Subject: DBI DBM Slow - Is a ramdisk the answer?
Message-Id: <umzt4inpg.fsf@notvalid.se>
Using WinXP Activestate 5.8.4
I'm using dbi:DBM to analyze some data. I read it from a file,
process it some and then put it in a dbi:DBM database. I run a couple
of sql's on it and save the result for later use.
But I'm finding it rather slow. 18 sec(*) to do 126 inserts and I'm
planning to do about 1000 inserts and then 5-10 selects when I'm
working on live data.
I moved the database to a ramdisk(**) and it took about 1,4 seconds.
(I'm not planning to save the database after each run.)
But then I got to thinking, is this the best way? Perhaps it's
possible to tell dbi:DBM not to write it's data to file, or at least
delay writing until I have *a big* chunk of data or until I
$dbh->disconnect;
Googling and reading the docs made me no wiser, and I have not had
enough guts to start reading the source yet.
So my question is this:
Can I speed up dbi:DBM without using a ramdisk?
If so how?
Or should I use somthing other than DBI:DBM for this task?
/PM
(*)
$dbh = DBI->connect('dbi:DBM:type=DB_File;mldbm=Storable');
$dbh->{f_dir} = 'tmp_db';
(**)
I'm currently using this, since I have an ancient app that
demands it's data from A:
Virtual Floppy Drive (VFD) for Windows NT platform.
http://chitchat.at.infoseek.co.jp/vmware/vfd.html
------------------------------
Date: Tue, 15 Mar 2005 14:08:38 -0600
From: cp <cpryce@nospam.pryce.net>
Subject: Exporting Symbols via Exporter
Message-Id: <150320051408389246%cpryce@nospam.pryce.net>
I'm confused as to why the sub make_percent in the following example is
not being exported.
#!/usr/bin/perl
package test;
use strict;
use Exporter;
our (@ISA, @EXPORT_OK);
@ISA = qw(Exporter);
@EXPORT_OK = qw(make_percent);
sub make_percent {
my ( $num, $div, $format ) = @_;
$format ||= "%.1f%%";
if ( defined $div ) {
# check for division by zero
return sprintf($format, 0) if $div == 0;
return sprintf($format, (($num/$div ) * 100) );
}
else {
return sprintf($format, ( $num * 100) );
}
}
1;
# this works
package qualify_it;
use test qw(make_percent);
my $per = test::make_percent(70, 150, undef);
print $per, "\n"; # prints 46.7%
# this give me an error: undefined subroutine use_it::make_percent
package use_it;
use test qw(make_percent);
my $per = make_percent(70, 150, undef); # gives error
print $per, "\n";
--
cp
------------------------------
Date: Tue, 15 Mar 2005 20:26:49 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: Exporting Symbols via Exporter
Message-Id: <Xns961A9D24D4DA0asu1cornelledu@127.0.0.1>
cp <cpryce@nospam.pryce.net> wrote in news:150320051408389246%
cpryce@nospam.pryce.net:
> I'm confused as to why the sub make_percent in the following example
> is not being exported.
But it is ...
> #!/usr/bin/perl
>
> package test;
In general, you should prefix your own modules, and avoid using generic
names such as test or config.
Here is what works for me:
D:\Home> cat My\Test.pm
package My::Test;
use strict;
use warnings;
require Exporter;
our @ISA = qw(Exporter);
our @EXPORT_OK = qw'mysub';
our $VERSION = '0.01';
sub mysub { 'mysub' }
1;
__END__
D:\Home> cat t1.pl
#! /usr/bin/perl
package MY1;
use strict;
use warnings;
use My::Test 'mysub';
print mysub;
package MY2;
use strict;
use warnings;
use My::Test 'mysub';
print mysub;
__END__
D:\Home> t1
mysubmysub
Sinan
------------------------------
Date: Tue, 15 Mar 2005 20:37:24 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: Exporting Symbols via Exporter
Message-Id: <Xns961A9EEFF4E0Dasu1cornelledu@127.0.0.1>
cp <cpryce@nospam.pryce.net> wrote in news:150320051408389246%
cpryce@nospam.pryce.net:
> I'm confused as to why the sub make_percent in the following example
> is not being exported.
Hmmm ... I am afraid my other answer might not be entirely correct.
> #!/usr/bin/perl
>
> package test;
>
> use strict;
> use Exporter;
>
> our @ISA = qw(Exporter);
> our @EXPORT_OK = qw(make_percent);
>
> sub make_percent {
...
> }
> }
...
> package qualify_it;
> use test qw(make_percent);
>
> my $per = test::make_percent(70, 150, undef);
> print $per, "\n"; # prints 46.7%
>
> # this give me an error: undefined subroutine use_it::make_percent
> package use_it;
> use test qw(make_percent);
>
> my $per = make_percent(70, 150, undef); # gives error
> print $per, "\n";
Am I correct in assuming that all these packages are in the same source
file? Further, can we assume that this source file is not called test.pm
and you have not placed such a test.pm file in one of the directories
listed in @INC?
If those assumptions are correct, please read
perldoc -f use
use Module LIST
Imports some semantics into the current package from the named
module, generally by aliasing certain subroutine or variable
names into your package. It is exactly equivalent to
BEGIN { require Module; import Module LIST; }
perldoc -f require
...
Otherwise, demands that a library file be included if it hasn't
already been included. The file is included via the do-FILE
mechanism,
That is, when you say
use test qw(make_percent);
A file called test.pm must exist somewhere in @INC.
Are you on a *nix system or Windows? If you are on Windows, then the
case insensitive file system is probably causing the standard module
Test.pm to be used.
Sinan.
------------------------------
Date: Tue, 15 Mar 2005 14:49:07 -0600
From: cp <cpryce@nospam.pryce.net>
Subject: Re: Exporting Symbols via Exporter
Message-Id: <150320051449074979%cpryce@nospam.pryce.net>
In article <Xns961A9EEFF4E0Dasu1cornelledu@127.0.0.1>, A. Sinan Unur
<1usa@llenroc.ude.invalid> wrote:
> Am I correct in assuming that all these packages are in the same source
> file? Further, can we assume that this source file is not called test.pm
> and you have not placed such a test.pm file in one of the directories
> listed in @INC?
I experienced the same behaviour when test was in its original package
My::HRIS::Common, which was in a file called Common.pm in the correct
folder structure(i.e. My/HRIS ) in a directory in @INC.
Other symbols in the same file were being exported, and I was stuck.
So, to trouble shoot, I thought I would isolate the offending code and
combine them into a single file
When reminded that my test case would never work, I tracked down the
error. Thanks for the reminder.
--
cp
------------------------------
Date: 15 Mar 2005 22:15:21 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: How to make a blessable anonymous scalar ref?
Message-Id: <d17mpp$32f$1@mamenchi.zrz.TU-Berlin.DE>
<xhoster@gmail.com> wrote in comp.lang.perl.misc:
> J Krugman <jkrugman345@yahbitoo.com> wrote:
> >
> > bless \undef, 'Foo' ===> Modification of a read-only value attempted
> > bless \\undef, 'Foo' ===> [ no error ]
> > bless \1, 'Foo' ===> Modification of a read-only value attempted
> > bless \\1, 'Foo' ===> Modification of a read-only value attempted
> >
> > Why are the behaviors for undef and 1 different?
>
> I don't know why they behave the precise way they do, but I can rationalize
> why they don't behave the same way. 1 is a constant, while undef is
> actually a function invokation, even thought it is often used as if it were
> a constant.
Ah, but being a function doesn't prevent it from returning a read-only
value, or an alias to one. Constants (as in the pragma) do that, and
so does
sub const { return $_ for 3 }
Anno
------------------------------
Date: Tue, 15 Mar 2005 20:34:42 GMT
From: Jean Paul Sartre <infos@leocharre.com>
Subject: perl thumbnail automatic gallery
Message-Id: <pan.2005.03.15.21.34.38.703201@leocharre.com>
I found a solution to my problem. This <a
href="http://b3thm00n.com/scripting/index_gallery/">perl thumbnail
automatic gallery</a> is quick and instant to install. It looks kind of
simple though. I could work on it some more, maybe change it.
JPS
------------------------------
Date: Tue, 15 Mar 2005 16:29:22 -0600
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: perl thumbnail automatic gallery
Message-Id: <slrnd3eoe2.7r1.tadmc@magna.augustmail.com>
Jean Paul Sartre <infos@leocharre.com> wrote:
> I found a solution to my problem. This <a
> href="http://b3thm00n.com/scripting/index_gallery/">perl thumbnail
> automatic gallery</a> is quick and instant to install. It looks kind of
> simple though. I could work on it some more, maybe change it.
Spammers go in the killfile.
Goodbye.
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Tue, 15 Mar 2005 20:45:11 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: perl thumbnail automatic gallery
Message-Id: <Xns961AA04211322asu1cornelledu@127.0.0.1>
Jean Paul Sartre <infos@leocharre.com> wrote in
news:pan.2005.03.15.21.34.38.703201@leocharre.com:
> I found a solution to my problem. This <a
> href="http://b3thm00n.com/scripting/index_gallery/">perl thumbnail
> automatic gallery</a> is quick and instant to install. It looks kind of
> simple though. I could work on it some more, maybe change it.
> JPS
Hi Leo:
If you want people to comment on various parts of your script, then
solicit advice openly.
If you just want to announce the availability of your script, this is not
the place to do it.
To announce it in this "stealth" manner is called spamming.
Sinan.
------------------------------
Date: Tue, 15 Mar 2005 20:50:19 +0000
From: Big and Blue <No_4@dsl.pipex.com>
Subject: Re: problem writing to stdin of child process
Message-Id: <V--dnX1WaaAa16rfRVnygg@pipex.net>
john wrote:
>
> Sorry, I spoke too soon. I'm still left with my original problem of
> how to attach the pipe to STDIN of the child. The various approaches
> I've tried either raise an invalid argument error or the child appears
> to read the number of the file descriptor rather than the data.
Hav a look at the documentation for open. It gives (or gave...) an
example of saving STDOUT and STDERR, opening somethign else on them and
then restoring the original.
Rearrange as appropriate for STDIN.
--
Just because I've written it doesn't mean that
either you or I have to believe it.
------------------------------
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 7884
***************************************