[EMAIL PROTECTED] wrote:

> Can someone make this work like I want? I'm trying to create a package
> USER and reference/change it. The only thing I'm able to do is to call the
> sub prtAll. I just want a structure that I can pass around in perl.
>
> test.pl
> -------
> use USER;
>
> #this does NOT work
> #how do i reference these vars
> USER::fname="bob";
> USER::lname="Bingham";
>
> print USER::prt . "\n";
>
> USER.pm
> -------
> package USER;
>
> $fname;
> $lname;
>
> sub prtAll { ... }

I'm going to take a different tack than the others on this, and just try to
show you what you need to do just what is shown above.  Note that this is not
the best way to do things, but if you really want global variables to be held
in a package for you, this should work:

test_user.pl:
#!perl

use User;

$fname = 'R.';
$lname = 'Newton';

print_all();

User.pm:
package User;

use strict;
use warnings;   # Don't expect much help without these two lines in your code.

use Exporter;

our @ISA = 'Exporter';
our @EXPORT = ('$fname', '$lname', 'print_all');

our $fname;
our $lname;

sub print_all {
  print "First name is $fname\n";
  print "Last name is $lname\n";
}

1;

Results:
Greetings! E:\d_drive\perlStuff>test_user.pl
First name is R.
Last name is Newton

Greetings! E:\d_drive\perlStuff>

Note:  This works, but it is really a terrible thing to do.  What it does is
make these mystery variables visible to everything in your script.  That means
that you have mystery varianles getting there values assigned here, getting
used there, maybe getting re-assigned in another place again before the
assignment you intended gets used.

It is pretty much what you seemed to be asking for, though.  If you want to
move beyond , start by taking a step back.  Write some modest programs with
strict and warnings turned on.  Keep them turned on as you add more
sophistication to your code.  The more powerful the programming tools and
techniques you learn, the more important they become.  Even here, they could
help you clean up your code so those giving you advice can focus on major
issues.

Then read up on references, packages, modules, and objects;
perldoc perlref
perldoc perlmod
perldoc perlobj

I would recommend making perlref your first priority.  There is probably no
programming concept as fundamental for power programming than references.
They are the "handles" by which we make use of objects.  They are the key to
making flexible, robust data structures of arbitrary complexity.  Passing by
reference is also the safest way to allow functions to modify the values in
another scope.

Its worth taking some time to work with references, on very simple programs,
before trying to take on large-scale projects.

Joseph


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to