0

I'm new to both Perl and its feature of .lib.

I made this simple subroutine and saved it as a file with an extension of .lib

sub shorterline {   
print "Content-type: text/html\n\n";
}

1;

As I tried to insert the subroutine into the Perl file with an extension of .cgi below, it doesn't work somehow:

#!/usr/bin/perl
require 'mysubs.lib'; 


&shorterline; 
print "Hello, world!";

I gave the .cgi the chmod permission, but the .cgi still doesn't work, what seem to be the problem ?

simbabque
  • 53,749
  • 8
  • 73
  • 136
  • What feature of .lib are you refering to? Do you mean `use lib "/some/path";` ? – xxfelixxx Nov 21 '17 at 09:19
  • http://perldoc.perl.org/lib.html – xxfelixxx Nov 21 '17 at 09:20
  • 3
    _", but the .cgi still doesn't work"_ that does not mean much, does not work how? it gives errors? It does not give errors? it does not stir the coffee? – Gerhard Nov 21 '17 at 09:20
  • Welcome to Stack Overflow and the Perl tag. It seems you are learning Perl from a _very_ out of date resource. Please take a look at the [tag wiki](https://stackoverflow.com/tags/perl/info), where a lot of good, modern tutorials are mentioned. Next, please consider that CGI is an obsolete technology. It sure still works, but if you want to do something serious with the web, there are technologies that work better and make your life easier as a developer. I suggest to watch [this talk](https://youtu.be/jKOqtRMT85s) by the current release manager of the Perl programming language. – simbabque Nov 21 '17 at 09:24
  • 1
    To address the problem you are facing... as a debugging aid, try to run your script from the command line with `perl` (that's the interpreter, while _Perl_ is the name of the language). The convention regarding library modules is to name them with a _.pm_ ending, and put a `package` declaration inside, so your code [in the module](http://perldoc.perl.org/perlmod.html) has a different namespace. You _can_ `require` other scripts, but that is a practice left over from Perl 4, which was 20 years ago. Don't do that. Write proper code instead. – simbabque Nov 21 '17 at 09:28
  • 1
    And as you seem to try to invent an implementation of your own CGI, look at [the CGI module](http://metacpan.org/pod/CGI) on [CPAN](https://en.wikipedia.org/wiki/CPAN). As [the talk](https://youtu.be/jKOqtRMT85s) mentioned above said, you shouldn't use it. But what you're doing is even worse. – simbabque Nov 21 '17 at 09:58
  • 1
    @simbabque you've managed Sawyer to sound like [cartoon character](https://www.youtube.com/watch?v=rLBehW71kAQ). – mpapec Nov 21 '17 at 12:33
  • See [Doesn't Perl include current directory in @INC by default?](https://stackoverflow.com/q/46549671/589924) – ikegami Nov 21 '17 at 15:49

1 Answers1

0

Your descriptions of what the problem is are rather unclear.

it doesn't work somehow

the .cgi still doesn't work

Without knowing what problems you're seeing, it's hard to know what the problem is. But I tried copying your code and running the program from the command line and I got this error message:

Can't locate mysubs.lib in @INC (@INC contains: ...)

So I think you are using a recent version of Perl and are running up against this change:

Removal of the current directory (".") from @INC

The perl binary includes a default set of paths in @INC. Historically it has also included the current directory (".") as the final entry, unless run with taint mode enabled (perl -T). While convenient, this has security implications: for example, where a script attempts to load an optional module when its current directory is untrusted (such as /tmp), it could load and execute code from under that directory.

Starting with v5.26, "." is always removed by default, not just under tainting. This has major implications for installing modules and executing scripts.

If this is the problem, then you can fix it by adding the script's directory to @INC as follows:

use FindBin qw( $RealBin );
use lib $RealBin;

before your call to require. If that doesn't solve your problem, perhaps you would consider sharing a little more detail about the problems that you are experiencing.

Community
  • 1
  • 1
Dave Cross
  • 68,119
  • 3
  • 51
  • 97