http://www.perlmonks.org?node_id=1081798


in reply to Re: RFC: Name and/or API for module ("Type::FromData") ( infer sql best guess cast type match sqltranslator )
in thread RFC: Name and/or API for module ("Type::FromData")

You can create a sub in Perl called foo*bar. (Yes, including the asterisk.) You can't do it the normal way like this, because that's bad syntax:

sub foo*bar { return 42; } print foo*bar(), "\n";

But all is fine and dandy if you quote the name properly (i.e. use symbolic references):

my $name = "foo*bar"; *$name = sub { return 42; }; print &$name(), "\n";

Similarly, PostgreSQL is perfectly happy for you to name a column "when". It's just that the syntax of SQL requires you to quote it.

$ psql tai psql (9.1.13) Type "help" for help. tai=# CREATE TABLE "foo" ("when" varchar, "select" varchar, "update" v +archar); CREATE TABLE tai=# INSERT INTO "foo" VALUES ('a','b','c'); INSERT 0 1 tai=# SELECT * FROM "foo"; when | select | update ------+--------+-------- a | b | c (1 row) tai=# DROP TABLE "foo"; DROP TABLE tai=#

(For MySQL, the quote character is ` instead of ", though MySQL does have an option to let it speak proper, grown-up SQL.)

If you're writing code that needs to deal with an unknown database schema, you should always quote SQL identifier names (e.g. table names, column names) because you don't know what kind of crazy stuff is going to get thrown at you.

use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name
  • Comment on Re^2: RFC: Name and/or API for module ("Type::FromData") ( infer sql best guess cast type match sqltranslator )
  • Select or Download Code

Replies are listed 'Best First'.
Re^3: RFC: Name and/or API for module ("Type::FromData") ( infer sql best guess cast type match sqltranslator )
by chacham (Prior) on Apr 10, 2014 at 18:52 UTC
    Quoting retains case, meaning it will have to be quoted when used, which can be quite inconvenient and very confusing.