[25268] in Perl-Users-Digest
Perl-Users Digest, Issue: 7513 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sun Dec 12 18:05:33 2004
Date: Sun, 12 Dec 2004 15:05:06 -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 Sun, 12 Dec 2004 Volume: 10 Number: 7513
Today's topics:
Re: Consecutive Numbers (Jay Tilton)
Re: Consecutive Numbers <1usa@llenroc.ude.invalid>
Re: Consecutive Numbers <abigail@abigail.nl>
Re: Consecutive Numbers <perl@my-header.org>
Re: Consecutive Numbers <1usa@llenroc.ude.invalid>
Re: Consecutive Numbers <nospam-abuse@ilyaz.org>
Re: Consecutive Numbers (Anno Siegel)
Re: Fast and high-quality Web Site Creation <dha@panix.com>
Need help with constants and package names. <just@say.no>
Re: Need help with constants and package names. <postmaster@castleamber.com>
Re: Need help with constants and package names. <spamtrap@dot-app.org>
Re: Need help with constants and package names. <1usa@llenroc.ude.invalid>
Re: Need help with constants and package names. <just@say.no>
Re: Need help with constants and package names. <apeiron+usenet@coitusmentis.info>
Re: Need help with constants and package names. <1usa@llenroc.ude.invalid>
Re: Need help with constants and package names. <spamtrap@dot-app.org>
Re: Newbie questions, migrating from c++ <joe@inwap.com>
Re: Newbie questions, migrating from c++ <barbr-en_delete_@online.no.invalid>
Re: Newbie questions, migrating from c++ <matthew.garrish@sympatico.ca>
Re: Newbie questions, migrating from c++ <noreply@gunnar.cc>
rsh daemon written in perl? <joebloggs74@hotmail.com>
Re: sprintf rounding puzzlements <junk@blackwater-pacific.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Sun, 12 Dec 2004 19:22:28 GMT
From: tiltonj@erols.com (Jay Tilton)
Subject: Re: Consecutive Numbers
Message-Id: <41bc9a26.5196191@news.erols.com>
Graham Drabble <graham.drabble@lineone.net> wrote:
: I have a file that contains a list of numbers. I'm trying to process
: the file to find out how many rows start with 4 consecutive numbers
: (either ascending or decending). Currently I've got
:
: use strict;
: use warnings;
:
: open (IN, '4bell.txt') or die "Can't open IN: $!";
:
: my $runs = 0;
: while(<IN>){
: chomp;
: my $first = substr($_,0,1);
: my $asc = $first . $first+1 . $first+2 . $first+3;
: my $des = $first . $first-1 . $first-2 . $first-3;
: if (/^($asc|$des)/){
: $runs++
: }
The sample data don't reveal it, but the equal precedence of the "+" and
"." operators can create false positive matches. Try it with an element
starting with "7900".
Even after disambiguating with parentheses, e.g.
my $asc = $first . ($first+1) . ($first+2) . ($first+3);
it could still create a false positive match if an element started with,
say, "78910".
: }
: print "There were $runs runs\n";
:
: IN
: 12345867
: 23457658
: 34568765
: 43215687
: 13245678
:
: Prints
: There were 4 runs
:
: which is correct.
: However I can't help but think there must be a
: shorter solution but can't think of it. Any ideas? The file could
: contain up to 5000 lines.
How about:
if(
/^([0-6])(??{($1+1) . ($1+2) . ($1+3)})/
or
/^([3-9])(??{($1-1) . ($1-2) . ($1-3)})/
) {
$runs++;
}
or:
if(
/^([0-6])/ and substr($_,1,3) - $1 x 3 == 123
or
/^([3-9])/ and substr($_,1,3) - $1 x 3 == -123
) {
$runs++;
}
------------------------------
Date: 12 Dec 2004 21:14:57 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: Consecutive Numbers
Message-Id: <Xns95BDA54CFEEADasu1cornelledu@132.236.56.8>
anno4000@lublin.zrz.tu-berlin.de (Anno Siegel) wrote in
news:cpi4c9$6c2$1@mamenchi.zrz.TU-Berlin.DE:
> my $runs = 0;
> while ( <DATA> ) {
> my @seq = /(\d)(\d)(\d)(\d)/ or next;
I think the OP wanted to see if the first four digits in each line were
consecutive. In light of that, and because I was curious, here is another
way of doing it.
use strict;
use warnings;
my $runs = 0;
my $lines = 0;
my $t = time;
while(my $line = <DATA>) {
next unless $line =~ /^(\d\d\d\d)/;
++$lines;
my @digits = split '', $1;
if(is_arithmetic_sequence(1, @digits)
|| is_arithmetic_sequence(-1, @digits)) {
++$runs;
}
}
$t = time - $t;
print "There were $runs runs\n";
print "It took $t seconds to process $lines lines\n";
sub is_arithmetic_sequence {
my $delta = shift;
for my $d (1 .. $#_) {
return 0 unless $delta == $_[$d] - $_[$d-1];
}
return 1;
}
__DATA__
12345867
23457658
... 1_211_896 lines ...
> That should deal with a few thousand lines with no big problems on
> current hardware.
C:\Temp> perl md.pl
There were 774520 runs
It took 63 seconds to process 1211896 lines
That's about 20_000 lines per second on a 1Ghz Celeron running Win XP. Not
bad :)
--
A. Sinan Unur
1usa@llenroc.ude.invalid
(remove '.invalid' and reverse each component for email address)
------------------------------
Date: 12 Dec 2004 21:16:40 GMT
From: Abigail <abigail@abigail.nl>
Subject: Re: Consecutive Numbers
Message-Id: <slrncrpd9o.fj.abigail@alexandra.abigail.nl>
Graham Drabble (graham.drabble@lineone.net) wrote on MMMMCXXI September
MCMXCIII in <URL:news:Xns95BDB5D279355grahamdrabblelineone@ID-77355.user.dfncis.de>:
;; I have a file that contains a list of numbers. I'm trying to process
;; the file to find out how many rows start with 4 consecutive numbers
;; (either ascending or decending). Currently I've got
;;
;; use strict;
;; use warnings;
;;
;; open (IN, '4bell.txt') or die "Can't open IN: $!";
;;
;; my $runs = 0;
;; while(<IN>){
;; chomp;
;; my $first = substr($_,0,1);
;; my $asc = $first . $first+1 . $first+2 . $first+3;
;; my $des = $first . $first-1 . $first-2 . $first-3;
;; if (/^($asc|$des)/){
;; $runs++
;; }
;; }
;; print "There were $runs runs\n";
;;
;; IN
;; 12345867
;; 23457658
;; 34568765
;; 43215687
;; 13245678
;;
;; Prints
;; There were 4 runs
;;
;; which is correct. However I can't help but think there must be a
;; shorter solution but can't think of it. Any ideas? The file could
;; contain up to 5000 lines.
The solutions I've seen so far all calculate something inside the loop.
That's not as efficient as doing the work outside of the loop. One should
realise there are only 12 possible starting strings. So, I'd do something
like (untested):
my $N = 4;
my @c = map {my $n = $_; join "" => map {$n + $_} 0 .. $N} 0 .. 10 - $N + 1;
my @d = map {my $n = $_; join "" => map {$n - $_} 0 .. $N} $N - 1 .. 10;
my $r = join "|" => @c, @d;
open my $fh => '4bell.txt' or die "open: $fh\n";
while (<$fh>) {$runs ++ if /$r/}
print "There were $runs runs\n";
__END__
Alternatively, only could write the first four lines as:
my $r = join "|" => qr /0123 1234 2345 3456 4567 5678 6789
3210 4321 5432 6543 7654 8765 9876/;
Abigail
--
perl -wle'print"Êõóô áîïôèåò Ðåòì Èáãëåò"^"\x80"x24'
------------------------------
Date: Sun, 12 Dec 2004 22:15:53 +0100
From: Matija Papec <perl@my-header.org>
Subject: Re: Consecutive Numbers
Message-Id: <82dpr013pru9spcjc7jadlpnb3uttmd5pa@4ax.com>
X-Ftn-To: Graham Drabble
Graham Drabble <graham.drabble@lineone.net> wrote:
>I have a file that contains a list of numbers. I'm trying to process
>the file to find out how many rows start with 4 consecutive numbers
>(either ascending or decending). Currently I've got
#untested
use strict;
use warnings;
open (IN, '4bell.txt') or die "Can't open IN: $!";
my $runs = 0;
my $s2 = reverse my $s1 = join "", 0..9;
while (<IN>) {
my ($m) = /^(\d{4})/ or next;
$runs++ if $s1 =~ /$m/ or $s2 =~ /$m/;
}
print "There were $runs runs\n";
--
Matija
------------------------------
Date: 12 Dec 2004 21:39:35 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: Consecutive Numbers
Message-Id: <Xns95BDA979C4527asu1cornelledu@132.236.56.8>
Abigail <abigail@abigail.nl> wrote in
news:slrncrpd9o.fj.abigail@alexandra.abigail.nl:
> The solutions I've seen so far all calculate something inside the
> loop. That's not as efficient as doing the work outside of the loop.
> One should realise there are only 12 possible starting strings.
Indeed. This results in an increase from about 20_000 lines per second to
about 200_000 lines per second.
> my $r = join "|" => qr /0123 1234 2345 3456 4567 5678 6789
> 3210 4321 5432 6543 7654 8765 9876/;
ITYM qw.
Sinan.
--
A. Sinan Unur
1usa@llenroc.ude.invalid
(remove '.invalid' and reverse each component for email address)
------------------------------
Date: Sun, 12 Dec 2004 22:12:57 +0000 (UTC)
From: Ilya Zakharevich <nospam-abuse@ilyaz.org>
Subject: Re: Consecutive Numbers
Message-Id: <cpifp9$qjh$1@agate.berkeley.edu>
[A complimentary Cc of this posting was sent to
Graham Drabble
<graham.drabble@lineone.net>], who wrote in article <Xns95BDB5D279355grahamdrabblelineone@ID-77355.user.dfncis.de>:
> I have a file that contains a list of numbers. I'm trying to process
> the file to find out how many rows start with 4 consecutive digits
> (either ascending or decending). Currently I've got
I'm puzzled at all complications: what is wrong with using something like
my $four = substr $in, 0, 4;
print 'OK' if length $four == 4 and
(0 <= index '0123456789', $four or 0 <= index '9876543210', $four);
Ilya
------------------------------
Date: 12 Dec 2004 23:01:11 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Consecutive Numbers
Message-Id: <cpiijn$e08$1@mamenchi.zrz.TU-Berlin.DE>
Ilya Zakharevich <nospam-abuse@ilyaz.org> wrote in comp.lang.perl.misc:
> [A complimentary Cc of this posting was sent to
> Graham Drabble
> <graham.drabble@lineone.net>], who wrote in article
> <Xns95BDB5D279355grahamdrabblelineone@ID-77355.user.dfncis.de>:
> > I have a file that contains a list of numbers. I'm trying to process
> > the file to find out how many rows start with 4 consecutive digits
> > (either ascending or decending). Currently I've got
>
> I'm puzzled at all complications: what is wrong with using something like
>
> my $four = substr $in, 0, 4;
> print 'OK' if length $four == 4 and
> (0 <= index '0123456789', $four or 0 <= index '9876543210', $four);
There's nothing wrong, it apperars to be even a tad faster than Abigail's
regex. (Only goes to show that alternatives in a regex are relatively slow).
When I introduced a general function is_arithmetic_sequence() to solve
the problem, I didn'd have speed in mind, but, on the contrary, that
it is affordable to use a more general utility. With the original data
size of a few thousand, the difference in speed wouldn't have mattered.
Anno
------------------------------
Date: Sun, 12 Dec 2004 21:55:37 +0000 (UTC)
From: "David H. Adler" <dha@panix.com>
Subject: Re: Fast and high-quality Web Site Creation
Message-Id: <slrncrpfiq.65i.dha@panix2.panix.com>
On 2004-12-12, CNA Programming Group <cna_pgroup@yahoo.com> wrote:
>
> If you want to make a hi-quality website fast and for a reasonable
> price - than this offer is for you.
You have posted a job posting or a resume in a technical group.
Longstanding Usenet tradition dictates that such postings go into
groups with names that contain "jobs", like "misc.jobs.offered", not
technical discussion groups like the ones to which you posted.
Had you read and understood the Usenet user manual posted frequently to
"news.announce.newusers", you might have already known this. :) (If
n.a.n is quieter than it should be, the relevent FAQs are available at
http://www.faqs.org/faqs/by-newsgroup/news/news.announce.newusers.html)
Another good source of information on how Usenet functions is
news.newusers.questions (information from which is also available at
http://www.geocities.com/nnqweb/).
Please do not explain your posting by saying "but I saw other job
postings here". Just because one person jumps off a bridge, doesn't
mean everyone does. Those postings are also in error, and I've
probably already notified them as well.
If you have questions about this policy, take it up with the news
administrators in the newsgroup news.admin.misc.
http://jobs.perl.org may be of more use to you
Yours for a better usenet,
dha
--
David H. Adler - <dha@panix.com> - http://www.panix.com/~dha/
If history teaches us anything, it's that everyone will be part of the
problem, but not everyone will be part of the solution.
- Larry Wall
------------------------------
Date: Sun, 12 Dec 2004 20:01:42 GMT
From: Terry <just@say.no>
Subject: Need help with constants and package names.
Message-Id: <ca8pr0pkrtso5n6o2lanlnhscmc2mqlgk0@4ax.com>
Hello all, I'm having trouble with packages and constants. What I'd
like to have is a single .pm for all constants used by the application.
The application will have other .pm files as well, and I'd like for them
to have access to the constants as well. I thought the following would
do what I want, but fails miserably:
#### file: ./main
#!/usr/bin/perl
use warnings;
use strict;
use One::Two::Constants;
use One::Two::Obj;
print "Constant PI is: " , PI , "\n";
my $obj = Obj->new();
print "Object member PI is: " , $obj->{'pi'} , "\n";
#### file ./One/Two/Constants.pm
package One::Two::Constants;
use constant { PI => 3.1415 };
1;
#### file ./One/Two/Obj.pm
package One::Two::Obj;
use warnings;
use strict;
use One::Two::Constants;
sub new {
my $class = shift;
my $self = {
some => 'thing' ,
more => 'stuff' ,
pi => PI
};
return bless $self , $class;
}
1;
$ ./main
Bareword "PI" not allowed while "strict subs" in use at One/Two/Obj.pm
line 10.
Compilation failed in require at ./main line 7.
BEGIN failed--compilation aborted at ./main line 7.
The only way I've been able to get this to run is by removing the
package declaration from Constants.pm, changing the package declaration
of Obj.pm to just 'package Obj', and by referring to 'PI' inside of
Obj.pm as 'main::PI'. Clearly this is a poor approach.
All I want is all constants kept in one place, able to be referred to
from anywhere else. Any suggestions?
As a side note, aren't package names supposed to be fully qualified?
i.e, aren't .pm file supposed to begin with something like:
package One::Two::Something;
Importing with 'use' only seems to work when packages are declared with
only their basename:
package Something;
Thank you for your help.
-Terry
------------------------------
Date: 12 Dec 2004 20:05:00 GMT
From: John Bokma <postmaster@castleamber.com>
Subject: Re: Need help with constants and package names.
Message-Id: <Xns95BD8F432D7A9castleamber@130.133.1.4>
Terry wrote:
> All I want is all constants kept in one place, able to be referred to
> from anywhere else. Any suggestions?
Just don't do it that way, try to use namespaces (packages), e.g
Math::PI
etc.
--
John Small Perl scripts: http://johnbokma.com/perl/
Perl programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html
------------------------------
Date: Sun, 12 Dec 2004 15:20:03 -0500
From: Sherm Pendley <spamtrap@dot-app.org>
Subject: Re: Need help with constants and package names.
Message-Id: <ko-dnQhE-bdvOiHcRVn-hw@adelphia.com>
Terry wrote:
> Hello all, I'm having trouble with packages and constants. What I'd
> like to have is a single .pm for all constants used by the application.
> The application will have other .pm files as well, and I'd like for them
> to have access to the constants as well. I thought the following would
> do what I want, but fails miserably:
... snip ...
> #### file ./One/Two/Constants.pm
> package One::Two::Constants;
>
> use constant { PI => 3.1415 };
>
> 1;
Long answer, read:
perldoc Exporter
perldoc perlmod
perldoc perlmodlib
Short answer, to export symbols into the caller's package, inherit from
Exporter and declare them in @EXPORT:
package One::Two::Constants;
use strict;
use warnings;
use Exporter;
our @ISA = qw(Exporter);
our @EXPORT = qw(PI);
use constant PI => 3.1415;
1;
But *do* read the long answer. The code above is enough to get you
started, but there's definitely a lot more to learn than this simple
example shows.
> As a side note, aren't package names supposed to be fully qualified?
> i.e, aren't .pm file supposed to begin with something like:
>
> package One::Two::Something;
That is correct.
> Importing with 'use' only seems to work when packages are declared with
> only their basename:
>
> package Something;
No, use works just fine with nested module names. If it's not working
for you, post a short (but complete) script that demonstrates the problem.
sherm--
--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
------------------------------
Date: 12 Dec 2004 20:40:21 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: Need help with constants and package names.
Message-Id: <Xns95BD9F6EF2C8Fasu1cornelledu@132.236.56.8>
Terry <just@say.no> wrote in news:ca8pr0pkrtso5n6o2lanlnhscmc2mqlgk0@
4ax.com:
> Hello all, I'm having trouble with packages and constants.
In addition to Sherm and John's excellent responses, let me just point
out that you can save yourself a lot of trouble by using h2xs. While its
official description states:
DESCRIPTION
*h2xs* builds a Perl extension from C header files. The extension
will include functions which can be used to retrieve the value of
any #define statement which was in the C header files.
it can also be used to generate nice skeleton files for your modules.
h2xs -AXn One::Two::Constants
See
perldoc h2xs
for more information.
Sinan
--
A. Sinan Unur
1usa@llenroc.ude.invalid
(remove '.invalid' and reverse each component for email address)
------------------------------
Date: Sun, 12 Dec 2004 21:17:22 GMT
From: Terry <just@say.no>
Subject: Re: Need help with constants and package names.
Message-Id: <cccpr050hr6m10tbue11ja9iqmhjooub3g@4ax.com>
On Sun, 12 Dec 2004 15:20:03 -0500, Sherm Pendley <spamtrap@dot-app.org>
wrote:
>Long answer, read:
>
>perldoc Exporter
>perldoc perlmod
>perldoc perlmodlib
Thanks for the lead! After reading the Exporter perldoc I see my
blunder.
>Short answer, to export symbols into the caller's package, inherit from
>Exporter and declare them in @EXPORT:
>
>package One::Two::Constants;
>
>use strict;
>use warnings;
>
>use Exporter;
>our @ISA = qw(Exporter);
>our @EXPORT = qw(PI);
>
>use constant PI => 3.1415;
>
>1;
Something I didn't find in the perldoc was how to export a hash using
the 'use constant' pragma. The examples seemed to be geared toward
single imports as in your example. The reason I'm using 'use export'
with a hash ref is to import multiple constants (I'll have at least
fifty by the time I'm done). ie:
use constant {
one => 1 ,
two => 2 ,
# ...
};
How then to pass this list to @EXPORT ? I tried assigning the hash ref
to a scalar first and assigning its keys into @ISA , but you probably
already know that didn't work ;) Any suggestions?
>> As a side note, aren't package names supposed to be fully qualified?
>> i.e, aren't .pm file supposed to begin with something like:
>>
>> package One::Two::Something;
>
>That is correct.
>
>> Importing with 'use' only seems to work when packages are declared with
>> only their basename:
>>
>> package Something;
>
>No, use works just fine with nested module names. If it's not working
>for you, post a short (but complete) script that demonstrates the problem.
>
The Obj.pm file in my last post demonstrates the problem:
##### file: ./One/Two/Obj.pm
package One::Two::Obj;
use warnings;
use strict;
use One::Two::Constants;
sub new {
my $class = shift;
my $self = {
some => 'thing' ,
more => 'stuff' ,
pi => PI
};
return bless $self , $class;
}
1;
###### file ./main
#!/usr/bin/perl
use warnings;
use strict;
use One::Two::Constants;
use One::Two::Obj;
my $obj = Obj->new();
$ ./main
Can't locate object method "new" via package "Obj" (perhaps you forgot
to load "Obj"?) at ./main line 10.
If I change the package declaration in Obj.pm from 'One::Two::Obj' to
just 'Obj' it works fine. I also tried exporting 'new' from Obj.pm as
in your example above to no avail.
>sherm--
Thanks again for your help Sherm!
------------------------------
Date: 12 Dec 2004 21:36:29 GMT
From: Christopher Nehren <apeiron+usenet@coitusmentis.info>
Subject: Re: Need help with constants and package names.
Message-Id: <slrncrpeet.2dsb.apeiron+usenet@prophecy.dyndns.org>
On 2004-12-12, A. Sinan Unur scribbled these
curious markings:
> In addition to Sherm and John's excellent responses, let me just point
> out that you can save yourself a lot of trouble by using h2xs. While its
> official description states:
[...]
> See
>
> perldoc h2xs
In further addition, I'd like to suggest Module::Starter (and its
command-line frontend, module-starter) as a modern alternative /
complement to h2xs. It presents a friendlier interface to module
skeleton creation, and isn't historically (read: confusingly, for the
new user anyway) named.
You can find M::S on your local CPAN mirror.
Best Regards,
Christopher Nehren
--
I abhor a system designed for the "user", if that word is a coded
pejorative meaning "stupid and unsophisticated". -- Ken Thompson
If you ask the wrong questions, you get answers like "42" and "God".
Unix is user friendly. However, it isn't idiot friendly.
------------------------------
Date: 12 Dec 2004 21:40:32 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: Need help with constants and package names.
Message-Id: <Xns95BDA9A2EC677asu1cornelledu@132.236.56.8>
Christopher Nehren <apeiron+usenet@coitusmentis.info> wrote in
news:slrncrpeet.2dsb.apeiron+usenet@prophecy.dyndns.org:
> In further addition, I'd like to suggest Module::Starter (and its
> command-line frontend, module-starter) as a modern alternative /
> complement to h2xs.
I did not know about this module. Thanks very much.
Sinan.
--
A. Sinan Unur
1usa@llenroc.ude.invalid
(remove '.invalid' and reverse each component for email address)
------------------------------
Date: Sun, 12 Dec 2004 17:24:16 -0500
From: Sherm Pendley <spamtrap@dot-app.org>
Subject: Re: Need help with constants and package names.
Message-Id: <8YydnW7N7PiNWCHcRVn-iA@adelphia.com>
Terry wrote:
> Something I didn't find in the perldoc was how to export a hash using
> the 'use constant' pragma. The examples seemed to be geared toward
> single imports as in your example. The reason I'm using 'use export'
> with a hash ref is to import multiple constants (I'll have at least
> fifty by the time I'm done). ie:
>
> use constant {
> one => 1 ,
> two => 2 ,
> # ...
> };
>
> How then to pass this list to @EXPORT ? I tried assigning the hash ref
> to a scalar first and assigning its keys into @ISA , but you probably
> already know that didn't work ;) Any suggestions?
I haven't found a way to do that either - and believe me I've tried. In
my CamelBones project I have one file with over a hundred constants -
each one appears twice, once to define its value and once in @EXPORT.
> The Obj.pm file in my last post demonstrates the problem:
>
> ##### file: ./One/Two/Obj.pm
> package One::Two::Obj;
>
> use warnings;
> use strict;
>
> use One::Two::Constants;
>
> sub new {
... snip ...
> ###### file ./main
> #!/usr/bin/perl
>
> use warnings;
> use strict;
>
> use One::Two::Constants;
> use One::Two::Obj;
>
> my $obj = Obj->new();
>
>
> $ ./main
> Can't locate object method "new" via package "Obj" (perhaps you forgot
> to load "Obj"?) at ./main line 10.
To call a class method like new(), you need to specify the full class
name, not just the last component of it:
my $obj = One::Two::Obj->new();
Objects know what package they belong to though, so instance methods can
be called with the name of the method alone:
$obj->do_something();
> If I change the package declaration in Obj.pm from 'One::Two::Obj' to
> just 'Obj' it works fine.
In that case, you're not using Exporter in One::Two::Obj. If you were,
it wouldn't work as expected because the file name doesn't match the
package name.
use One::Two::Obj;
is roughly equivalent to
BEGIN {
require "One/Two/Obj.pm";
One::Two::Obj::import();
}
But if the code in One/Two/Obj.pm is actually in package Obj, there
would be no One::Two::Obj::import() to call. So the symbols in the
module wouldn't be correctly imported into the calling package.
> I also tried exporting 'new' from Obj.pm as
You only need to export non-oo functions. Class methods are normally
called using the fully-qualified class name as above. Instance methods
are called via an object reference, and the class of the object is used
to determine the package to find the method in.
sherm--
--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
------------------------------
Date: Sun, 12 Dec 2004 19:08:23 GMT
From: Joe Smith <joe@inwap.com>
Subject: Re: Newbie questions, migrating from c++
Message-Id: <HG0vd.748491$8_6.231174@attbi_s04>
gg500@lycos.com wrote:
> Just to correct myself, my space counting code does not work. But I
> tried this and it did work:
> $i=1;while (/^ {$i}/) {++$i;}--$i;
> But this didn't work:
> $i=0;while (/^ {$i+1}/) {++$i;}
> which needless to say I do not understand.
That's not an appropriate use of {} in a regular expression.
Use parentheses inside the regex to pick up a desired substring:
$i = /(^ *)/ ? length $1 : 0;
-Joe
------------------------------
Date: Sun, 12 Dec 2004 20:12:06 +0100
From: Kåre Olai Lindbach <barbr-en_delete_@online.no.invalid>
Subject: Re: Newbie questions, migrating from c++
Message-Id: <kg4pr0pigorafeh2fcgl2rfa7bdlmsibhm@4ax.com>
On 12 Dec 2004 10:41:31 -0800, gg500@lycos.com wrote:
>Thanks, that seems is the correct way. However I get those warnings
>which I have mentioned and I have to add the weird condition not to
>receive warning:
>
>/^(\s+)/;
>$i=defined ($1) ? length($1) : 0;
>
>If I write:
>
>/^(\s+)/;
>$i=length($1);
>
>I get "Use of uninitialized value in length..."
That's why I put the regex-match in an if-statement, if it doesn't
match, 'number of whitespaces first' are considered zero, or whatever
you are trying to achieve.
I would not trust $n values unless matched in the regex. If you later
loop through several lines you want to count for starting spaces, you
will get last line's value if current line has no starting spaces,
unless you deliberately set a counter to 0, or use an if-statement.
If things aren't working correctly with 'use warnings;' and 'use
strict;', it is because something is programmed wrong...
Your way is one way to make a printable output when $1 is not set
(un-initialized).
Another way:
# Untested!
use strict;
use warnings;
my $string = ' string with whitespaces first';
my $start_space_counter = 0;
if($string =~ /^(\s+)/) {
$start_space_counter = length($1);
}
print "Number of whitespaces first: $start_space_counter\n";
Yet Another way:
# Untested!
use strict;
use warnings;
my $string = ' string with whitespaces first';
if($string =~ /^(\s+)/) {
print 'Number of whitespaces first:', length($1), "\n";
}
else
{
print "No whitespaces first\n";
}
It is up to you to decide what to achieve!
--
mvh/Regards Kåre Olai Lindbach
(News: Remove '_delete_' and '.invalid')
(HTML-written email from unknown will be discarded)
------------------------------
Date: Sun, 12 Dec 2004 13:58:33 -0500
From: "Matt Garrish" <matthew.garrish@sympatico.ca>
Subject: Re: Newbie questions, migrating from c++
Message-Id: <qx0vd.1454$pb.80356@news20.bellglobal.com>
<gg500@lycos.com> wrote in message
news:1102876890.974390.30480@c13g2000cwb.googlegroups.com...
> Thanks, that seems is the correct way. However I get those warnings
> which I have mentioned and I have to add the weird condition not to
> receive warning:
>
> /^(\s+)/;
> $i=defined ($1) ? length($1) : 0;
>
> If I write:
>
> /^(\s+)/;
> $i=length($1);
>
> I get "Use of uninitialized value in length..."
>
They're called warnings because that's all they are. If the string has no
spaces at the beginning of it, nothing is assigned to $1 and so you get a
warning that you're calling the length function on an undefined value. If it
really bothers you, turn off the warnings for that part of your code. Most
people would do something like the following if it really matters if $1 gets
assigned to:
my ($cnt) = ($string =~ /^(\s+)/);
if ($cnt) { ... }
That way you don't execute code you don't want executed.
Matt
------------------------------
Date: Sun, 12 Dec 2004 20:08:43 +0100
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Newbie questions, migrating from c++
Message-Id: <323jroF3hnvq2U1@individual.net>
Sherm Pendley wrote:
> In your example, you tried to read the environment variable
> REMOTE_ADDR. That implies that you're writing a CGI script and you
> want the remote host address. In that case, the normal way to get it
> is to call the remote_host() method in CGI.pm.
remote_host() is not equivalent to $ENV{REMOTE_ADDR}, so that method is
reasonably not "normal" if you are after the REMOTE_ADDR env. variable.
Besides, if it had been a method that only returned an environment
variable, personally I would have found it difficult to see the point
with it.
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
------------------------------
Date: Sun, 12 Dec 2004 22:14:58 GMT
From: "Joe Bloggs" <joebloggs74@hotmail.com>
Subject: rsh daemon written in perl?
Message-Id: <Cp3vd.4031$Z3.2673@newsfe3-win.ntli.net>
Wondering if anyone has already written a rsh daemon in Perl?
Have found rsh client, but no rsh daemon server element.
http://cpan.uwinnipeg.ca/dist/Net-Rsh
Thanks,
Joe.
------------------------------
Date: Sun, 12 Dec 2004 12:04:29 -0800
From: Steve May <junk@blackwater-pacific.com>
Subject: Re: sprintf rounding puzzlements
Message-Id: <10rp8qok81jrb3a@corp.supernews.com>
Sherm Pendley wrote:
> Joe Smith wrote:
>
>> When would you use '-w' for new code?
>
>
> When you're writing it for an old Perl. :-)
>
Exactly....
I have to deal with several servers that are still running < 5.6,
so I still tend to use -w as a matter of habit unless I *know*
the code is only intended for machines with newer Perl.
Yes, I know they should be upgraded, but when you have web-servers
running a few hundred virtuals with non-stock perl modules loaded,
the old saw about "if it ain't broken, don't fix it" gets pretty
important. Most of the servers *are* up to 5.8+ and over the next
several months, the older machines will be retired, but in the
meantime...
\s
------------------------------
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 7513
***************************************