FAQ

On 05/01/2016 09:12 AM, Mathieu Gagnonn wrote:
Hello James,

You can try :database($db)
Look at this http://design.perl6.org/S02.html#Adverbial_Pair_forms
I've found it very useful!


James E Keenan writes:
Yesterday I made my first attempt at using any library in a Perl6
program -- specifically, DBIish.

The following succeeded in establishing a connection to a Postgresql
database named 'hierarchy' on the same disk as Perl6:

#####
$ cat dbiish_connect.pl6
#!/usr/bin/env perl6
use DBIish;

my $dbh = DBIish.connect("Pg", :database<hierarchy>);

say "Database: connection ", ($dbh.ping ?? "established" !! "not
established");

$ perl6 dbiish_connect.pl6
Database: connection established
#####

Since I have several different Postgresql databases on disk, I want to
be able to store a DB's name in a variable, then have DBIish.connect
interpolate that variable while establishing a connection. I tried:

#####
$ cat dbiish_connect_dynamic.pl6
#!/usr/bin/env perl6
use DBIish;

my $db = 'hierarchy';
my $dbh = DBIish.connect("Pg", :database<$db>);

say "Database $db: connection ", ($dbh.ping ?? "established" !! "not
established");
#####

But I only got this error output:

#####
$ perl6 dbiish_connect_dynamic.pl6
DBDish::Pg: Can't connect: FATAL: database "$db" does not exist
(1)
in block at
/home/jkeenan/rakudo-star/share/perl6/site/sources/3EA876C6AC912AF3692CEF8EEF5E9033C03F7507
(DBIish) line 41
in any at
/home/jkeenan/.perl6/precomp/D7820A4EF6D97B780F45CAC4B50C4E6E59589690.1462056119.68849/3E/3EA876C6AC912AF3692CEF8EEF5E9033C03F7507
line 1
in method connect at
/home/jkeenan/rakudo-star/share/perl6/site/sources/C2BC378F86912AB748EF3CF51FBE6E3AE0CFE0EA
(DBDish::Pg) line 79
in method connect at
/home/jkeenan/rakudo-star/share/perl6/site/sources/3EA876C6AC912AF3692CEF8EEF5E9033C03F7507
(DBIish) line 45
in block <unit> at dbiish_connect_dynamic.pl6 line 5
#####

I also tried:

#####
$ cat dbiish_connect_dynamic_2.pl6
#!/usr/bin/env perl6
use DBIish;

my $db = 'hierarchy';
my $dbh = DBIish.connect("Pg", ":database<$db>");

say "Database $db: connection ", ($dbh.ping ?? "established" !! "not
established");
#####

But that gave me a different error:

#####
$ perl6 dbiish_connect_dynamic_2.pl6
Too many positionals passed; expected 2 arguments but got 3
in method connect at
/home/jkeenan/rakudo-star/share/perl6/site/sources/3EA876C6AC912AF3692CEF8EEF5E9033C03F7507
(DBIish) line 29
in block <unit> at dbiish_connect_dynamic_2.pl6 line 5
#####

Why does variable interpolation not work here? What part is my doing
something wrong? What part is a limitation in the DBIish library?

Thank you very much.
Jim Keenan

Thanks; that worked:

#####
cat dbiish_connect_dynamic_3.pl6
#!/usr/bin/env perl6
use DBIish;

my $db = 'hierarchy';
my $dbh = DBIish.connect("Pg", :database($db));

say "Database $db: connection ", ($dbh.ping ?? "established" !! "not
established");

$ perl6 dbiish_connect_dynamic_3.pl6
Database hierarchy: connection established
#####

Search Discussions

Discussion Posts

Previous

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 4 of 5 | next ›
Discussion Overview
groupperl6-users @
categoriesperl
postedMay 1, '16 at 12:59p
activeMay 1, '16 at 1:57p
posts5
users4
websiteperl6.org

People

Translate

site design / logo © 2021 Grokbase