Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

G'day SpaceCowboy,

You have a lot of mistakes in your code. Here's a selection:

  • There is no dbi module on CPAN. You probably meant DBI; if not, provide a link to this dbi module.
  • Don't use smart quotes. You'll run into all sorts of problems; e.g.
    $ perl -MO=Deparse -e '$dbh->do("alter session set current_schema = th +e current schema”);' Can't find string terminator '"' anywhere before EOF at -e line 1. $ perl -MO=Deparse -e 'print “third script executed”;' Unrecognized character \xE2; marked by <-- HERE after print <-- HERE n +ear column 7 at -e line 1.
  • You used $dbh once; then left out the sigil in a multitude of places thereafter.
  • You have ttemp in one place; that should probably be temp.

We all make typos but, with this many, you're quite likely to spend more time debugging than coding. Pay more attention to the code you're writing: don't make a rod for your back.

"Any general wisdom here?"

The following is intended to be very generic. There's not enough up-front information for me to provide working code. Adapt what I have here for your specific needs.

I'd probably start with a front-end script that might look something like:

#!/usr/bin/env perl use strict; use warnings; use SpaceCowboy::Database::Module; my $database_credentials = { ... }; my $sdm = SpaceCowboy::Database::Module::->new($database_credentials); $sdm->$_() for qw{primary first second third};

I don't know what you had in mind for "***import database credentials here"; perhaps Getopt::Long, a config file, or something else. I'll leave how you populate $database_credentials up to you.

In a separate file (.../SpaceCowboy/Database/Module.pm), code SpaceCowboy::Database::Module which does all the work. Here's a rough example of what that might look like:

package SpaceCowboy::Database::Module; use 5.010; use strict; use warnings; use DBI; sub new { my ($class, $params) = @_; return bless $params => $class; } { my $DBH; sub dbh { my ($self) = @_; $DBH //= $self->_connect(); } } sub _connect { my ($self) = @_; # Use credentials in DBI->connect(...); # e.g. $self->host(), $self->username(), etc. return DBI->connect(...); } sub primary { my ($self) = @_; $self->dbh()->do("alter session ..."); return; } sub first { my ($self) = @_; $self->dbh()->do("create ..."); $self->dbh()->do("insert ..."); return; } # ditto for second() and third() 1;

I've included a very basic new() method; however, you'll probably find one of the many frameworks available are easier to work with — Moo might be a good one to use here. See also: "perlintro: OO Perl" and perlootut.

The database handle, $DBH, is lexically scoped such that only dbh() can access it directly. It is only ever set once.

You'll need to look at the DBD::whatever documentation, for whatever database you intend to use, to find out the correct way to call DBI->connect(...).

"how can I serialize the execution?"

You could use flags for each of primary(), first(), etc. Set these to indicate which parts have completed successfully; do not rerun the flagged parts. You'll possibly need a reset() method, to turn these off, so that a fresh run from the start is possible.

"Is it possible to write multiple statements like dbh->do("create temp schema", "insert table");"

That may be dependent on the database you're using. You could write:

$self->dbh()->do($_) for @statements;

Be aware that might be problematic if you're also trying to flag the success of individual statements.

Ask yourself why you think you'd need this. If you can't come up with a good reason, don't do it.

— Ken


In reply to Re: designing a program - your wisdom needed by kcott
in thread designing a program - your wisdom needed by SpaceCowboy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (1)
As of 2024-04-25 00:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found