From faber at linuxnj.com Fri Oct 1 14:35:39 2004 From: faber at linuxnj.com (Faber Fedor) Date: Fri Oct 1 14:35:41 2004 Subject: [ABE.pm] array references? Message-ID: <20041001193539.GA1070@uranus.faber.nom> How does one access elements in an array reference? I'v done this: my $data_array = $dbh->selectall_arrayref("select A, B from $table where realdate = '" . $fulldate ."'"); and I get something back, I just can't figure out how to access the data in column A or B. How would I print out each pair of values? -- Regards, Faber Linux New Jersey: Open Source Solutions for New Jersey http://www.linuxnj.com From phil at five-lawrences.com Fri Oct 1 14:51:48 2004 From: phil at five-lawrences.com (Phil Lawrence) Date: Fri Oct 1 14:51:55 2004 Subject: [ABE.pm] array references? In-Reply-To: <20041001193539.GA1070@uranus.faber.nom> References: <20041001193539.GA1070@uranus.faber.nom> Message-ID: <550E9CB6-13E3-11D9-99A4-003065C45FE0@five-lawrences.com> On Oct 1, 2004, at 15:35, Faber Fedor wrote: > How does one access elements in an array reference? I'v done this: > > my $data_array = $dbh->selectall_arrayref("select A, B from > $table where realdate = '" . $fulldate ."'"); > > and I get something back, I just can't figure out how to access the > data > in column A or B. How would I print out each pair of values? print $aref->[0][0]; #prints first elem of first row Also, fire up the perl debugger and make like so: ... x $aref prl From faber at linuxnj.com Fri Oct 1 17:05:11 2004 From: faber at linuxnj.com (Faber Fedor) Date: Fri Oct 1 17:05:13 2004 Subject: [ABE.pm] Re: array references? In-Reply-To: <550E9CB6-13E3-11D9-99A4-003065C45FE0@five-lawrences.com> References: <20041001193539.GA1070@uranus.faber.nom> <550E9CB6-13E3-11D9-99A4-003065C45FE0@five-lawrences.com> Message-ID: <20041001220511.GB1263@uranus.faber.nom> On 01/10/04 15:51 -0400, Phil Lawrence wrote: > > On Oct 1, 2004, at 15:35, Faber Fedor wrote: > > >How does one access elements in an array reference? I'v done this: > > > >my $data_array = $dbh->selectall_arrayref("select A, B from > > $table where realdate = '" . $fulldate ."'"); > > > >and I get something back, I just can't figure out how to access the > >data > >in column A or B. How would I print out each pair of values? > > print $aref->[0][0]; #prints first elem of first row I hacked this together from something I read on usenet: my $data_array = $dbh->selectall_arrayref("select id, weight from $table where realdate = '" . $fulldate ."'"); my $sum ; # forsome reason =+ didn't work here :-? foreach my $i (@{$data_array}) { $sum = $sum + @$i->[1]; } foreach my $i (@{$data_array}) { @$i->[1] = 100*(@$i->[1]/$sum); } open(OUTFILE, "> $outfile"); foreach my $i (@{$data_array}) { printf OUTFILE "%8s, %5.4f \n", @$i->[0], @$i->[1] ; } close(OUTFILE); but there's got to be a more elegant way. > Also, fire up the perl debugger and make like so: I really have to learn how to use that thing... -- Regards, Faber Linux New Jersey: Open Source Solutions for New Jersey http://www.linuxnj.com From phil at five-lawrences.com Fri Oct 1 23:20:42 2004 From: phil at five-lawrences.com (Phil Lawrence) Date: Fri Oct 1 23:20:55 2004 Subject: [ABE.pm] Re: array references? In-Reply-To: <20041001220511.GB1263@uranus.faber.nom> References: <20041001193539.GA1070@uranus.faber.nom> <550E9CB6-13E3-11D9-99A4-003065C45FE0@five-lawrences.com> <20041001220511.GB1263@uranus.faber.nom> Message-ID: <6C8AEF1A-142A-11D9-AF00-003065C45FE0@five-lawrences.com> On Oct 1, 2004, at 18:05, Faber Fedor wrote: > On 01/10/04 15:51 -0400, Phil Lawrence wrote: >> >> On Oct 1, 2004, at 15:35, Faber Fedor wrote: >> >>> How does one access elements in an array reference? I'v done this: >>> >>> my $data_array = $dbh->selectall_arrayref("select A, B from >>> $table where realdate = '" . $fulldate ."'"); >>> >>> and I get something back, I just can't figure out how to access the >>> data >>> in column A or B. How would I print out each pair of values? >> >> print $aref->[0][0]; #prints first elem of first row > > I hacked this together from something I read on usenet: > > my $data_array = $dbh->selectall_arrayref("select id, weight from > $table where realdate = '" . $fulldate ."'"); my $nested_arefs = $dbh->selectall_arrayref(<<"ENDSQL", undef, $fulldate); SELECT id , weight FROM $table WHERE realdate = ? ENDSQL > > my $sum ; > # forsome reason =+ didn't work here :-? > foreach my $i (@{$data_array}) { > $sum = $sum + @$i->[1]; > } # it's +=, not =+ :-) my $sum; for my $record_aref (@$nested_arefs) { $sum += $record_aref->[1]; } -- OR -- my $sum; $sum += $_->[1] for @$nested_arefs; > > foreach my $i (@{$data_array}) { > @$i->[1] = 100*(@$i->[1]/$sum); > } $_->[1] = 100 * ($_->[1]/$sum) for @$nested_arefs; > open(OUTFILE, "> $outfile"); > > foreach my $i (@{$data_array}) { > printf OUTFILE "%8s, %5.4f \n", @$i->[0], @$i->[1] ; > } > > close(OUTFILE); open OUTFILE, ">$outfile" or die; printf OUTFILE "%8s, %5.4f \n", @$_[0,1] for @$nested_arefs; close OUTFILE; > but there's got to be a more elegant way. How do those examples look to you? better? Be careful, i see you were using the '@' sigil when all you needed was the '$' sigil. Example: @$i->[1] should be: $i->[1] I think that's because the infix operator ('->') combined with the 'square' brackets already implies the fact that you're dereferencing an aref. You'll probably pick it up the sigil thing pretty quick. >> Also, fire up the perl debugger and make like so: > > I really have to learn how to use that thing... I don't know how to *really* use it, but I can do a few things: ~ phil$ perl -de0 Loading DB routines from perl5db.pl version 1.22 Editor support available. Enter h or `h h' for help, or `man perldebug' for more help. main::(-e:1): 0 DB<1> $aref = [ [1,2], [3,4] ] DB<2> x $aref 0 ARRAY(0x892b50) 0 ARRAY(0x801318) 0 1 1 2 1 ARRAY(0x892b20) 0 3 1 4 DB<3> q ~ phil$ Oh, and don't forget these at the top of *every* script: use warnings; use strict; use diagnostics; :-) prl From rjbs-perl-abe at lists.manxome.org Sat Oct 2 15:17:47 2004 From: rjbs-perl-abe at lists.manxome.org (Ricardo SIGNES) Date: Sat Oct 2 15:18:11 2004 Subject: [ABE.pm] Re: array references? In-Reply-To: <20041001220511.GB1263@uranus.faber.nom> References: <20041001193539.GA1070@uranus.faber.nom> <550E9CB6-13E3-11D9-99A4-003065C45FE0@five-lawrences.com> <20041001220511.GB1263@uranus.faber.nom> Message-ID: <20041002161747.A239@manxome.org> * Faber Fedor [2004-10-01T18:05:11] > I hacked this together from something I read on usenet: > > my $data_array = $dbh->selectall_arrayref("select id, weight from > $table where realdate = '" . $fulldate ."'"); I'm not sure about the use of $table, but at least consider $dbh->selectall_arrayref( "SELECT id, weight FROM table WHERE realdate = ?", undef, $fulldate ); This will use proper quoting to get the date in, and avoid SQL injection attacks. > my $sum ; > # forsome reason =+ didn't work here :-? Because it's += > foreach my $i (@{$data_array}) { > $sum = $sum + @$i->[1]; > } $sum += @{$_->[1]} for @$data_array; > foreach my $i (@{$data_array}) { > @$i->[1] = 100*(@$i->[1]/$sum); > } > open(OUTFILE, "> $outfile"); Eliminate the foreach. open my $output, '>', $outfile; This creates a lexical filehandle-reference, which will autoclose when it goes out of scope. There are other benefits. Check "perldoc perlopentut" > foreach my $i (@{$data_array}) { > printf OUTFILE "%8s, %5.4f \n", @$i->[0], @$i->[1] ; > } foreach (@$data_array) { printf $output "%8s, %5.4f \n", $_->[0], (100*($_->[1]/$sum)); } This combines the previous loop into this one. You could reassign the element in the arrayref, if you wanted, but I was guessing you just did that to get at it here. You could also do something like this: print $output map { sprintf "%8s, %5.4f\n", $_->[0], (100...) } @$data_array; but that's just a question of whether you like reading English or Hebrew. > close(OUTFILE); close $output; I think that's less inelegant. Surely there are other, possibly better, ways to do this. -- rjbs -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 189 bytes Desc: not available Url : http://mail.pm.org/archives/abe-pm/attachments/20041002/111ad26f/attachment.bin From rjbs-perl-abe at lists.manxome.org Mon Oct 4 14:36:37 2004 From: rjbs-perl-abe at lists.manxome.org (Ricardo SIGNES) Date: Mon Oct 4 14:37:33 2004 Subject: [ABE.pm] next week! Message-ID: <20041004153636.C511@manxome.org> Next week will make it six months since we last met! Let's get together and yammer, and possibly follow up on the yammering with some beer and fries. Whaddaya say? I have some keen-o stuff I've been playing with, and would be deligted to rant about for a little while: * Querylet: a way to make it easy for non-boneheaded non-programmers to write queries and reports in Not Quite Perl (*of special interest to non-Perl programmers as well as old hands) * Module::Starter: the nice, new, pluggable replacement for h2xs * Class::DBI and DBD::SQLite, two of my new best friends * CPAN::Mini, for a nice local CPAN mirror * other assorted bits of nonsense -- rjbs -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 189 bytes Desc: not available Url : http://mail.pm.org/archives/abe-pm/attachments/20041004/2ee4bd08/attachment.bin From phil at five-lawrences.com Mon Oct 4 14:44:02 2004 From: phil at five-lawrences.com (Phil Lawrence) Date: Mon Oct 4 14:44:09 2004 Subject: [ABE.pm] next week! In-Reply-To: <20041004153636.C511@manxome.org> References: <20041004153636.C511@manxome.org> Message-ID: On Oct 4, 2004, at 15:36, Ricardo SIGNES wrote: > Next week will make it six months since we last met! Let's get > together > and yammer, and possibly follow up on the yammering with some beer and > fries. Whaddaya say? Wahoo! I'll be there, and Steve and Jim said 6 months sounds about right for another meeting! ;-) Definitely following up with beer & fries. prl From sol0 at lehigh.edu Mon Oct 4 16:47:00 2004 From: sol0 at lehigh.edu (Lidie Steve) Date: Mon Oct 4 16:47:10 2004 Subject: [ABE.pm] next week! In-Reply-To: <20041004153636.C511@manxome.org> References: <20041004153636.C511@manxome.org> Message-ID: On Oct 4, 2004, at 3:36 PM, Ricardo SIGNES wrote: > > Next week will make it six months since we last met! Let's get > together > i'll try to make it - what time again - it's been a while? From faber at linuxnj.com Mon Oct 4 18:06:04 2004 From: faber at linuxnj.com (Faber Fedor) Date: Mon Oct 4 18:06:07 2004 Subject: [ABE.pm] Per *Unix writing DOS files Message-ID: <20041004230604.GA24393@uranus.faber.nom> If I'm on a Linux box running a Perl script, writing out to a file via print, what is the Proper way of making the output file a "DOS file", e.g. with DOS EOL (\r\n, right? Or do I do ^M?). -- Regards, Faber Linux New Jersey: Open Source Solutions for New Jersey http://www.linuxnj.com From faber at linuxnj.com Mon Oct 4 19:05:56 2004 From: faber at linuxnj.com (Faber Fedor) Date: Mon Oct 4 19:05:58 2004 Subject: [ABE.pm] Re: array references? In-Reply-To: <20041002161747.A239@manxome.org> References: <20041001193539.GA1070@uranus.faber.nom> <550E9CB6-13E3-11D9-99A4-003065C45FE0@five-lawrences.com> <20041001220511.GB1263@uranus.faber.nom> <20041002161747.A239@manxome.org> Message-ID: <20041005000556.GF24393@uranus.faber.nom> On 02/10/04 16:17 -0400, Ricardo SIGNES wrote: > * Faber Fedor [2004-10-01T18:05:11] > > I hacked this together from something I read on usenet: > > > > my $data_array = $dbh->selectall_arrayref("select id, weight from > > $table where realdate = '" . $fulldate ."'"); > > I'm not sure about the use of $table, but at least consider $table is a variable that get's set, just like $fulldate. > > $dbh->selectall_arrayref( > "SELECT id, weight FROM table WHERE realdate = ?", > undef, > $fulldate > ); > > This will use proper quoting to get the date in, and avoid SQL injection > attacks. This will put quotes around the date, e.g. the SQL will read like this: SELECT id, weight FROM table WHERE realdate = '1993-01-31' :-? > $sum += @{$_->[1]} for @$data_array; Now what's the difference between yours and Phils, other than you have more squigglies in yours? > open my $output, '>', $outfile; > > This creates a lexical filehandle-reference, which will autoclose when > it goes out of scope. That's cute. -- Regards, Faber Linux New Jersey: Open Source Solutions for New Jersey http://www.linuxnj.com From rjbs-perl-abe at lists.manxome.org Mon Oct 4 19:26:19 2004 From: rjbs-perl-abe at lists.manxome.org (Ricardo SIGNES) Date: Mon Oct 4 19:27:19 2004 Subject: [ABE.pm] next week! In-Reply-To: References: <20041004153636.C511@manxome.org> Message-ID: <20041004202619.E511@manxome.org> * Lidie Steve [2004-10-04T17:47:00] > > On Oct 4, 2004, at 3:36 PM, Ricardo SIGNES wrote: > > > > >Next week will make it six months since we last met! Let's get > >together > > > > i'll try to make it - what time again - it's been a while? iCal says 19:00 -- rjbs -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 189 bytes Desc: not available Url : http://mail.pm.org/archives/abe-pm/attachments/20041004/fdf719b3/attachment.bin From rjbs-perl-abe at lists.manxome.org Mon Oct 4 19:29:04 2004 From: rjbs-perl-abe at lists.manxome.org (Ricardo SIGNES) Date: Mon Oct 4 19:30:03 2004 Subject: [ABE.pm] Per *Unix writing DOS files In-Reply-To: <20041004230604.GA24393@uranus.faber.nom> References: <20041004230604.GA24393@uranus.faber.nom> Message-ID: <20041004202904.F511@manxome.org> * Faber Fedor [2004-10-04T19:06:04] > If I'm on a Linux box running a Perl script, writing out to a file via > print, what is the Proper way of making the output file a "DOS file", > e.g. with DOS EOL (\r\n, right? Or do I do ^M?). Consult "perldoc perlvar" under $OUTPUT_RECORD_SEPARATOR. The variable you want to muck about with is $\ and I suggest you localize it for the block in which you write the file. @data = gather_some_lines; { local $\ = "\r\n"; open my $dos_fh, '>', 'file_dos.txt' or die "can't open file for writing: $!"; print $dos_fh @data; } -- rjbs -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 189 bytes Desc: not available Url : http://mail.pm.org/archives/abe-pm/attachments/20041004/51d2af9e/attachment.bin From faber at linuxnj.com Mon Oct 4 19:44:40 2004 From: faber at linuxnj.com (Faber Fedor) Date: Mon Oct 4 19:44:42 2004 Subject: [ABE.pm] Re: Per *Unix writing DOS files In-Reply-To: <20041004202904.F511@manxome.org> References: <20041004230604.GA24393@uranus.faber.nom> <20041004202904.F511@manxome.org> Message-ID: <20041005004440.GA24688@uranus.faber.nom> On 04/10/04 20:29 -0400, Ricardo SIGNES wrote: > * Faber Fedor [2004-10-04T19:06:04] > > If I'm on a Linux box running a Perl script, writing out to a file via > > print, what is the Proper way of making the output file a "DOS file", > > e.g. with DOS EOL (\r\n, right? Or do I do ^M?). > > Consult "perldoc perlvar" under $OUTPUT_RECORD_SEPARATOR. The variable > you want to muck about with is $\ and I suggest you localize it for the > block in which you write the file. The way the original programmer wrote it, the "local block" is the entire file. :-) But this will come in handy nevertheless. -- Regards, Faber Linux New Jersey: Open Source Solutions for New Jersey http://www.linuxnj.com From sol0 at lehigh.edu Tue Oct 5 06:38:00 2004 From: sol0 at lehigh.edu (Lidie Steve) Date: Tue Oct 5 06:38:03 2004 Subject: [ABE.pm] next week! In-Reply-To: <20041004202619.E511@manxome.org> References: <20041004153636.C511@manxome.org> <20041004202619.E511@manxome.org> Message-ID: <028AC87C-16C3-11D9-A148-000A95BA93C6@lehigh.edu> On Oct 4, 2004, at 8:26 PM, Ricardo SIGNES wrote: > * Lidie Steve [2004-10-04T17:47:00] >> >> On Oct 4, 2004, at 3:36 PM, Ricardo SIGNES wrote: >> >>> >>> Next week will make it six months since we last met! Let's get >>> together >>> >> >> i'll try to make it - what time again - it's been a while? > > iCal says 19:00 > iCal'ed here as well, thanks. From phil at five-lawrences.com Tue Oct 5 07:45:04 2004 From: phil at five-lawrences.com (Phil Lawrence) Date: Tue Oct 5 07:45:10 2004 Subject: [ABE.pm] array references? In-Reply-To: <20041005000556.GF24393@uranus.faber.nom> References: <20041001193539.GA1070@uranus.faber.nom> <550E9CB6-13E3-11D9-99A4-003065C45FE0@five-lawrences.com> <20041001220511.GB1263@uranus.faber.nom> <20041002161747.A239@manxome.org> <20041005000556.GF24393@uranus.faber.nom> Message-ID: <61499894-16CC-11D9-8EF8-003065C45FE0@five-lawrences.com> On Oct 4, 2004, at 20:05, Faber Fedor wrote: > On 02/10/04 16:17 -0400, Ricardo SIGNES wrote: >> * Faber Fedor [2004-10-01T18:05:11] >>> I hacked this together from something I read on usenet: >>> >>> my $data_array = $dbh->selectall_arrayref("select id, weight from >>> $table where realdate = '" . $fulldate ."'"); >> >> I'm not sure about the use of $table, but at least consider > > $table is a variable that get's set, just like $fulldate. "Richardo" ;-) may have been wondering if you could also bind the table name, instead of interpolating. If so, I can say I've not know any of the DBD modules I use to allow this. It's dependent on Oracle, MySQL, whatever. >> >> $dbh->selectall_arrayref( >> "SELECT id, weight FROM table WHERE realdate = ?", >> undef, >> $fulldate >> ); >> >> This will use proper quoting to get the date in, and avoid SQL >> injection >> attacks. > > This will put quotes around the date, e.g. the SQL will read like this: > > SELECT id, weight FROM table WHERE realdate = '1993-01-31' > > :-? No, it will "do the right thing," whether it be date or string. Handy! > >> $sum += @{$_->[1]} for @$data_array; > > Now what's the difference between yours and Phils, other than you have > more squigglies in yours? This is wrong, or, more charitably, advanced and opaque. += expects a scalar number, and treating the 1st element of the current aref as an aref itself is not... intuitive. If I recall, this element was in fact a scalar in your example anyway. IIRC, if it really was an aref, this would return the last element of the aref to the += operator. Better to rely on the warnings pragma to alert you that it's not a scalar by simply referencing it as such. > >> open my $output, '>', $outfile; >> >> This creates a lexical filehandle-reference, which will autoclose when >> it goes out of scope. > > That's cute. Indeed. My code is now jealous. :-) prl From phil at five-lawrences.com Mon Oct 11 08:45:40 2004 From: phil at five-lawrences.com (Phil Lawrence) Date: Mon Oct 11 08:45:37 2004 Subject: [ABE.pm] reminder: tonight at 1900 In-Reply-To: <028AC87C-16C3-11D9-A148-000A95BA93C6@lehigh.edu> References: <20041004153636.C511@manxome.org> <20041004202619.E511@manxome.org> <028AC87C-16C3-11D9-A148-000A95BA93C6@lehigh.edu> Message-ID: See you all tonight! prl From faber at linuxnj.com Mon Oct 11 22:50:02 2004 From: faber at linuxnj.com (Faber Fedor) Date: Mon Oct 11 22:50:04 2004 Subject: [ABE.pm] Perl Expect modules? Message-ID: <20041012035002.GA9272@uranus.faber.nom> I'm putting together a (coupla?) system(s) that involves ftp downloads using Expect, database loads using (ugh!) MySQL's LOAD DATA INFILE and some Perl scripts using DBI to modify data before loading it. For the previous two, I'm using bash to tie things together (or in one case, using Perl as a shell program). I'm wondering (and asking youse guys) if there is a Perl module that does what Expect does, e.g. handles interactive programs like ftp. Also, is there a better interface to RDBMS' than DBI or Classes::DBI (e.g. that take advantage of LOAD INFILE and other RDBMS features which aren't shell-based)? BTW, how did tonight's meeting go? I learned about it (thanks, Phil!) after I drove back from North Hampton/Northhampton and didn't feel like driving back out to Allentown again. -- Regards, Faber Linux New Jersey: Open Source Solutions for New Jersey http://www.linuxnj.com From rjbs-perl-abe at lists.manxome.org Tue Oct 12 05:57:44 2004 From: rjbs-perl-abe at lists.manxome.org (Ricardo SIGNES) Date: Tue Oct 12 06:00:48 2004 Subject: [ABE.pm] Perl Expect modules? In-Reply-To: <20041012035002.GA9272@uranus.faber.nom> References: <20041012035002.GA9272@uranus.faber.nom> Message-ID: <20041012065744.H25611@manxome.org> * Faber Fedor [2004-10-11T23:50:02] > I'm wondering (and asking youse guys) if there is a Perl module that does > what Expect does, e.g. handles interactive programs like ftp. Also, is http://search.cpan.org/dist/Expect/ > there a better interface to RDBMS' than DBI or Classes::DBI (e.g. that > take advantage of LOAD INFILE and other RDBMS features which aren't > shell-based)? I'm not sure what you mean by "not shell-based." You can perform any function that can be executed through SQL with the DBI. -- rjbs -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 189 bytes Desc: not available Url : http://mail.pm.org/archives/abe-pm/attachments/20041012/2b73b660/attachment.bin From faber at linuxnj.com Wed Oct 13 22:02:50 2004 From: faber at linuxnj.com (Faber Fedor) Date: Thu Oct 14 10:33:00 2004 Subject: [ABE.pm] A coupla' more Qs Message-ID: <20041014030250.GB1795@uranus.faber.nom> 1. I've got a bunch of separate programs that all share similar functions (connectToDB(), etc.). I want to create a common library/file that all of these programs can include. I tried "use" and "require" but the former expects a .pm and the latter doesn't scope the variables the way I want ($dbh in the include file is scoped only inside that file). Do I need to learn how to write a Perl module or is there an easier way? 2. I HATE MAINFRAMERS AND THEIR FIXED-LENGTH RECORDS! (This ML isn't spidered by Google, is it? :-) Does anyone have a good tool for determining where and how long fixed length records are? I've been using the unpack() function and doing a trial and error (thank Ghu for ptkdb!). 3. I need to separate things into a production database and a development database (running under one RDBMS). I'm thinking of requiring a command-line argument; if --db=prodn is passed, the program reads/writes to the production database, otherwise, it R/Ws to the development DB. Maybe I should make it an enviroment variable instead? Any suggestions on how to do this elegantly with my poor :-) Perl skills? 4. Anyone use the ActiveState perl debugger? Is it worth the money? I use ptkdb when I need a GUI debugger, but I can't install that on the production server because A) it's a production server and B) the server is running RHEL3 and is a bit outdated. I'm not too keen on losing up2date on the box, but I may go that route anywho. -- Regards, Faber Linux New Jersey: Open Source Solutions for New Jersey http://www.linuxnj.com From rjbs-perl-abe at lists.manxome.org Thu Oct 14 11:40:42 2004 From: rjbs-perl-abe at lists.manxome.org (Ricardo SIGNES) Date: Thu Oct 14 11:42:15 2004 Subject: [ABE.pm] A coupla' more Qs In-Reply-To: <20041014030250.GB1795@uranus.faber.nom> References: <20041014030250.GB1795@uranus.faber.nom> Message-ID: <20041014124042.D284@manxome.org> * Faber Fedor [2004-10-13T23:02:50] > 1. I've got a bunch of separate programs that all share similar functions > (connectToDB(), etc.). I want to create a common library/file that all > of these programs can include. I tried "use" and "require" but the > former expects a .pm and the latter doesn't scope the variables the way > I want ($dbh in the include file is scoped only inside that file). > > Do I need to learn how to write a Perl module or is there an easier way? Look at the perldoc for "do", specifically "do EXPR" This is ugly, and other programmers will probably scowl if they see it. Writing a Perl module is /really/ easy, and a good thing to learn. VERY VERY roughly, you could get away with this: Assuming your code is in FaberCode.pm, you'd put that code in a directory that's in @INC. You'd put, at the beginning, "package FaberCode;" and, at the end, "1;" Now you can "use FaberCode;" and that package gets defined. If you have a sub "foo" in it, you could call FaberCode::foo(); If you don't want to prefix it with the name, you'd include lines like this, in FaberCode.pm: @FaberCode::ISA = qw(Exporter); @FaberCode::EXPORT = qw(foo); Consult perldoc perlmod, perldoc -f use, perldoc -f package, perldoc Exporter. > 2. I HATE MAINFRAMERS AND THEIR FIXED-LENGTH RECORDS! (This ML isn't > spidered by Google, is it? :-) Does anyone have a good tool for > determining where and how long fixed length records are? I've been using > the unpack() function and doing a trial and error (thank Ghu for > ptkdb!). I have no idea how you'd do that. > 3. I need to separate things into a production database and a > development database (running under one RDBMS). I'm thinking of > requiring a command-line argument; if --db=prodn is passed, the program > reads/writes to the production database, otherwise, it R/Ws to the > development DB. Maybe I should make it an enviroment variable instead? > Any suggestions on how to do this elegantly with my poor :-) Perl > skills? Both. $which_db = $opt{db} || $ENV{DB_FOR_FABER} || 'default'; > 4. Anyone use the ActiveState perl debugger? Is it worth the money? I > use ptkdb when I need a GUI debugger, but I can't install that on the > production server because A) it's a production server and B) the server > is running RHEL3 and is a bit outdated. I'm not too keen on losing > up2date on the box, but I may go that route anywho. I hate hate hate it... not because I use it, but because it makes it basically impossible to use the wonderful standard Perl debugger. I use perldb all the time, and it rules. -- rjbs -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 189 bytes Desc: not available Url : http://mail.pm.org/archives/abe-pm/attachments/20041014/45d33ab6/attachment.bin From faber at linuxnj.com Thu Oct 14 16:09:20 2004 From: faber at linuxnj.com (Faber Fedor) Date: Thu Oct 14 16:09:23 2004 Subject: [ABE.pm] Re: A coupla' more Qs In-Reply-To: <20041014124042.D284@manxome.org> References: <20041014030250.GB1795@uranus.faber.nom> <20041014124042.D284@manxome.org> Message-ID: <20041014210920.GA5722@uranus.faber.nom> On 14/10/04 12:40 -0400, Ricardo SIGNES wrote: > * Faber Fedor [2004-10-13T23:02:50] > > Do I need to learn how to write a Perl module or is there an easier way? > > Look at the perldoc for "do", specifically "do EXPR" > > This is ugly, and other programmers will probably scowl if they see it. > Writing a Perl module is /really/ easy, and a good thing to learn. Yeah, that was easy. And I found the "use lib" command(?) which gotme away from modifying @INC. > > 3. I need to separate things into a production database and a > > development database (running under one RDBMS). I'm thinking of > > requiring a command-line argument; if --db=prodn is passed, the program > > reads/writes to the production database, otherwise, it R/Ws to the > > development DB. Maybe I should make it an enviroment variable instead? > > Any suggestions on how to do this elegantly with my poor :-) Perl > > skills? > > Both. > $which_db = $opt{db} || $ENV{DB_FOR_FABER} || 'default'; Sweet. > I hate hate hate [ActiveState's Perl IDE]... not because I use it, but because it makes it > basically impossible to use the wonderful standard Perl debugger. I use > perldb all the time, and it rules. I think that's the first time I've ever heard "wonderful" and "standard Perl debugger" in the same sentence. :-) I must be missing something. Care to give a demo on practical usage of perldb sometime? -- Regards, Faber Linux New Jersey: Open Source Solutions for New Jersey http://www.linuxnj.com From rjbs-perl-abe at lists.manxome.org Thu Oct 14 17:03:48 2004 From: rjbs-perl-abe at lists.manxome.org (Ricardo SIGNES) Date: Thu Oct 14 17:05:25 2004 Subject: [ABE.pm] Re: A coupla' more Qs In-Reply-To: <20041014210920.GA5722@uranus.faber.nom> References: <20041014030250.GB1795@uranus.faber.nom> <20041014124042.D284@manxome.org> <20041014210920.GA5722@uranus.faber.nom> Message-ID: <20041014180348.F284@manxome.org> * Faber Fedor [2004-10-14T17:09:20] > On 14/10/04 12:40 -0400, Ricardo SIGNES wrote: > > > > This is ugly, and other programmers will probably scowl if they see it. > > Writing a Perl module is /really/ easy, and a good thing to learn. > > Yeah, that was easy. And I found the "use lib" command(?) which gotme > away from modifying @INC. Yeah. Look at ExtUtils::MakeMaker, and the contents of a simple distribution like, say, Games-Die. It's easy to make a distribution for your own code, so you can easily send your code into the right place in @INC every time you need to update the installed code. > > I hate hate hate [ActiveState's Perl IDE]... not because I use it, > > but because it makes it basically impossible to use the wonderful > > standard Perl debugger. I use perldb all the time, and it rules. > > I think that's the first time I've ever heard "wonderful" and "standard > Perl debugger" in the same sentence. :-) I must be missing something. > Care to give a demo on practical usage of perldb sometime? Yes... but not tonight. :) My hands are killing me! Actually, I should try to prepare a good example, too, rather than just blather. -- rjbs -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 189 bytes Desc: not available Url : http://mail.pm.org/archives/abe-pm/attachments/20041014/10361f68/attachment.bin From faber at linuxnj.com Tue Oct 19 10:18:46 2004 From: faber at linuxnj.com (Faber Fedor) Date: Tue Oct 19 10:18:51 2004 Subject: [ABE.pm] rolling a loop Message-ID: <20041019151846.GB16645@uranus.faber.nom> Hi guys, I'm trying to roll a loop (as opposed to "unrolling a loop") but I can't figure out the details. Here's a sample of what I've got: $table="mytable1"; $Vfield="field1"; $dum="sum1"; $col="col1"; $stmt = "update $table set $field = $sum where $col='Y'"; $dbh->do($stmt); $table="mytable2"; $field="field2"; $dum="sum2"; $col="col2"; $stmt = "update $table set $field = $sum where $col='Y'"; $dbh->do($stmt); $table="mytable3"; $field="field3"; $dum="sum3"; $col="col3"; $stmt = "update $table set $field = $sum where $col='Y'"; $dbh->do($stmt); etc. What I want to do is something like this @data = ("mytable1", "field1", "sum1", "col1", "mytable2", "field2", "sum2", "col2", "mytable3", "field3", "sum3", "col3", ); foreach my ($table, $field, $sum, $col) (@data) { $stmt = "update $table set $field = $sum where $col='Y'"; $dbh->do($stmt); } But that doesn't work in Perl. I think I spent too much time in Python. What's te proper Perl idiom/technique to roll this loop up? -- Regards, Faber Linux New Jersey: Open Source Solutions for New Jersey http://www.linuxnj.com From rjbs-perl-abe at lists.manxome.org Tue Oct 19 10:59:37 2004 From: rjbs-perl-abe at lists.manxome.org (Ricardo SIGNES) Date: Tue Oct 19 11:02:35 2004 Subject: [ABE.pm] rolling a loop In-Reply-To: <20041019151846.GB16645@uranus.faber.nom> References: <20041019151846.GB16645@uranus.faber.nom> Message-ID: <20041019115937.C238@manxome.org> * Faber Fedor [2004-10-19T11:18:46] > @data = ("mytable1", "field1", "sum1", "col1", > "mytable2", "field2", "sum2", "col2", > "mytable3", "field3", "sum3", "col3", > ); > > foreach my ($table, $field, $sum, $col) (@data) { > $stmt = "update $table set $field = $sum where $col='Y'"; > $dbh->do($stmt); > } > > But that doesn't work in Perl. I think I spent too much time in Python. > What's te proper Perl idiom/technique to roll this loop up? How would Perl know which fields to break out at once? In Python, you'd probably have a list of lists, or tuple of tuples. Otherwise, you can't look at the datastructure alone and really know what's going on. You could do this: @data = ( [ 'mytable1', 'field1', 'sum1', 'col1' ], [ 'mytable2', ... which I would write like this: @data = ( [ qw(mytable1 field1 sum1 col1) ], [ qw(mytable2 ... (perldoc perlop, quote-like operators) Then: for (@data) { my ($table, $field, $sum, $col) = @$_; ... } Alternately, you could use a hashref, which is closer to what I'd really do. my @data = ( { table => 't1', field => 'f1', sum => 's1', col => 'c1' } ... Then for (@data) { do_something("select $_->{field} FROM $_->{table}"); ... -- rjbs -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 189 bytes Desc: not available Url : http://mail.pm.org/archives/abe-pm/attachments/20041019/4f100210/attachment.bin From phil at five-lawrences.com Thu Oct 21 08:29:24 2004 From: phil at five-lawrences.com (Phil Lawrence) Date: Thu Oct 21 08:29:31 2004 Subject: [ABE.pm] A coupla' more Qs In-Reply-To: <20041014030250.GB1795@uranus.faber.nom> References: <20041014030250.GB1795@uranus.faber.nom> Message-ID: <393117A4-2365-11D9-BF08-003065C45FE0@five-lawrences.com> On Oct 13, 2004, at 23:02, Faber Fedor wrote: > 2. I HATE MAINFRAMERS AND THEIR FIXED-LENGTH RECORDS! (This ML isn't > spidered by Google, is it? :-) Does anyone have a good tool for > determining where and how long fixed length records are? I've been > using > the unpack() function and doing a trial and error (thank Ghu for > ptkdb!). Yes. I recall writing a mainframe data-dictionary parser. If you have access to the data dictionary, I might be able to help out. prl From faber at linuxnj.com Thu Oct 21 10:34:47 2004 From: faber at linuxnj.com (Faber Fedor) Date: Thu Oct 21 10:34:55 2004 Subject: [ABE.pm] Re: A coupla' more Qs In-Reply-To: <393117A4-2365-11D9-BF08-003065C45FE0@five-lawrences.com> References: <20041014030250.GB1795@uranus.faber.nom> <393117A4-2365-11D9-BF08-003065C45FE0@five-lawrences.com> Message-ID: <20041021153447.GB31754@uranus.faber.nom> On 21/10/04 09:29 -0400, Phil Lawrence wrote: > > On Oct 13, 2004, at 23:02, Faber Fedor wrote: > >2. I HATE MAINFRAMERS AND THEIR FIXED-LENGTH RECORDS! (This ML isn't > >spidered by Google, is it? :-) Does anyone have a good tool for > >determining where and how long fixed length records are? I've been > >using > >the unpack() function and doing a trial and error (thank Ghu for > >ptkdb!). > > > Yes. I recall writing a mainframe data-dictionary parser. If you have > access to the data dictionary, I might be able to help out. "access to the data dictionary"?! You kids expect everything handed to you on a silver platter! :-) All I had was the data file and had to figure out where one field started and ended. Manually counting across 200 columns is fraught with errors and I ended up running the program several times going "tweak this field over one", run the program "tweek the next field over two", etc. PITFA! -- Regards, Faber Linux New Jersey: Open Source Solutions for New Jersey http://www.linuxnj.com From faber at linuxnj.com Thu Oct 21 14:34:25 2004 From: faber at linuxnj.com (Faber Fedor) Date: Thu Oct 21 14:34:28 2004 Subject: [ABE.pm] business day calculations Message-ID: <20041021193425.GA32726@uranus.faber.nom> How do you guys do business day calculations, i.e. what is the last business day of the month for 2004-02 ('27' if you don't want to look it up)? The only thing I've come across so far is Bob Orlando's routines (http://www.orlandokuntao.com/mf_holidays.html). I find it a little strange his routines aren't in CPAN or that I can't find anything in CPAN to handle this. -- Regards, Faber Linux New Jersey: Open Source Solutions for New Jersey http://www.linuxnj.com From rjbs-perl-abe at lists.manxome.org Thu Oct 21 14:41:46 2004 From: rjbs-perl-abe at lists.manxome.org (Ricardo SIGNES) Date: Thu Oct 21 14:45:19 2004 Subject: [ABE.pm] business day calculations In-Reply-To: <20041021193425.GA32726@uranus.faber.nom> References: <20041021193425.GA32726@uranus.faber.nom> Message-ID: <20041021154146.A26930@manxome.org> * Faber Fedor [2004-10-21T15:34:25] > How do you guys do business day calculations, i.e. what is the last > business day of the month for 2004-02 ('27' if you don't want to look it > up)? > > The only thing I've come across so far is Bob Orlando's routines > (http://www.orlandokuntao.com/mf_holidays.html). > > I find it a little strange his routines aren't in CPAN or that I can't > find anything in CPAN to handle this. That code makes me want to stab someone. Surely there is something for this on the CPAN. Once search.cpan.org is back up, I'll have a look, but I'd wager that there is either a DateTime::* module, or that Date::Calc can easily do it. -- rjbs -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 189 bytes Desc: not available Url : http://mail.pm.org/archives/abe-pm/attachments/20041021/08835457/attachment.bin From faber at linuxnj.com Thu Oct 21 14:59:36 2004 From: faber at linuxnj.com (Faber Fedor) Date: Thu Oct 21 14:59:38 2004 Subject: [ABE.pm] Re: business day calculations In-Reply-To: <20041021154146.A26930@manxome.org> References: <20041021193425.GA32726@uranus.faber.nom> <20041021154146.A26930@manxome.org> Message-ID: <20041021195936.GA2507@uranus.faber.nom> On 21/10/04 15:41 -0400, Ricardo SIGNES wrote: > * Faber Fedor [2004-10-21T15:34:25] > > How do you guys do business day calculations, i.e. what is the last > > business day of the month for 2004-02 ('27' if you don't want to look it > > up)? > > > > The only thing I've come across so far is Bob Orlando's routines > > (http://www.orlandokuntao.com/mf_holidays.html). > > > > I find it a little strange his routines aren't in CPAN or that I can't > > find anything in CPAN to handle this. > > That code makes me want to stab someone. On top of that, it calls modules that can't be googled for. OTOH, his awk script does work. > Surely there is something for this on the CPAN. That's what I thought, but looking through the module list, nothing jumped out at me. > Once search.cpan.org is back up, I'll have a look, > but I'd wager that there is either a DateTime::* module, or that > Date::Calc can easily do it. I'll RTFM on those two... -- Regards, Faber Linux New Jersey: Open Source Solutions for New Jersey http://www.linuxnj.com From faber at linuxnj.com Thu Oct 21 15:18:38 2004 From: faber at linuxnj.com (Faber Fedor) Date: Thu Oct 21 15:18:41 2004 Subject: [ABE.pm] Re: business day calculations In-Reply-To: <20041021195936.GA2507@uranus.faber.nom> References: <20041021193425.GA32726@uranus.faber.nom> <20041021154146.A26930@manxome.org> <20041021195936.GA2507@uranus.faber.nom> Message-ID: <20041021201838.GB2507@uranus.faber.nom> On 21/10/04 15:59 -0400, Faber Fedor wrote: > On 21/10/04 15:41 -0400, Ricardo SIGNES wrote: > > Once search.cpan.org is back up, I'll have a look, > > but I'd wager that there is either a DateTime::* module, or that > > Date::Calc can easily do it. the DateTime FAQ (http://datetime.perl.org/faq.html) sez : go to the last_day_of_month and counts backwards. my $dt = DateTime->last_day_of_month( year => 2003, month => 8 ); # day 6 is Saturday, day 7 is Sunday while ( $dt->day_of_week >= 6 ) { $dt->subtract( days => 1 ) } print "Payday is ", $dt->ymd, "\n"; It'll do. -- Regards, Faber Linux New Jersey: Open Source Solutions for New Jersey http://www.linuxnj.com From faber at linuxnj.com Thu Oct 21 22:19:43 2004 From: faber at linuxnj.com (Faber Fedor) Date: Thu Oct 21 22:19:45 2004 Subject: [ABE.pm] So... Message-ID: <20041022031943.GA3458@uranus.faber.nom> What are your views on Perl 6? I've read a few of the early Apocalypses and, quite frankly, they were over my head. I've heard that Perl 6 will break Perl 5, i.e. everything will have to be re-writen (sp?) to work with P6. Discuss. -- Regards, Faber Linux New Jersey: Open Source Solutions for New Jersey http://www.linuxnj.com From rjbs-perl-abe at lists.manxome.org Fri Oct 22 05:33:42 2004 From: rjbs-perl-abe at lists.manxome.org (Ricardo SIGNES) Date: Fri Oct 22 05:37:26 2004 Subject: [ABE.pm] So... In-Reply-To: <20041022031943.GA3458@uranus.faber.nom> References: <20041022031943.GA3458@uranus.faber.nom> Message-ID: <20041022063342.D26930@manxome.org> * Faber Fedor [2004-10-21T23:19:43] > What are your views on Perl 6? > > I've read a few of the early Apocalypses and, quite frankly, they were > over my head. Perl 6 is a major improvement, in a lot of ways. It provides better support for objects. It has many higher-order language features that are not commonly available in other popular languages. It fixes many of Perl 5's limitations and makes it even easier to write even less code that does even more. (I could expound on some of these, but the Apocalypses do it better. If they're to mind-blowing, look for the Synopses or Exegeses.) Perl 6 is a good improvement to the language. It just has one major problem: it is a hypothetical language that does not exist. It has been years since the project began, and it will be years before an alpha-quality p6 interpreter exists. (Parrot has been a nice by-product, though!) Until then, spending too much time thinking about p6 is a waste, like learning to speak Lojban or how to play zero-g basketball. > I've heard that Perl 6 will break Perl 5, i.e. everything will have to > be re-writen (sp?) to work with P6. No, Perl 6 will be able to interpret Perl 5. However, they are not compatible languages. That is: the p6 interpreter will know how to interpret p5, but you won't be able to take your crufty old p5 program and add a few lines of Perl 6 to it. If you want to move to Perl 6, you will need to move to Perl 6. You can't just stick a hyperoperator in the middle of an old-style sub (except for the cases where you can). -- rjbs -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 189 bytes Desc: not available Url : http://mail.pm.org/archives/abe-pm/attachments/20041022/427649de/attachment.bin