sunmoon
sunmoon

Reputation: 31

perl byte code generation with too many file.pl

How to make perl bytecode if sub is there in another file.pl so that I can get all perl script in to binary to give for usage but I am getting codedump warning.

Here is the example how I have done!

File: add.pl

require "util.pl";

$a = 1;

$b = 2;

$res = add($a,$b);

print $res;

File: util.pl

sub add()

{

my ($a,$b) = @_;


my $c = $a + $b;

return $c;

}

1; #to return true

Then when I run:

perlcc add.pl

./a.out

I get

Segmentation fault (core dumped)

I also tried

perlcc add.pl util.pl

but it says

/usr/bin/perlcc: using add.pl as input file, ignoring util.pl

Note:

If both are in single file

perlcc file.pl 

and

./a.out 

will work

Upvotes: 0

Views: 252

Answers (2)

Dave Cross
Dave Cross

Reputation: 69314

perlcc was removed from Perl in version 5.10.0 (almost five years ago). The perldelta manual page has this to say:

perlcc, the byteloader and the supporting modules (B::C, B::CC, B::Bytecode, etc.) are no longer distributed with the perl sources. Those experimental tools have never worked reliably, and, due to the lack of volunteers to keep them in line with the perl interpreter developments, it was decided to remove them instead of shipping a broken version of those.

Seeing that, I have to suggest that using perlcc with any version of Perl is probably a rather bad idea. It was an experimental feature that never really worked. You probably want to move away from using it.

Upvotes: 0

Joel Berger
Joel Berger

Reputation: 20280

I cannot answer for the actual compiler problem, but let me make a few notes.

<Edit> the more I look at this, the more I think that the problem is the namespacing of the add function. When they are in the same file, the function is declared in the main namespace. I think that would be true of the require-d file too, since there was not package declaration. Either way, these are still some good notes that I hope help. </Edit>

  1. You really should use strict and warnings pragmas
  2. You shouldn't use $a and $b, because they are semi-magical in Perl and should be avoided (yeah, thats a weird one)
  3. Perl prototypes are not the same as most languages, and even then the empty prototype () on your add function is incorrect, best to leave it off

Those things said here is how I would format my files.

File: add.pl

use strict;
use warnings;

use MyUtils;

my $x = 1;

my $y = 2;

my $res = add($a,$b);

print $res;

File: MyUtils.pm

package MyUtils;

use strict;
use warnings

use parent 'Exporter';
our @EXPORT = ('add');

sub add

{

my ($x,$y) = @_;


my $c = $x + $y;

return $c;

}

1;

This uses the more modern module/package formalism for reusable libraries. The use directive contains a require directive, but does it at compile-time rather than run-time.

The Exporter module (and the @EXPORT variable) correctly import the function into the script's namespace (typically main).

Perhaps perlcc will like these changes better; but even if not, these are good practices to get used to.

Upvotes: 1

Related Questions