0

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;
  • 1
    Please show the program you are running as well, and a directory listing. Use `tree` or something similar to make that listing so we can see what's where. – simbabque Jul 23 '19 at 11:17
  • I edited it, I hope it is good enough –  Jul 23 '19 at 11:43
  • Works for me. `› find|grep pm|parallel perl -c`␤`./MyApp/Schema.pm syntax OK`␤`./MyApp/Schema/Result/CD.pm syntax OK`␤`./MyApp/Schema/Result/Artist.pm syntax OK` – daxim Jul 23 '19 at 12:03
  • Where is the script relative to the module? How do you tell Perl where to look for the module? [Is this relevant?](https://stackoverflow.com/q/46549671/589924) – ikegami Jul 23 '19 at 12:17
  • Sorry man, I really don't know how to answer to this question. I am new in creating the back-end side for a web page so I don't know what I am doing. If you know a good tutorial where I can learn all of these I will be grateful. –  Jul 23 '19 at 13:13
  • Let me phrase is differently. What is the full path to the program/script you are running, and what is the full path to `Schema.pm`? – ikegami Jul 23 '19 at 14:35
  • I used the command realpath in terminal and the path to the application directory is /home/alex/MyApp and the path to Schema.pm is /home/alex/Schema.pm –  Jul 23 '19 at 14:38

1 Answers1

3

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.

Dave Cross
  • 68,119
  • 3
  • 51
  • 97
  • Thank you, but it doesn't work . I used use lib '.' and use FindBin '$RealBin'; use lib $RealBin; but I am getting the same error : https://pastebin.com/PdYyVesc . (I inserted in CD.pm exactly use FindBin '$RealBin'; use lib $RealBin; on the top of the file, I don't know if I must to modify the RealBin with someting else). I am not that new in programming, I use C++ and Java before Perl to make problems, use oop and datebase with postgresql, I understand a little bit of what create the error but I don't know how to solve it. –  Jul 23 '19 at 14:15
  • 1
    Re "*I inserted in CD.pm*", No, in the main program file – ikegami Jul 23 '19 at 14:36
  • 2
    DO NOT USE `use lib '.';`. It's buggy and a security risk. – ikegami Jul 23 '19 at 14:37
  • @AlexUnLimited: If your problem is that Perl can't find your modules, then it's really hard to see how making a change to one of your modules is going to improve the situation. As I said in my answer, you need to add the code to your program, not the module. – Dave Cross Jul 23 '19 at 14:54
  • Sorry guys because I wasted your time, I don't even have a main file in the projects, I was running file by file just to see if everything is ok, I think that is the problem. –  Jul 23 '19 at 15:05