LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-15-2009, 09:38 AM   #1
donnied
Member
 
Registered: Oct 2006
Distribution: Debian x64
Posts: 198

Rep: Reputation: 30
Perl: How to do a foreach for elements in a database?


For Perl I need to understand a few things. It's unlikely that anyone will be able to give me direct answers but if they could point out where to look that would be _very_ helpful.

I'm trying to hack down an existing script http://eoylpac.org/source/eoy.pl.
What I need to be able to do (in a nut shell):
1. Open a database
2. Read the values for each uniq id and execute a subroutine for each of those values.
(The subroutine already exists in the above file 'do_pdf').
 
Old 07-15-2009, 04:17 PM   #2
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 59
If you don't know Perl at all, then the first thing you need to do is learn the basics of it. Take a look at either Learning Perl or Beginning Perl, and spend a weekend on that. Beginning Perl even has a chapter on the basics of working with databases using Perl. (It generally means using the DBI module.)
 
Old 07-16-2009, 02:04 AM   #3
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Rep: Reputation: 58
Check out this link http://www.perl.com/pub/a/1999/10/DBI.html
 
Old 07-16-2009, 02:18 AM   #4
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,244

Rep: Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024
See also

http://www.perlmonks.org/?node=Tutorials
http://www.perlmonks.org/?node_id=284436
http://search.cpan.org/~timb/DBI-1.609/DBI.pm
 
Old 07-16-2009, 03:28 PM   #5
donnied
Member
 
Registered: Oct 2006
Distribution: Debian x64
Posts: 198

Original Poster
Rep: Reputation: 30
perl dbi pdf

Unfortunately, I don't really have the time to do a good job of learning Perl at this moment. I have hobbled together a script with DBI:
Code:
#!/usr/bin/perl -wT
        use DBI;
        use PDF::Create;
        use CGI::Carp qw( fatalsToBrowser );
        use CGI qw( :standard );

        my $db = 'test';
        my $host = 'test-mysql';
        my $db_user = '';
        my $db_password = '';
        my $cid = 1;
        $dbh = DBI -> connect ( "dbi:mysql:$db:$host", "$db_user", "$db_password",
                        { RaiseError => 1, AutoCommit => 0});

        $sth = $dbh->prepare("SELECT id FROM don_eoy");
        $sth->execute( );

        while ( @row = $sth->fetchrow_array() ) {
        &do_pdf( $row[0] );
        print( $row[0] );
        }



sub do_pdf
The sub do_pdf routine is too long to post. It seems to work but it's streaming and not creating the individual pdfs. I can redirect the output to a file which contains all the various pdfs in one file (which is problematic).

The code should produce output files. As it was (a response to a click on a webpage) it produced the individual files, but now in my attempt to create a script which bulk creates pdfs it does not.
pertinent snippet:
Code:
use PDF::Create;
        my $file_name = qq{$student_last_name\_$student_first_name.pdf};

        print header(   -type => 'application/x-pdf',
                                -attachment => $file_name );

        my $pdf = new PDF::Create(      'filename'      => '-', # Stdout
                                                        'Version'       => 1.2,
                                                        'PageMode'      => 'UseOutlines',
                                                        'Author'        => 'Don ',
                                                        'Title' => 'Some Title',
 
Old 07-17-2009, 02:07 PM   #6
donnied
Member
 
Registered: Oct 2006
Distribution: Debian x64
Posts: 198

Original Poster
Rep: Reputation: 30
My problem was:
Code:
my $pdf = new PDF::Create(      'filename'      => '-',
which sent everything to stdout. What I needed was:
Code:
 my $file_name = qq{$student_last_name\_$student_first_name.pdf};
 print header(   -type => 'application/x-pdf',
                  -attachment => $file_name );

my $pdf = new PDF::Create(      'filename'      => $file_name,
                                                        'Version'       => 1.2,
                                                        'PageMode'      => 'UseOutlines',
                                                                                                        );
 
  


Reply

Tags
database, foreach, perl


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
trimming perl array elements homey Programming 7 02-17-2008 03:48 PM
PERL - foreach array not clearing michael.barnes Programming 8 08-21-2007 09:37 PM
perl - get number of elements in an array AM1SHFURN1TURE Programming 3 03-07-2005 03:59 PM
Perl: Where am I in a foreach loop? jrtayloriv Programming 3 01-30-2005 10:43 PM
perl:foreach katana Programming 3 07-24-2001 01:05 AM


All times are GMT -5. The time now is 05:48 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration