[29852] in Perl-Users-Digest
Perl-Users Digest, Issue: 1095 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Dec 7 00:09:41 2007
Date: Thu, 6 Dec 2007 21:09:07 -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 Thu, 6 Dec 2007 Volume: 11 Number: 1095
Today's topics:
Re: Code reuse without inheritance? <glex_no-spam@qwest-spam-no.invalid>
Re: Code reuse without inheritance? <joost@zeekat.nl>
Re: Code reuse without inheritance? <socyl@987jk.com.invalid>
Re: Code reuse without inheritance? <brian.d.foy@gmail.com>
Re: Code reuse without inheritance? <ben@morrow.me.uk>
Re: Code reuse without inheritance? xhoster@gmail.com
Re: Code reuse without inheritance? <ben@morrow.me.uk>
Re: Code reuse without inheritance? xhoster@gmail.com
Re: crontab script <glex_no-spam@qwest-spam-no.invalid>
Re: crontab script <glennj@ncf.ca>
Re: crontab script <krahnj@telus.net>
Re: crontab script <krahnj@telus.net>
Re: crontab script <ben@morrow.me.uk>
Re: crontab script <gabrielepetrone@gmail.com>
Re: crontab script <kkeller-usenet@wombat.san-francisco.ca.us>
Re: crontab script <ben@morrow.me.uk>
Re: How to parse out specific data in text files and pl <tadmc@seesig.invalid>
How to parse out specific data in text files and plugs cyrusgreats@gmail.com
Re: How to parse out specific data in text files and pl <glex_no-spam@qwest-spam-no.invalid>
Re: Multi phase processing <tadmc@seesig.invalid>
Re: Multi phase processing xhoster@gmail.com
Re: Multi phase processing xhoster@gmail.com
Re: Nested sorting of a hash <tadmc@seesig.invalid>
Re: sprintf or format <kingskippus@gmail.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Thu, 06 Dec 2007 15:25:28 -0600
From: "J. Gleixner" <glex_no-spam@qwest-spam-no.invalid>
Subject: Re: Code reuse without inheritance?
Message-Id: <475868c8$0$509$815e3792@news.qwest.net>
kj wrote:
> OK, there's this module XYZ (say, downloaded from CPAN), that is
> *almost* perfectly fits my needs. Furthermore, all it would take
> to make this fit perfect amounts to changing a couple of lines of
> code in some ubiquitously used internal sub, XYZ::_pesky. Assume
> that, for whatever reason, this is not a change that the author of
> XYZ.pm would make.
>
> What's a programmer to do? [...]
Provided the class is not doing anything bad, typically
you'd over-ride the method.
package MyObject;
use base "Some::Other::Object";
sub _pesky { # my version of _pesky }
------------------------------
Date: 06 Dec 2007 22:32:12 GMT
From: Joost Diepenmaat <joost@zeekat.nl>
Subject: Re: Code reuse without inheritance?
Message-Id: <4758786c$0$23829$e4fe514c@dreader20.news.xs4all.nl>
On Thu, 06 Dec 2007 21:06:15 +0000, kj wrote:
<...>
> An alternative to wholesale cut-and-paste would be to redefine the
> function in question in my own code:
>
> use XYZ ':all';
>
> {
> package XYZ;
> no warnings 'redefine';
> sub _pesky {
> # ...
> }
> }
>
> This approach may be a bit less of a chore when XYZ gets updated
<...>
In all likelyhood, at least as much of a chore as just overriding the
method in a subclass.
The only reason you'd want to do this is if you want to force your code
way down into some nested calls that you all can't change. If all the
calls to _pesky are on objects that you can change to be of your own
subclass, there's no need to redefine the original class.
Anyway, I'm assuming that the real problem is that the _pesky method is
just too complex/big. Otherwise you wouldn't have a problem with
overriding it in a subclass. So. Split up the sub so that you can
override and/or pass in just what you need. Then post those changes back
to the original author.
Joost.
------------------------------
Date: Thu, 6 Dec 2007 22:41:10 +0000 (UTC)
From: kj <socyl@987jk.com.invalid>
Subject: Re: Code reuse without inheritance?
Message-Id: <fj9tq6$av2$1@reader1.panix.com>
Sorry! I should have made it clear that XYZ is *NOT* an OO module!
(That bit most have got lost while I was editing the post.) In
other words, "overriding" is out of the question.
kj
--
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.
------------------------------
Date: Thu, 06 Dec 2007 18:27:29 -0600
From: brian d foy <brian.d.foy@gmail.com>
Subject: Re: Code reuse without inheritance?
Message-Id: <061220071827299403%brian.d.foy@gmail.com>
In article <fj9o87$cqk$1@reader1.panix.com>, kj
<socyl@987jk.com.invalid> wrote:
> OK, there's this module XYZ (say, downloaded from CPAN), that is
> *almost* perfectly fits my needs. Furthermore, all it would take
> to make this fit perfect amounts to changing a couple of lines of
> code in some ubiquitously used internal sub, XYZ::_pesky.
There's a couple of examples in Chapter 10, "Modifying and Jury-Rigging
Modules", of Mastering Perl:
http://www252.pair.com/comdog/mastering_perl/Chapters/10.subclassing.htm
l
------------------------------
Date: Fri, 7 Dec 2007 03:03:34 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Code reuse without inheritance?
Message-Id: <6ppm25-ihg1.ln1@osiris.mauzo.dyndns.org>
Quoth kj <socyl@987jk.com.invalid>:
>
> OK, there's this module XYZ (say, downloaded from CPAN), that is
> *almost* perfectly fits my needs. Furthermore, all it would take
> to make this fit perfect amounts to changing a couple of lines of
> code in some ubiquitously used internal sub, XYZ::_pesky. Assume
> that, for whatever reason, this is not a change that the author of
> XYZ.pm would make.
>
<snip>
>
> An alternative to wholesale cut-and-paste would be to redefine the
> function in question in my own code:
>
> use XYZ ':all';
>
> {
> package XYZ;
> no warnings 'redefine';
> sub _pesky {
> # ...
> }
> }
Try Test::MockModule or Sub::Override.
My first thought was to copy all the subs into a new package and then
replace some of them, but that won't work because the subs that have
been already compiled will still call the versions in the old package.
So you have to replace them in-place, for a given dynamic scope.
Ben
------------------------------
Date: 07 Dec 2007 03:26:13 GMT
From: xhoster@gmail.com
Subject: Re: Code reuse without inheritance?
Message-Id: <20071206222616.611$kp@newsreader.com>
kj <socyl@987jk.com.invalid> wrote:
> OK, there's this module XYZ (say, downloaded from CPAN), that is
> *almost* perfectly fits my needs. Furthermore, all it would take
> to make this fit perfect amounts to changing a couple of lines of
> code in some ubiquitously used internal sub, XYZ::_pesky. Assume
> that, for whatever reason, this is not a change that the author of
> XYZ.pm would make.
>
> What's a programmer to do? The most expedient workaround, of
> course, would be to simply copy the contents of XYZ.pm to a new
> file, give it a different name, and modify this clone to my heart's
> content.
But you also have to change all the parts of your code that "use"s the
module to have the new name. And you probably need to change the package
name, not just the file name, to avoid confusion (and you definitely need
to do that if you want other modules to get the old version rather than
yours.). So you also need to change all the parts of your program that use
class methods, or fully qualified subs are variables.
> But this sort of cut-and-paste programming is notoriously
> problematic.
Everything in programming is problematic. That is why we have cursers.
IME, things which are notoriously problematic are often less problematic
than the conventional wisdom replacements for them.
> For one thing, it would have to be repeated everytime
> XYZ.pm is updated (assuming we want to remain always at the bleeding
> edge of XYZ-tude).
If the upgrade fixes the problem, then you can just upgrade and do away
with your hack. If the upgrade doesn't fix the only problem that I
actually care about, then I see no reason to upgrade.
> Plus, I feel queasy about lifting a fellow
> programmer's work wholesale like that. (And here I'm completely
> setting aside legal issues regarding intellectual property, licensing,
> etc.)
Unless I'm distributing it, I don't feel queasy at all about it. Should I?
I don't think I've ever seen a license file (for Perl modules) that would
object to such use.
> An alternative to wholesale cut-and-paste would be to redefine the
> function in question in my own code:
>
> use XYZ ':all';
>
> {
> package XYZ;
> no warnings 'redefine';
> sub _pesky {
> # ...
> }
> }
>
> This approach may be a bit less of a chore when XYZ gets updated,
I don't think it is. Either way you have to verify that your hack
remains compatible with whatever changes were made in the new version.
I don't see why one way is worse than the other. Maybe even the other way
around. If you gave the copy a new name and you use it by that name, your
SA doing an upgrade without telling you isn't going to cause a you to
silently mix a fix for one version into the bulk of a different version.
But I do use this in my standard configuration module to alter functions
defined in CPAN modules. Make a change in my config module, it magically
takes place in all of my code, which is usually what I want.
> but it's still prety fragile. Plus, it doesn't work so well when
> the function to be redesigned needs to access lexically-scoped data
> in XYZ.pm.
I guess it wouldn't, but I've never run into that problem myself. Is this
a problem you actually have, or are you just thinking of the general
case?
> And, even when such redefinition is workable, it goes
> too far, because it changes the behavior of XYZ everywhere; if some
> other module we use happens to use XYZ, such redefinition is likely
> to backfire on us...
Only if the redefinition is to serve some peculiar purpose rather than
being an general improvement. If you think the change is worthy of going
into CPAN but the author disagrees with you, then you should be willing to
use it in all *your* code.
>
> Before I go off implementing some utterly insane scheme involving
> File::Slurp, s///, and eval I thought I'd ask here if there's a
> halfway decent "best practice" in these situations.
Fix the module to be object oriented and then override that method?
Barring that, I don't think you will find a single "best practice" that
meets all objections.
Of course, that brings up another problem, when you have an OO module
that is used indirectly and you need to convince the module which uses it
directly to use your subclass rather than the original.
Xho
--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
------------------------------
Date: Fri, 7 Dec 2007 03:47:26 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Code reuse without inheritance?
Message-Id: <ebsm25-blh1.ln1@osiris.mauzo.dyndns.org>
Quoth xhoster@gmail.com:
>
> Fix the module to be object oriented and then override that method?
'Fix'?? Hmmm... 'Change', perhaps... :)
> Of course, that brings up another problem, when you have an OO module
> that is used indirectly and you need to convince the module which uses it
> directly to use your subclass rather than the original.
Oh, *that*'s easy...
package My::App;
sub Foo::Bar { "My::Foo::Bar" }
use Foo; # uses Foo::Bar and calls e.g. Foo::Bar->new
It only works if the directly-calling package uses a bareword for the
class name, of course... ain't Perl evil? ;)
Ben
------------------------------
Date: 07 Dec 2007 04:45:38 GMT
From: xhoster@gmail.com
Subject: Re: Code reuse without inheritance?
Message-Id: <20071206234542.628$Gz@newsreader.com>
Ben Morrow <ben@morrow.me.uk> wrote:
> Quoth xhoster@gmail.com:
> >
> > Fix the module to be object oriented and then override that method?
>
> 'Fix'?? Hmmm... 'Change', perhaps... :)
>
> > Of course, that brings up another problem, when you have an OO module
> > that is used indirectly and you need to convince the module which uses
> > it directly to use your subclass rather than the original.
>
> Oh, *that*'s easy...
>
> package My::App;
>
> sub Foo::Bar { "My::Foo::Bar" }
>
> use Foo; # uses Foo::Bar and calls e.g. Foo::Bar->new
>
> It only works if the directly-calling package uses a bareword for the
> class name, of course... ain't Perl evil? ;)
Yes, downright demonic. I take it that moving the sub declaration to
after the "use" wouldn't work?
Xho
--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
------------------------------
Date: Thu, 06 Dec 2007 15:14:08 -0600
From: "J. Gleixner" <glex_no-spam@qwest-spam-no.invalid>
Subject: Re: crontab script
Message-Id: <47586621$0$3576$815e3792@news.qwest.net>
Manuel wrote:
> Hello,
> i was trying to create a script that checks if internet connection and
> if it is off the perl script tries to connect again and lunch a perl
> script.
A perl script for lunch??..
> */1 * * * * perl /home/user/perl/crontab.pl
No need for "*/1".
* * * * * perl /home/user/perl/crontab.pl
Does the script run outside of cron? Debug it outside of cron first.
Do you get any of your print() output?
> $ora_aggiornata=time();
> open (CHECKBOOK, "timestamp.txt");
You're sure it's able to read it?
open (CHECKBOOK, "timestamp.txt") or die "Can't open timestamp.txt: $!";
> $timestamp = <CHECKBOOK>;
> $differenza=$ora_aggiornata-$timestamp;
> print "$differenza\n";
> if ($differenza > 10000){
> $ifc=`ifconfig | grep P-t-P`;
> if ($ifc eq "") {
> `poff dsl-provider`;
> `sleep 1`;
No need to run a shell for sleep.
sleep 1;
> `pon dsl-provider`;
> }
> }
> if ($differenza > 500) {
print "blog.pl exists\n" if -f "blog.pl"; # to ensure it's there.
Really though, if all it does is update timestamp.txt, just
put that code here.
> exec("perl blog.pl &");
> } else {
> print "no not yet\n";
>
> }
------------------------------
Date: 6 Dec 2007 21:17:21 GMT
From: Glenn Jackman <glennj@ncf.ca>
Subject: Re: crontab script
Message-Id: <slrnflgpn3.qp6.glennj@smeagol.ncf.ca>
At 2007-12-06 01:52PM, "Manuel" wrote:
[...]
> */1 * * * * perl /home/user/perl/crontab.pl
Almost invariably, problems with cron scripts boild down to differences
between cron's environment and your shell. Also, you're doing no error
checking, so cron and perl aren't telling you what the problem is.
> this is the code of crontab.pl:
[...]
> open (CHECKBOOK, "timestamp.txt");
What's the current directory when running this script inside cron?
Does this file exist there?
open (CHECKBOOK, "timestamp.txt") or do {
require Cwd;
my $cwd = cwd();
die "can't read $cwd/timestamp.txt: $!\n";
}
[...]
> `sleep 1`;
Do you know Perl has a function called sleep?
--
Glenn Jackman
"You can only be young once. But you can always be immature." -- Dave Barry
------------------------------
Date: Thu, 06 Dec 2007 21:38:03 GMT
From: "John W. Krahn" <krahnj@telus.net>
Subject: Re: crontab script
Message-Id: <47586BBA.3C1BF3DC@telus.net>
Glenn Jackman wrote:
>
> At 2007-12-06 01:52PM, "Manuel" wrote:
> [...]
> > */1 * * * * perl /home/user/perl/crontab.pl
>
> Almost invariably, problems with cron scripts boild down to differences
> between cron's environment and your shell. Also, you're doing no error
> checking, so cron and perl aren't telling you what the problem is.
>
> > this is the code of crontab.pl:
> [...]
> > open (CHECKBOOK, "timestamp.txt");
>
> What's the current directory when running this script inside cron?
> Does this file exist there?
>
> open (CHECKBOOK, "timestamp.txt") or do {
> require Cwd;
> my $cwd = cwd();
You are using require so that should be:
my $cwd = Cwd::cwd();
John
--
use Perl;
program
fulfillment
------------------------------
Date: Thu, 06 Dec 2007 21:56:54 GMT
From: "John W. Krahn" <krahnj@telus.net>
Subject: Re: crontab script
Message-Id: <47587025.D6A89618@telus.net>
Manuel wrote:
>
> i was trying to create a script that checks if internet connection and
> if it is off the perl script tries to connect again and lunch a perl
> script.
> it opens a txt file, get the timestamp. in $differenza i get the
> difference between now and the timestamp in the txt file (in this file
> i write with blog.pl the operation time timestamp ). if the difference
> is more than 10000 seconds it means the connection is off so it tries
> to connect again. and if the difference is more than 500 it start the
> blog.pl process again. i did put this file in the crontab list:
> */1 * * * * perl /home/user/perl/crontab.pl
> but the blog.pl never stars. Why???
>
> this is the code of crontab.pl:
use warnings;
use strict;
> $ora_aggiornata=time();
> open (CHECKBOOK, "timestamp.txt");
You should *always* verify that the file opened correctly:
open CHECKBOOK, '<', 'timestamp.txt' or die "Cannot open 'timestamp.txt'
$!";
> $timestamp = <CHECKBOOK>;
chomp( my $timestamp = <CHECKBOOK> );
> $differenza=$ora_aggiornata-$timestamp;
> print "$differenza\n";
> if ($differenza > 10000){
> $ifc=`ifconfig | grep P-t-P`;
> if ($ifc eq "") {
Since you don't chomp() the output the variable $ifc will never equal
"".
> `poff dsl-provider`;
> `sleep 1`;
> `pon dsl-provider`;
You should use the complete absolute path to any external programs. You
shouldn't use back-ticks in a void context:
0 == system '/somewhere/bin/poff', 'dsl-provider'
or die "system '/somewhere/bin/poff' failed: $?"
sleep 1;
0 == system '/somewhere/bin/pon', 'dsl-provider'
or die "system '/somewhere/bin/pon' failed: $?"
> }
> }
> if ($differenza > 500) {
> exec("perl blog.pl &");
If you want to run that in the background you can use fork():
defined( $child = fork ) and $child
or exec '/usr/bin/perl', '/somewhere/blog.pl';
> } else {
> print "no not yet\n";
>
> }
John
--
use Perl;
program
fulfillment
------------------------------
Date: Thu, 6 Dec 2007 21:18:55 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: crontab script
Message-Id: <vi5m25-p1f1.ln1@osiris.mauzo.dyndns.org>
Quoth Manuel <gabrielepetrone@gmail.com>:
> i was trying to create a script that checks if internet connection and
You may find the module LWP::Online useful for this.
> if it is off the perl script tries to connect again and lunch a perl
> script.
> it opens a txt file, get the timestamp. in $differenza i get the
> difference between now and the timestamp in the txt file (in this file
> i write with blog.pl the operation time timestamp ). if the difference
> is more than 10000 seconds it means the connection is off so it tries
> to connect again. and if the difference is more than 500 it start the
> blog.pl process again. i did put this file in the crontab list:
> */1 * * * * perl /home/user/perl/crontab.pl
> but the blog.pl never stars. Why???
>
>
> this is the code of crontab.pl:
>
Where is
#!/usr/bin/perl
use warnings;
use strict;
?
> $ora_aggiornata=time();
> open (CHECKBOOK, "timestamp.txt");
Always check the return value of open.
Use lexical filehandles.
Use three-arg open.
open my $CHECKBOOK, '<', 'timestamp.txt'
or die "can't open timestamp.txt: $!";
> $timestamp = <CHECKBOOK>;
> $differenza=$ora_aggiornata-$timestamp;
> print "$differenza\n";
> if ($differenza > 10000){
> $ifc=`ifconfig | grep P-t-P`;
> if ($ifc eq "") {
> `poff dsl-provider`;
Don't use backticks just to run a command. That's what system is for.
system 'poff dsl-provider';
or, clearer and safer,
system 'poff', 'dsl-provider';
I wouldn't use an external grep at all, just read the whole lot in and
check it in Perl:
local $/ = undef;
if (`ifconfig` =~ /P-t-P/) {
but thats something of a matter of taste in this instance. If ifconfig
were going to produce a large amount of output (several hundred MB or
more) it might be better to filter it with grep first, but that isn't
the case here.
> `sleep 1`;
Perl has a sleep function built in. You don't need an external command.
sleep 1;
> `pon dsl-provider`;
> }
> }
> if ($differenza > 500) {
> exec("perl blog.pl &");
I'm not sure what you intended to acheive with this... Perl is not
shell. This statement will replace your perl process with a shell, which
will run blog.pl in the background and wait for it, then exit. That's a
whole process sitting there doing nothing for no reason.
exec 'perl blog.pl';
or, for the same reasons as above,
exec 'perl', 'blog.pl';
or, more portably,
exec $^X, 'blog.pl';
$^X is a special variable which names the currently running copy of
perl. exec can fail. You need to check for this, particularly as I
suspect your problem is your program ends up running in the wrong
directory, so it can't find blog.pl.
exec $^X, 'blog.pl'
or die "exec or blog.pl failed: $!";
> } else {
> print "no not yet\n";
>
> }
Ben
------------------------------
Date: Thu, 6 Dec 2007 15:50:26 -0800 (PST)
From: Manuel <gabrielepetrone@gmail.com>
Subject: Re: crontab script
Message-Id: <07cfddc9-b7f4-4e65-b162-95b79f0b6b70@e25g2000prg.googlegroups.com>
sorry but this
*/1 * * * * perl /home/user/perl/crontab.pl
means that it starts crontab.pl every minute right?
------------------------------
Date: Thu, 6 Dec 2007 16:36:48 -0800
From: Keith Keller <kkeller-usenet@wombat.san-francisco.ca.us>
Subject: Re: crontab script
Message-Id: <16hm25x4ok.ln2@goaway.wombat.san-francisco.ca.us>
On 2007-12-06, Manuel <gabrielepetrone@gmail.com> wrote:
> sorry but this
> */1 * * * * perl /home/user/perl/crontab.pl
>
> means that it starts crontab.pl every minute right?
That's a cron question, not a perl question. (You're right, but it's
better as
* * * * * .....
)
--keith
--
kkeller-usenet@wombat.san-francisco.ca.us
(try just my userid to email me)
AOLSFAQ=http://www.therockgarden.ca/aolsfaq.txt
see X- headers for PGP signature information
------------------------------
Date: Fri, 7 Dec 2007 03:07:29 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: crontab script
Message-Id: <h0qm25-ihg1.ln1@osiris.mauzo.dyndns.org>
Quoth Manuel <gabrielepetrone@gmail.com>:
> sorry but this
> */1 * * * * perl /home/user/perl/crontab.pl
>
> means that it starts crontab.pl every minute right?
No idea, read your crontab manual. The syntax is (slightly) different on
different systems. Also, at least on my system, cron only wakes up once
a minute, so it's likely you won't get terribly regular scheduling. It's
better for short waits like this to write a process that runs the whole
time, with something like
while (1) {
sleep 60;
# or something better, that calculates the appropriate wait
# do some stuff
}
around the main work.
Ben
------------------------------
Date: Fri, 07 Dec 2007 01:38:48 GMT
From: Tad McClellan <tadmc@seesig.invalid>
Subject: Re: How to parse out specific data in text files and plugs into the spreadsheet
Message-Id: <slrnflh32d.43v.tadmc@tadmc30.sbcglobal.net>
cyrusgreats@gmail.com <cyrusgreats@gmail.com> wrote:
> I need help writing a script in perl that takes text files and parses
> out specific
You have left the specifics unspecified...
> data and plugs the data back into the spreadsheet.
Errr, what flavor of spreadsheet would that be?
Many of them have there own, uncompatible, data formats.
> Can
> anyone help me on that..
Yes.
> Thanks in advance..
You're welcome in advance.
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
------------------------------
Date: Thu, 6 Dec 2007 14:40:31 -0800 (PST)
From: cyrusgreats@gmail.com
Subject: How to parse out specific data in text files and plugs into the spreadsheet
Message-Id: <65e3b7ae-0d34-4a10-8e5e-2c01aa76faeb@s8g2000prg.googlegroups.com>
I need help writing a script in perl that takes text files and parses
out specific data and plugs the data back into the spreadsheet. Can
anyone help me on that..Thanks in advance..
------------------------------
Date: Thu, 06 Dec 2007 17:20:06 -0600
From: "J. Gleixner" <glex_no-spam@qwest-spam-no.invalid>
Subject: Re: How to parse out specific data in text files and plugs into the spreadsheet
Message-Id: <475883a6$0$509$815e3792@news.qwest.net>
cyrusgreats@gmail.com wrote:
> I need help writing a script in perl that takes text files and parses
> out specific data and plugs the data back into the spreadsheet. Can
> anyone help me on that..Thanks in advance..
The documentation can help, along with thousands of Web sites, and
many, many books.
perldoc perlopentut
perldoc -f split
Books:
http://books.perl.org/
------------------------------
Date: Fri, 07 Dec 2007 01:38:48 GMT
From: Tad McClellan <tadmc@seesig.invalid>
Subject: Re: Multi phase processing
Message-Id: <slrnflh3dn.43v.tadmc@tadmc30.sbcglobal.net>
Dave <daves@orpheusmail.co.uk> wrote:
> Since Apache is stateless, all the data input from previous
> pages has to be put back into the next page in the form of hidden
^^^^^^
^^^^^^
No it doesn't.
> input tags, and then read back in each time.
Passing via hidden fields is but 1 of 3 "usual" ways of managing state,
and it is generally the worst of the three.
The 3 usual ways, in order of "best to worst" are:
store info on server and pass only a session ID around
store info in cookies
store info in hidden fields
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
------------------------------
Date: 07 Dec 2007 02:32:23 GMT
From: xhoster@gmail.com
Subject: Re: Multi phase processing
Message-Id: <20071206213226.480$d4@newsreader.com>
Dave <daves@orpheusmail.co.uk> wrote:
>
> The end result would look something like this, where each '@' is a
> separate input type="checkbox" html tag:
>
> Language VME Windows Linux
> Application Master @ @ @
> C @ @ @
> Cobol @ @ @
> Perl @ @ @
> SCL @ @ @
>
> The idea being that if someone has programmed in Cobol on VM, and in
> Perl on Windows and Linux the appropriate boxes are ticked. The
> validation is fun because in the above, cut down, example, Application
> master and SCL are only available on VME, and Perl is the only one not
> available on VME. The other two being available on all platforms.
I'm not sure why any peculiar type validation is needed. You have no way
to prevent people from lying, do you? If not, why take pains to make
sure their lies are at least plausible?
> Where this is the only page, so that all the info is input in one go,
> I have no problems. Where I have a problem, is holding this in
> temporary input type = "hidden" tags within the html page. Does anyone
> have any suggestions on the best way to hold this data?
I don't understand the problem. You should be able to stuff it back
into hidden tags exactly the same way you get it out of the original
submission. For example, if all the checkbox names start with "lang_exp",
like "lang_exp_C_VME", "lang_exp_Perl_Linux", then something like:
print $q->hidden($_) foreach grep /^lang_exp/, $q->param();
Should work, right?
Xho
--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
------------------------------
Date: 07 Dec 2007 02:46:35 GMT
From: xhoster@gmail.com
Subject: Re: Multi phase processing
Message-Id: <20071206214639.025$PS@newsreader.com>
Tad McClellan <tadmc@seesig.invalid> wrote:
> Dave <daves@orpheusmail.co.uk> wrote:
>
> > Since Apache is stateless, all the data input from previous
> > pages has to be put back into the next page in the form of hidden
> ^^^^^^
> ^^^^^^
> No it doesn't.
>
> > input tags, and then read back in each time.
>
> Passing via hidden fields is but 1 of 3 "usual" ways of managing state,
> and it is generally the worst of the three.
>
> The 3 usual ways, in order of "best to worst" are:
>
> store info on server and pass only a session ID around
>
> store info in cookies
>
> store info in hidden fields
Why are cookies better than hidden fields? I usually find hidden fields
much better than cookies. In fact, I often find them better than sessions.
It doesn't really require any extra flap-doodle, as I need to collect and
validate the data in the first place, I can use the same code to do it
again out of the hidden field. I don't need to install some special module
to keep state and then maintain and configure it. If the server crashes
momentarily, the user hasn't lost their state. I don't have to decide how
long to maintain state for people who are taking a long time to finish all
the steps of the task.
Xho
--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
------------------------------
Date: Thu, 6 Dec 2007 05:56:02 -0600
From: Tad McClellan <tadmc@seesig.invalid>
Subject: Re: Nested sorting of a hash
Message-Id: <slrnflfoqi.pmt.tadmc@tadmc30.sbcglobal.net>
Justin C <justin.0711@purestblue.com> wrote:
> I have a hash containing stock codes (the keys) and the number of units
> sold (the values).
> Here is an example of the data I have:
>
> Code Qty
> ABZ001 13
> ADF001 7
> ADF002 6
> ADF003 6
> ADF004 6
> ABZ002 5
> ABZ003 5
> ABZ120 4
> ABZ047 4
> ABZ022 4
> ADF027 4
> ADF019 4
>
> What I'd like to have is within those items that have sold 4 units they
> be sorted asciibetically.
Your question is answered in the Perl FAQ:
How do I sort a hash (optionally by value instead of key)?
> The sort I have for the hash so far is:
>
> foreach ( sort { $sales{$b} <=> $sales{$a} } keys %sales) {
> print "$_;\t$sales{$_}\n";
> }
foreach ( sort { $sales{$b} <=> $sales{$a} or $a cmp $b } keys %sales) {
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
------------------------------
Date: Thu, 6 Dec 2007 14:11:38 -0800 (PST)
From: TonyV <kingskippus@gmail.com>
Subject: Re: sprintf or format
Message-Id: <78211163-2dc5-4f56-b2e7-45077bda2835@j20g2000hsi.googlegroups.com>
On Dec 6, 3:31 pm, joeyfoley <joey.fo...@gmail.com> wrote:
> Hi All,
>
> I have a file that I am reading in that I need to reprint and format
> the output.
>
> There are 4 parts that need to be printed on a line.
>
> Name 1-14
> Type 16-22
> Start 24-28 (left justified)
> Width 30-32 (left justified)
>
> However, if the name is greater than 14 characters, then the rest of
> the code is moved right accordingly.
>
> The algorithm is basically:
>
> If length(Name) <=14 then:
> print @1 Name
> print @16 Type
> print @24 Start (left justified)
> print @30 Width (left justified)
> else
> print @1 Name
> print@(length(name)+1) Type
> print@(length(name)+10) Start (left justified)
> print@(length(name)+15) Width (left justified)
>
> How do I print with this algorithm?
>
> Thanks.
Personally, I'd use an sprintf. If you're just one-off printing a
row, you don't need to do anything special; the %s format specifier
will print the whole string if it's longer than a given field length.
For example:
print sprintf("%13s %s", "This field is waaaay", "too long");
...will print "This field is waaaay too long". However, this one:
print sprintf("%13s %s", "This one's", "short");
...will print "___This one's short" (where _'s are actually spaces).
If you want to truncate the field, you'd need to take a substr of its
field length:
print sprintf("%13s %s", substr("This field is waaaay", 0, 13), "too
long");
...will print "This field is too long".
If you're printing a bunch of rows of fields, you'll need to make a
first pass to find out what your maximum field width is. Here's a
short demo program that you can adapt if you want. Hope it helps!
# ---------------------------------------------------------
my @rows = (
{ name => "John Jacob Jingleheimerschmidt",
type => "guy",
start => 123,
width => 24 },
{ name => "Mary R. Public",
type => "girl",
start => 456,
width => 12 },
{ name => "Baby New Year",
type => "child",
start => 789,
width => 1 }
);
my @widths = (14, 7, 5, 3); # Default minimum widths
foreach (@rows) {
if (length $_->{name} > $widths[0])
{ $widths[0] = length $_->{name}; }
if (length $_->{type} > $widths[1])
{ $widths[1] = length $_->{type}; }
if (length $_->{start} > $widths[2])
{ $widths[2] = length $_->{start}; }
if (length $_->{width} > $widths[3])
{ $widths[3] = length $_->{width}; }
}
print "-" x $widths[0] . " ";
print "-" x $widths[1] . " ";
print "-" x $widths[2] . " ";
print "-" x $widths[3] . "\n";
my $format_string = "%$widths[0]s %$widths[1]s " .
"%-$widths[2]s %-$widths[3]s\n";
foreach (@rows) {
print sprintf($format_string,
$_->{name}, $_->{type}, $_->{start}, $_->{width});
}
# ---------------------------------------------------------
------------------------------
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 V11 Issue 1095
***************************************