I am trying to connect to a database using perl. I tried to follow this tutorial https://metacpan.org/pod/release/RIBASUSHI/DBIx-Class-0.082840/lib/DBIx/Class.pod and create a simple app named MyApp as they made and create the files exactly as they did but when I compile the CD.pm file I am getting the error Can't locate MyApp/Schema.pm in @INC (you may need to install the MyApp::Schema module).
The program is looking like that in Atom using package Script to run the code on ubuntu:
MyApp---
| |
| Schema--
| |
| Result-----
| | |
| Artist.pm CD.pm
|
Schema.pm
#Schema.pm
package MyApp::Schema;
use base qw/DBIx::Class::Schema/;
__PACKAGE__->load_namespaces();
1;
#CD.pm
package MyApp::Schema::Result::CD;
use base qw/DBIx::Class::Core/;
__PACKAGE__->load_components(qw/InflateColumn::DateTime/);
__PACKAGE__->table('cd');
__PACKAGE__->add_columns(qw/ cdid artistid title year /);
__PACKAGE__->set_primary_key('cdid');
__PACKAGE__->belongs_to(artist => 'MyApp::Schema::Result::Artist', 'artistid');
1;
#Artist.pm
package MyApp::Schema::Result::Artist;
use base qw/DBIx::Class::Core/;
__PACKAGE__->table('artist');
__PACKAGE__->add_columns(qw/ artistid name /);
__PACKAGE__->set_primary_key('artistid');
__PACKAGE__->has_many(cds => 'MyApp::Schema::Result::CD', 'artistid');
1;
I am new in creating the back-end side for a web page so I don't know what I am doing
If that's the case, then I really think that building an application that uses DBIx::Class is a bit of a stretch for your first attempt. I'd recommend spending some time getting to grips with writing command-line programs with Perl first.
But that might not be possible. In which case, read on...
You haven't given us the full error message that you're getting (and, for future reference, that would have been really useful) but I suspect that it goes on to tell you the list of directories that are currently in @INC
.
When Perl loads a module, it needs to know where it might find the file that contains that module. It does that by using a special array variable called @INC
. @INC
contains a list of all of the directories that Perl should search to find module code. There are a number of directories that are "baked-in" to your Perl installation when it is set up, but there are various ways to add to that list.
Your error message will have given you the current list of directories in @INC
, but you can also get it from this simple command-line Perl program:
perl -E'say for @INC'
That list contains the "baked-in" directories that I mentioned before. These are the standard directories where Perl expects to find modules. But the modules that are part of your application (the MyApp::Schema
modules) aren't stored in those directories. They are in the same directory tree as your program.
In older versions of Perl, the current directory was automatically added to @INC
(you would see '.' included in the output of the program I mentioned above) but that was seen as a security risk so it was removed (that's why ikegami pointed you to this answer).
So we need to add the current directory to @INC
. We can add directories to @INC
using the lib pragma. The easiest option would be to add this line near the top of your program (before the use MyApp::Schema
line):
[Note: The current directory was removed from the default @INC
for VERY GOOD REASONS. And the line of code below just undoes the good work that the Perl development team did by removing it. As I say above, this is certainly the easiest option, but it's also a really stupid idea.]
# Don't do this. Use the next code block instead.
use lib '.';
But that's not going to work if you run your program from somewhere other than the directory where it lives. So a more robust solution is:
use FindBin '$RealBin';
use lib $RealBin;
If that doesn't work, then it might be that you have put your libraries in a different directory structure and we'll need more information.