Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

oracle connection

by johnpat123 (Initiate)
on Jun 01, 2012 at 17:41 UTC ( #973825=perlquestion: print w/replies, xml ) Need Help??
johnpat123 has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am trying to create a sub procedure which will connect to an oracle db and select some data. The issue is that the data it retrieves is different each time. Meaning the sql string changing based on the parameters passed into this procedure. so the following commented out 'while' line works, but it is hard coded. Is there a way to put a variavle in fetch whch can contain different variables each time?


#while (my ( $DBNAME,$USERNAME,$PROFILE )=$sth->fetchrow())

while (my ( '$xxx' )=$sth->fetchrow())




Replies are listed 'Best First'.
Re: oracle connection
by Eliya (Vicar) on Jun 01, 2012 at 18:00 UTC

    You could put them in a hash. For example, pass the names to the routine (e.g. proc( [qw(foo bar)] )), and then say in the routine

    my $names = shift; ... @v{@$names} = $sth->fetchrow()

    (The @v{...} is a hash slice, which means you can assign multiple entries in one go.)

    You can then access the "variables" as $v{foo} and $v{bar}.

      You can also directly ask DBI to return you a hash:

      my $hash = $sth->fetchrow_hashref();

        Sure you can (presuming the query produces the desired column names).

        I was mainly trying to answer the general case, i.e. the typical "beginner's" problem of wanting to dynamically create variables like

        $xxx = "$DBNAME,$USERNAME,$PROFILE"; my ( '$xxx' ) = ( ... );

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://973825]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (1)
As of 2017-08-18 00:40 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (294 votes). Check out past polls.