2

I want to create a library with methods A(), B(), and C() which will be called hundreds of times in each perl program with different strings as parameters. Each method updates its respective array (@A, @B, and @C) so I don't want to pass the array by reference each time. Each perl program also some unique strings they add to the array, so I cannot have the array be created and only modified by the package.

Is it possible to manipulate a shared array from the main program in a package without having to pass it through a subroutine? Is there a way to set an alias array or array pointer to reference the array in main?

main.pl

use MyPackage;
our @a;

MyPackage.pm

package MyPackage;
$alias_a = @main::a;
push @alias_a, "1";

OR

$aref = \@main::a;
push @$aref, "1";

edit: updated the OP with more background information.

Stuart
  • 1,733
  • 4
  • 21
  • 34
  • 1
    [The XY problem](https://en.wikipedia.org/wiki/XY_problem) – Ted Lyngmo Jun 18 '20 at 01:43
  • 1
    The second works if you add the missing comma. But it's a horrible thing to do! – ikegami Jun 18 '20 at 02:48
  • 1
    Can you explain why -- what you want to do with that? It's really not a good idea at all, but whatever you may need from that there are good ways for it. – zdim Jun 18 '20 at 05:21
  • @zdim I want to create a library with methods A(), B(), and C() which will be called hundreds of times in each perl program with different strings as parameters. Each method updates its respective array (@A, @B, and @C) so I don't want to pass the array by reference each time. Each perl program also some unique strings they add to the array, so I cannot have the array be created and only modified by the package. (Adding this to the original post as well) – Stuart Jun 18 '20 at 07:22
  • "_don't want to pass the array by reference each time_" --- well, OK ... but why? That denies about 30-40 years of software development, you know, about components which communicate via well defined interfaces (etc), instead of being all hard wired, If it's performance, it gains you practically nothing. As for "design aesthetics," it strikes me as quite right to create arrays where you need them (in programs that use the library), and pass the reference for update as needed to the library routines. Or are there other, more specific reasons? – zdim Jun 18 '20 at 20:56
  • See [this post](https://stackoverflow.com/a/62292260/4653379) for the most recent example of what you ask – zdim Jun 18 '20 at 21:20

1 Answers1

1

That is the way global Variables are declared and used in Perl. In your main executable you can use

@main::a 

directly or (pre-)declare it with

our @a;

or

use vars q(@a);

and use it with @a in your main script.

When you are using this global variable in another package, you have to use the fully qualified form @main::a. Especially for global Variables declared in packages (other than main) there is the Exporter - package that allows you to import them in your own namespace, so you don't have to use the form @package::variable.

See https://perldoc.perl.org/Exporter.html, https://perldoc.perl.org/functions/our.html and https://perldoc.perl.org/vars.html for further explanations.

Georg Mavridis
  • 2,312
  • 1
  • 15
  • 23
  • I'm still a bit iffy on the syntax to setup an array in the package that is a reference to the @main::arrayA. Would this be an array ref `\@main::arrayA`? – Stuart Jun 18 '20 at 07:29
  • but if you want an array reference, you could use, `$main::arrayref` and define `our $arrayref = []` in your main script. – Georg Mavridis Jun 18 '20 at 07:58