[25186] in Perl-Users-Digest
Perl-Users Digest, Issue: 7435 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sun Nov 21 21:05:48 2004
Date: Sun, 21 Nov 2004 18:05:04 -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, 21 Nov 2004 Volume: 10 Number: 7435
Today's topics:
Re: Copying a folder <james@emarch.net>
Re: Copying a folder <1usa@llenroc.ude.invalid>
Re: Dat files help <james@emarch.net>
error from pipe in sendmail alias <ata@gotimeevents.com>
FAQ 2.0: How can I convince my sysadmin/supervisor/empl <comdog@panix.com>
I found this great little site (I found this great little site)
Re: Perl Test - Any examples on web <postmaster@castleamber.com>
Real world examples <chlo.prog@gmail.com>
Re: Real world examples <1usa@llenroc.ude.invalid>
Re: Real world examples <postmaster@castleamber.com>
Re: Real world examples <amead@comcast.net>
Searching backward (Evan)
Re: Searching backward <see@sig.invalid>
Re: Searching backward <james@emarch.net>
where to look for object method? (Piet)
Re: where to look for object method? <spamtrap@dot-app.org>
Re: where to look for object method? <see@sig.invalid>
Re: where to look for object method? (Jay Tilton)
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Sun, 21 Nov 2004 22:34:00 GMT
From: "James Tolley" <james@emarch.net>
Subject: Re: Copying a folder
Message-Id: <sJ8od.4022$K36.927@trndny03>
"Jim" <garvey_jim@yahoo.com> wrote in message
news:3327e074.0411202046.6c90398e@posting.google.com...
> Hi,
>
> My company has an office in Europe. I oftem have to download
> files/directories from a server there. I'd like to write a script
> that will download these dirs automaticaly for me. I can access the
> files via a browser (it is a windows view, has "parent direcoty" link,
> etc), I right click the directory, copy it, and then paste the dir to
> my hard drive.
use LWP::Simple;
mirror('http://www.whereever.com/etc/etc.html','C:/whereever/file.html');
------------------------------
Date: 22 Nov 2004 00:26:19 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: Copying a folder
Message-Id: <Xns95A8C5BE88C1Casu1cornelledu@132.236.56.8>
garvey_jim@yahoo.com (Jim) wrote in news:3327e074.0411211048.3df2782
@posting.google.com:
> Thanks for the help.
>
> I can only access the information viz http.
^^^ via
Why did you not say so in your first post?
> wget is not supported in the dos cmd window.
Are you really running in DOS or are you referring to the cmd.exe shell
in a NT/2K/XP environment. Your statement is false in both environments:
http://www.google.com/search?sourceid=mozclient&ie=utf-8&oe=utf-8
&q=wget+windows
http://www.rahul.net/dkaufman/
It is now fully established that your question is not Perl related.
If you would like to write a wget work-alike in Perl, check out LWP. Once
you have some code, post questions related specifically to your code.
--
A. Sinan Unur
1usa@llenroc.ude.invalid
(remove '.invalid' and reverse each component for email address)
------------------------------
Date: Sun, 21 Nov 2004 22:43:48 GMT
From: "James Tolley" <james@emarch.net>
Subject: Re: Dat files help
Message-Id: <ES8od.4815$hJ6.1382@trndny01>
<hope@hope.com> wrote in message
news:hi01q0he2cphu125d6b46pvjqcfs7tbj8v@4ax.com...
> Hi
>
> 1....I have a load of dat files in a folder called users this is in the
cgi folder like
>
> c:\apache\apache2\cgi-bin\data\users\1234.dat 5678.dat etc
>
> 2.....Inside each of these dat files I have information like
> password
> email
> FullName
> Street Address
> City
> Country
> Zip
> Phone
> 3.....Now what I want to do is fetch the above info out of the dat files
and do one of two things
>
> 1......Put in a excel l file OR
> 2......Make a text/csv file with information all on one line with
it separated by a "," I then can import that into a excel my self
>
Something LIKE this should work:
use Text::CSV;
my $csv = Text::CSV->new;
open my $out, '+>/home/me/outfile' or die $!;
opendir my $dh, '/usr/local/whatever' or die $!; # open the dir
my @files = grep /\.dat$/, readdir $dh; # only get dat files
for my $file (@files) {
open my $fh, "$dir/$file" or die $!;
read $fh, my $data, -s $fh;
my @fields = split /\n/, $data;
$csv->combine(@fields);
print $out $csv->string."\n";
}
__END__
hth
------------------------------
Date: 21 Nov 2004 15:25:58 -0800
From: "nickname" <ata@gotimeevents.com>
Subject: error from pipe in sendmail alias
Message-Id: <1101079558.798685.68190@c13g2000cwb.googlegroups.com>
Hello,
I posted this on a sendmail group, to no avail. Any suggestions about
tightening up the code or making it 'nicer' is appreciated as well.
I have an alias consisting of a pipe to a perl script and a username as
well,
web1: "| /home/sellis/bin/email-filter.pl",robert
I am getting the desired results from this; a copy of the email goes to
robert's mailbox, and a copy is processed by email-filter.pl. However
I'm seeing errors in my maillog and messages as follows,
maillog, stat=unknown mailer error 137
and messages, /kernel: pid 94738 (perl), uid 26, was killed: out of
swap space
I'm wondering how to track this down, or what I could be doing wrong.
If I run the script from the command line it terminates right away; I
specifically added a line to tell the script to exit.
I am using sendmail version 8.12.11,
thanks for any suggestions,
ta, Sean
code follows,
#!/usr/bin/perl -w
use strict;
use Email::Filter;
use DBI;
my
($first_name,$last_name,$name,$fname,$lname,$address_1,$city,$state,$zip,$country,$phone_1,$user_email);
my $user_id = `/usr/home/sellis/bin/make-user_id.php`;
my $address_type = "BT";
my $default_last_name = "NONE";
my $timestamp = time;
my $vendor_id = "1";
my $shopper_group_id = "15";
my $password = "XXXXXX";
my $perms = "shopper";
my $dsn = "DBI:mysql:some_db";
my $db_user = "db_user";
my $db_pass = "XXXXXX";
my $mail = Email::Filter->new(emergency =>
"/usr/home/sellis/mail/webforms");
exit 0 if $mail->subject !~ /FORM POST/;
my $conn = DBI->connect($dsn, $db_user, $db_pass) or die $DBI::errstr;
my $body = $mail->body;
my @responses = split "\n", $body;
foreach my $line (@responses) {
next unless defined $line;
if ($line =~ /^Fname\s+: (.*)$/) {
# print "$1\n";
$fname = $1;
}
if ($line =~ /^Lname\s+: (.*)$/) {
# print "$1\n";
$lname = $1;
}
MORE ASSIGNMENT STUFF LIKE ABOVE SNIPPED FOR BREVITY
}
$first_name = $fname || $name;
$last_name = $lname || $default_last_name;
doSQL($conn, "INSERT INTO user_info (user_id, address_type, last_name,
first_name, phone_1, address_1, city, state, country, zip, user_email,
cdate, mdate) VALUES
('$user_id','$address_type','$last_name','$first_name','$phone_1','$address_1','$city','$state','$country','$zip','$user_email','$timestamp','$timestamp')");
doSQL($conn, "INSERT INTO shopper_vendor_xref (user_id, vendor_id,
shopper_group_id) VALUES
('$user_id','$vendor_id','$shopper_group_id')");
doSQL($conn, "INSERT INTO auth_user_md5 (user_id, username, password,
perms) VALUES ('$user_id','$timestamp','$password','$perms')");
$conn->disconnect;
sub doSQL {
my ($conn, $command) = @_;
# print $command, "\n";
my $sth = $conn->prepare($command);
my $nrows = $sth->execute;
# print "status is ", $DBI::err, "\n" if $DBI::err;
# print "number of rows affected is ", $nrows, "\n";
# print "error message: ", $DBI::errstr, "\n" if $DBI::err;
}
exit 0;
------------------------------
Date: Sun, 21 Nov 2004 23:03:02 +0000 (UTC)
From: PerlFAQ Server <comdog@panix.com>
Subject: FAQ 2.0: How can I convince my sysadmin/supervisor/employees to use version 5/5.6.1/Perl instead of some other language?
Message-Id: <cnr6r6$at2$1@reader1.panix.com>
This message is one of several periodic postings to comp.lang.perl.misc
intended to make it easier for perl programmers to find answers to
common questions. The core of this message represents an excerpt
from the documentation provided with Perl.
--------------------------------------------------------------------
2.0: How can I convince my sysadmin/supervisor/employees to use version 5/5.6.1/Perl instead of some other language?
If your manager or employees are wary of unsupported software, or
software which doesn't officially ship with your operating system, you
might try to appeal to their self-interest. If programmers can be more
productive using and utilizing Perl constructs, functionality,
simplicity, and power, then the typical manager/supervisor/employee may
be persuaded. Regarding using Perl in general, it's also sometimes
helpful to point out that delivery times may be reduced using Perl
compared to other languages.
If you have a project which has a bottleneck, especially in terms of
translation or testing, Perl almost certainly will provide a viable,
quick solution. In conjunction with any persuasion effort, you should
not fail to point out that Perl is used, quite extensively, and with
extremely reliable and valuable results, at many large computer software
and hardware companies throughout the world. In fact, many Unix vendors
now ship Perl by default. Support is usually just a news-posting away,
if you can't find the answer in the *comprehensive* documentation,
including this FAQ.
See http://www.perl.org/advocacy/ for more information.
If you face reluctance to upgrading from an older version of perl, then
point out that version 4 is utterly unmaintained and unsupported by the
Perl Development Team. Another big sell for Perl5 is the large number of
modules and extensions which greatly reduce development time for any
given task. Also mention that the difference between version 4 and
version 5 of Perl is like the difference between awk and C++. (Well, OK,
maybe it's not quite that distinct, but you get the idea.) If you want
support and a reasonable guarantee that what you're developing will
continue to work in the future, then you have to run the supported
version. As of December 2003 that means running either 5.8.2 (released
in November 2003), or one of the older releases like 5.6.2 (also
released in November 2003; a maintenance release to let perl 5.6 compile
on newer systems as 5.6.1 was released in April 2001) or 5.005_03
(released in March 1999), although 5.004_05 isn't that bad if you
absolutely need such an old version (released in April 1999) for
stability reasons. Anything older than 5.004_05 shouldn't be used.
Of particular note is the massive bug hunt for buffer overflow problems
that went into the 5.004 release. All releases prior to that, including
perl4, are considered insecure and should be upgraded as soon as
possible.
In August 2000 in all Linux distributions a new security problem was
found in the optional 'suidperl' (not built or installed by default) in
all the Perl branches 5.6, 5.005, and 5.004, see
http://www.cpan.org/src/5.0/sperl-2000-08-05/ Perl maintenance releases
5.6.1 and 5.8.0 have this security hole closed. Most, if not all, Linux
distribution have patches for this vulnerability available, see
http://www.linuxsecurity.com/advisories/ , but the most recommendable
way is to upgrade to at least Perl 5.6.1.
--------------------------------------------------------------------
Documents such as this have been called "Answers to Frequently
Asked Questions" or FAQ for short. They represent an important
part of the Usenet tradition. They serve to reduce the volume of
redundant traffic on a news group by providing quality answers to
questions that keep coming up.
If you are some how irritated by seeing these postings you are free
to ignore them or add the sender to your killfile. If you find
errors or other problems with these postings please send corrections
or comments to the posting email address or to the maintainers as
directed in the perlfaq manual page.
Note that the FAQ text posted by this server may have been modified
from that distributed in the stable Perl release. It may have been
edited to reflect the additions, changes and corrections provided
by respondents, reviewers, and critics to previous postings of
these FAQ. Complete text of these FAQ are available on request.
The perlfaq manual page contains the following copyright notice.
AUTHOR AND COPYRIGHT
Copyright (c) 1997-2002 Tom Christiansen and Nathan
Torkington, and other contributors as noted. All rights
reserved.
This posting is provided in the hope that it will be useful but
does not represent a commitment or contract of any kind on the part
of the contributers, authors or their agents.
------------------------------
Date: Sun, 21 Nov 04 15:26:00 GMT
From: lisawill4u@yahoo.com(I found this great little site)
Subject: I found this great little site
Message-Id: <04112115260047765@newshost.allthenewsgroups.com>
I found this great little site. I signed up two weeks ago and got 2 Disney tickets and this week they are sending me 2 Universal Studios tickets. Here's the link http://66.219.102.40/ and by the way I am a real person, this is my real email address. I'm not a spammer and didn't appreciate the nasty email I got last time I tried to post this link.
Thanks, Lisa
http://66.219.102.40/
http://66.219.102.40/
------------------------------
Date: 21 Nov 2004 20:22:30 GMT
From: John Bokma <postmaster@castleamber.com>
Subject: Re: Perl Test - Any examples on web
Message-Id: <Xns95A89239A9D41castleamber@130.133.1.4>
Stephen Adam wrote:
> Hi there,
>
> I've got an interview for a junior web developer role and they will be
> testing my Perl skills in the interview. I was just wondering if there
> was an online resource for example tests of varying levels for Perl
> and any other languages.
Download some hot CGI scripts written in Perl and see if you [a] can
understand what they are doing and [b] if you can find at least 20 huge
mistakes in them.
--
John Small Perl scripts: http://johnbokma.com/perl/
Perl programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html
------------------------------
Date: 21 Nov 2004 14:29:43 -0800
From: "rc" <chlo.prog@gmail.com>
Subject: Real world examples
Message-Id: <1101076183.352311.218570@f14g2000cwb.googlegroups.com>
Hi, I would like some help in finding some "real world" Perl
application examples. Everything I'm finding are short programs
without proper logging and error handling and only accomplish a simple
task.
Here's what I'm trying to accomplish for my first application:
1. Write a singleton class/module that will create/hold a file handle
to do logging (I found a singleton example in an old "ThePerlReview").
This way all the other modules I write can simply call methods this
object to do logging. -- Is this a good idea, how is it done in the
real world so all modules can write to the same log?
2. Write a class/module that will read a configuration file in a
generic way (so like the logging class, I can reuse it for other
projects). This way I can call get/set... methods, I've seen a great
generic example in "Object Oriented Perl" for the get/set part. -- I
want to do this instead of sourcing in a hash so it can check that
valid methods are being called.
3. Write a wrapper around Net::FTP that will catch exceptions and
retry login/put/get if time outs occur. This class will use the
logging singleton class to write to the same filehandle that the main
application and all other modules are witting too.
4. The actual application will use these modules and setup the logging
singleton.
Any help in pointing resources that do any of these specific things
and especially an "application" that does things similar to this would
be greatly appreciated.
Thanks
------------------------------
Date: 22 Nov 2004 00:20:40 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: Real world examples
Message-Id: <Xns95A8C4C9A92A9asu1cornelledu@132.236.56.8>
"rc" <chlo.prog@gmail.com> wrote in news:1101076183.352311.218570
@f14g2000cwb.googlegroups.com:
> 1. Write a singleton class/module that will create/hold a file handle
> to do logging (I found a singleton example in an old "ThePerlReview").
> This way all the other modules I write can simply call methods this
> object to do logging. -- Is this a good idea, how is it done in the
> real world so all modules can write to the same log?
http://search.cpan.org/~mschilli/Log-Log4perl-0.49/lib/Log/Log4perl.pm
among others
> 2. Write a class/module that will read a configuration file in a
> generic way (so like the logging class, I can reuse it for other
> projects). This way I can call get/set... methods, I've seen a great
> generic example in "Object Oriented Perl" for the get/set part. -- I
> want to do this instead of sourcing in a hash so it can check that
> valid methods are being called.
http://search.cpan.org/~kane/Config-Auto-0.11/lib/Config/Auto.pm among
others
> 3. Write a wrapper around Net::FTP that will catch exceptions and
> retry login/put/get if time outs occur. This class will use the
> logging singleton class to write to the same filehandle that the main
> application and all other modules are witting too.
If you so wish ...
> 4. The actual application will use these modules and setup the logging
> singleton.
OK.
--
A. Sinan Unur
1usa@llenroc.ude.invalid
(remove '.invalid' and reverse each component for email address)
------------------------------
Date: 22 Nov 2004 01:02:57 GMT
From: John Bokma <postmaster@castleamber.com>
Subject: Re: Real world examples
Message-Id: <Xns95A8C1C77E136castleamber@130.133.1.4>
rc wrote:
> projects). This way I can call get/set... methods, I've seen a great
> generic example in "Object Oriented Perl" for the get/set part. -- I
> want to do this instead of sourcing in a hash so it can check that
> valid methods are being called.
If you use the proxy method, you can set up a hash to verify if the method
is allowed, see perltoot, Autoloaded Data Methods for example.
--
John Small Perl scripts: http://johnbokma.com/perl/
Perl programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html
------------------------------
Date: Sun, 21 Nov 2004 20:01:52 -0600
From: Alan Mead <amead@comcast.net>
Subject: Re: Real world examples
Message-Id: <pan.2004.11.22.02.01.50.866177@comcast.net>
On Sun, 21 Nov 2004 14:29:43 -0800, rc wrote:
> Hi, I would like some help in finding some "real world" Perl application
> examples. Everything I'm finding are short programs without proper
> logging and error handling and only accomplish a simple task.
I agree that there are a lot of short examples and not a lot of big
applications. One exception is that some modules on CPAN are pretty
comprehensive and pretty sizable.
There are examples of full-blown applications but one issue is that they
might have been written for a different version of Perl or they might just
not be written very well.
So, I suggest that you make your own application and post here with
specific questions as/if problems arise.
-Alan
------------------------------
Date: 21 Nov 2004 13:03:23 -0800
From: stern_evan@yahoo.com (Evan)
Subject: Searching backward
Message-Id: <b2d9cc70.0411211303.1bcb4db2@posting.google.com>
Hi guys,
This is a scaled down version of my work
I have a long string
$PASSENGERS ="Tom Departure Newark Arrives Sanfrasico FLIGHT_STATUS2
MORNING FLIGHT DEPARTS FROM DANE REACHES NEWARK AFTERNOON LEAVES
NEWARK AND REACHES SAN FRANSISCO NEXT MORNING"
I have multiple patterns to search within each string defined in a
separate file.
Departure|Arrival
MORNING|AFTERNOON
Need to search for the occurence and position of pattern 1, if found,
search for the occurence and position of pattern 2 and so on. The
search for reasons needs to be done in a backward direction.
Looked at rindex. It searches the string right to left but the problem
using it is that the string cannot be a regexp.
So the only option remains is to use a regexp.
Could anyone suggest any ideas in the direction of approaching this
problem.
- Evan
------------------------------
Date: Sun, 21 Nov 2004 16:47:00 -0500
From: Bob Walton <see@sig.invalid>
Subject: Re: Searching backward
Message-Id: <41a10a65$1_5@127.0.0.1>
Evan wrote:
...
> I have a long string
>
> $PASSENGERS ="Tom Departure Newark Arrives Sanfrasico FLIGHT_STATUS2
> MORNING FLIGHT DEPARTS FROM DANE REACHES NEWARK AFTERNOON LEAVES
> NEWARK AND REACHES SAN FRANSISCO NEXT MORNING"
>
> I have multiple patterns to search within each string defined in a
> separate file.
>
> Departure|Arrival
> MORNING|AFTERNOON
>
> Need to search for the occurence and position of pattern 1, if found,
> search for the occurence and position of pattern 2 and so on. The
> search for reasons needs to be done in a backward direction.
>
> Looked at rindex. It searches the string right to left but the problem
> using it is that the string cannot be a regexp.
>
> So the only option remains is to use a regexp.
>
> Could anyone suggest any ideas in the direction of approaching this
> problem.
>
> - Evan
I'm not very clear on exactly what you want to do, but: Could you
reverse() the string and the patterns you are seeking to match, and use
the regexp engine in that fashion? Maybe something like [some liberties
taken with the string you provided]:
use warnings;
use strict;
my $PASSENGERS ="Tom Departure Newark Arrival Sanfrasico".
" FLIGHT_STATUS2 MORNING FLIGHT DEPARTURE FROM DANE".
" REACHES NEWARK AFTERNOON LEAVES NEWARK AND arrival".
" SAN FRANSISCO NEXT MORNING";
my $pat1="Departure|Arrival";
my $pat2="MORNING|AFTERNOON";
$PASSENGERS=reverse $PASSENGERS;
$pat1=reverse $pat1;
$pat2=reverse $pat2;
my @matches=$PASSENGERS=~/($pat2).*?($pat1)/gi;
print scalar reverse join ':',@matches;
--
Bob Walton
Email: http://bwalton.com/cgi-bin/emailbob.pl
------------------------------
Date: Sun, 21 Nov 2004 22:26:20 GMT
From: "James Tolley" <james@emarch.net>
Subject: Re: Searching backward
Message-Id: <gC8od.8617$Vy.89@trndny06>
"Evan" <stern_evan@yahoo.com> wrote in message
news:b2d9cc70.0411211303.1bcb4db2@posting.google.com...
> Hi guys,
>
> I have multiple patterns to search within each string defined in a
> separate file.
>
> Departure|Arrival
> MORNING|AFTERNOON
>
> Need to search for the occurence and position of pattern 1, if found,
> search for the occurence and position of pattern 2 and so on. The
> search for reasons needs to be done in a backward direction.
>
Something like this should work, if I understand the issue. It's not tested,
though.
my $string = 'test this';
my @regexes = get_regexes(); # ('One|Two','Three|Four')
while(@regexes) {
my $regex = join '|', map {"($_)"} @regexes; # '(One|Two)|(Three|Four)'
$string =~ /.+($regex)/ or last; # get the regex that matches last in
the string
my $match = $1;
# do something with $match
}
hth
------------------------------
Date: 21 Nov 2004 12:53:10 -0800
From: pit.grinja@gmx.de (Piet)
Subject: where to look for object method?
Message-Id: <39cbe663.0411211253.5cb0f58c@posting.google.com>
Hello,
I just started with OO Programming in Perl and stumbled over some
behaviour that I would like to understand.
When running the following code:
use strict;
print Cow->sound."\n";
Cow->speak;
{package Cow;
use vars qw(@ISA);
@ISA = qw(Animal);
sub sound {"moooh"};
}
{package Animal;
sub sound {"some undefined noise"};
sub speak {
my $class = $_[0];
print "A $class makes ",$class->sound," !\n";
};
}
it turns out that the class method is correctly invoked, but the class
method of the superclass is not found. However, when I put
"Cow->speak" at the end (i. e. after the package definitions), I get
the expected output. Normally, I would put any code after the package
imports and definitions, in which case the behaviour described above
would not have shown up. But I am still interested in your
explanations: Why is the class method found, while the inheritance
tree is NOT climbed up?
Thanks for any hints.
Piet
------------------------------
Date: Sun, 21 Nov 2004 17:10:56 -0500
From: Sherm Pendley <spamtrap@dot-app.org>
Subject: Re: where to look for object method?
Message-Id: <ZsWdndjp5pTsjzzcRVn-2g@adelphia.com>
Piet wrote:
> use strict;
> print Cow->sound."\n";
> Cow->speak;
> {package Cow;
> use vars qw(@ISA);
> @ISA = qw(Animal);
> sub sound {"moooh"};
> }
> {package Animal;
> sub sound {"some undefined noise"};
> sub speak {
> my $class = $_[0];
> print "A $class makes ",$class->sound," !\n";
> };
> }
> it turns out that the class method is correctly invoked, but the class
> method of the superclass is not found. However, when I put
> "Cow->speak" at the end (i. e. after the package definitions), I get
> the expected output.
Perl code is executed in the order in which it appears in the source
file. Package declarations change the default package, but they do *not*
change the order of execution. To see why that's important, lets' remove
the pragmas, package declarations, and method definitions and examine
what's left:
print Cow->sound."\n";
Cow->speak;
@ISA = qw(Animal);
Does that help clear it up? Animal isn't added to @Cow::ISA until
*after* the Cow->speak method is called.
> Normally, I would put any code after the package
> imports and definitions, in which case the behaviour described above
> would not have shown up.
That's one way of doing it. Another way is to place a BEGIN { ... }
block around your class definitions, which will force them to be
executed at compile-time instead of at run-time - that is, before the
rest of your code runs.
Yet another way - which is far more common - is to place each class in
its own .pm file, and "use" them at the top of your code. Since "use"
has an implicit BEGIN block, that has the same effect.
Now, if you all will excuse me for a while, all this talk of barnyard
animals has left me wanting to hear "Pigs on the Wing". :-)
sherm--
--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
------------------------------
Date: Sun, 21 Nov 2004 17:17:15 -0500
From: Bob Walton <see@sig.invalid>
Subject: Re: where to look for object method?
Message-Id: <41a1117d$1_2@127.0.0.1>
Piet wrote:
...
> When running the following code:
> use strict;
> print Cow->sound."\n";
> Cow->speak;
> {package Cow;
> use vars qw(@ISA);
> @ISA = qw(Animal);
> sub sound {"moooh"};
> }
> {package Animal;
> sub sound {"some undefined noise"};
> sub speak {
> my $class = $_[0];
> print "A $class makes ",$class->sound," !\n";
> };
> }
> it turns out that the class method is correctly invoked, but the class
> method of the superclass is not found. However, when I put
> "Cow->speak" at the end (i. e. after the package definitions), I get
> the expected output. Normally, I would put any code after the package
> imports and definitions, in which case the behaviour described above
> would not have shown up. But I am still interested in your
> explanations: Why is the class method found, while the inheritance
> tree is NOT climbed up?
I think you're stumbling over timing. The package() function affects
things at compile time (since its only effect is at compile time, per
perldoc -f package:
"[package()] merely influences which symbol table is used
by the compiler for the rest of that block."
Thus, in your case, the sub's get set up in the symbol tables (one for
each package) prior to run time, but the code inside the packages
(notably @ISA=...) hasn't been executed yet, since you're waiting until
run time to execute it, and it gets executed after your calls. So Cow
ISN'TA Animal yet :-). Either put your packages before the code that
uses them, or put the packages in BEGIN{} blocks.
...
> Piet
--
Bob Walton
Email: http://bwalton.com/cgi-bin/emailbob.pl
------------------------------
Date: Sun, 21 Nov 2004 22:20:20 GMT
From: tiltonj@erols.com (Jay Tilton)
Subject: Re: where to look for object method?
Message-Id: <41a11175.17448890@news.erols.com>
pit.grinja@gmx.de (Piet) wrote:
: I just started with OO Programming in Perl and stumbled over some
: behaviour that I would like to understand.
: When running the following code:
: use strict;
: print Cow->sound."\n";
: Cow->speak;
: {package Cow;
: use vars qw(@ISA);
: @ISA = qw(Animal);
: sub sound {"moooh"};
: }
: {package Animal;
: sub sound {"some undefined noise"};
: sub speak {
: my $class = $_[0];
: print "A $class makes ",$class->sound," !\n";
: };
: }
: it turns out that the class method is correctly invoked, but the class
: method of the superclass is not found. However, when I put
: "Cow->speak" at the end (i. e. after the package definitions), I get
: the expected output. Normally, I would put any code after the package
: imports and definitions, in which case the behaviour described above
: would not have shown up. But I am still interested in your
: explanations: Why is the class method found, while the inheritance
: tree is NOT climbed up?
@Cow::ISA needs to be defined before invoking Cow->speak. If you put the
method call before the "@ISA = qw(Animal);" line, the method gets called
before Cow's inheritance is defined.
The usual solution is to use a BEGIN{} block to initialize such package
variables before the class is ever used:
BEGIN { @ISA = 'Animal';}
------------------------------
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 7435
***************************************