overginign two arrays to a package!

R

RBCS

Hello,

Why are in the package Testing all the Vars from arr1 and arr2 in the
@array1 array and the @array2 empty?

Roman

___________________________

my (@tasks);
my (@args);

my $ResultofTesting = Testing->info($par1, $par2, @arr1, @arr2);


ackage Testing;

sub info{
my( undef, $param1, $param2, @array1, @array2 ) = @_;
 
P

Paul Lalli

RBCS said:
Subject: Re: overginign two arrays to a package!

I have *no* idea what that first word is supposed to be. Please
consider at least using a spell checker.
Why are in the package Testing all the Vars from arr1 and arr2 in the
@array1 array and the @array2 empty?

my (@tasks);
my (@args);

my $ResultofTesting = Testing->info($par1, $par2, @arr1, @arr2);


package Testing;

sub info{
my( undef, $param1, $param2, @array1, @array2 ) = @_;

Because that's how subroutines work in Perl. More generally, that's
how lists and arrays work in Perl. Arrays "flatten" when making a list
assignment. If you want to keep track of where one array ended and
another began, you have to use references.

my $ResultOfTesting = Testing->info($par1, $par2, \@arr1, @arr2);

sub info {
my $class = shift; #don't throw this away...
my ($param1, $param2, $ref1, $ref2) = @_;
my @array1 = @{$ref1};
my @array2 = @{$ref2};
# . . .
}

Please read:
perldoc perlsub
perldoc perlreftut

Paul Lalli
 
R

RBCS

Hello Paul

Thanks

I Got it...!

Roman

I have *no* idea what that first word is supposed to be. Please consider
at least using a spell checker.


Because that's how subroutines work in Perl. More generally, that's how
lists and arrays work in Perl. Arrays "flatten" when making a list
assignment. If you want to keep track of where one array ended and
another began, you have to use references.

my $ResultOfTesting = Testing->info($par1, $par2, \@arr1, @arr2);

sub info {
my $class = shift; #don't throw this away... my ($param1, $param2,
$ref1, $ref2) = @_; my @array1 = @{$ref1};
my @array2 = @{$ref2};
# . . .
}
}
Please read:
perldoc perlsub
perldoc perlreftut

Paul Lalli
 
T

Tad McClellan

my $ResultOfTesting = Testing->info($par1, $par2, \@arr1, @arr2);
^^^

You missed a backslash:

my $ResultOfTesting = Testing->info($par1, $par2, \@arr1, \@arr2);
 
A

Anno Siegel

Tad McClellan said:
^^^

You missed a backslash:

my $ResultOfTesting = Testing->info($par1, $par2, \@arr1, \@arr2);

Not necessarily. Change

to

sub info {
my( $class, $param1, $param2, $ref1, @array) = @_;

to make it work.

Anno
 
B

Brian McCauley

Anno said:
Not necessarily.

TMTOWTDI! However some are preferable to others.
Change


to

sub info {
my( $class, $param1, $param2, $ref1, @array) = @_;

to make it work.

Yes that works. But when you come along next week and want to add
another argument to info() you'll curse yourself.

Also as a stylistic convention I'm certainly in favour of shifting a
method's class/self argument off @_ as a separate operation from
unpacking the remainder of the argument list.
 
B

Brian McCauley

Paul said:
Please consider at least using a spell checker.

See also:

http://www.geocities.com/SouthBeach/1915/chequer.html
sub info {
my $class = shift; #don't throw this away...
my ($param1, $param2, $ref1, $ref2) = @_;
my @array1 = @{$ref1};
my @array2 = @{$ref2};

I think you should point out that copying the arrays like this could be
expensive and usually not necessary. It is often simpler to work with
$ref1 and $ref2 dierctly.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top