1

I want to get the array that I send in my function but it seem's to be empty. I call send_file(); with an array in the param

send_file($addr, @curfile);

And this is the way I get back the param

sub send_file($$)
{
    my $addr = $_[0];
    my @elem = @_;
    ...
}

Why my @elem is empty ? How could I get back the array without losing everything ?

1
  • 2
    Try to use a referece to the array as paramter send_file($addr, \@curfile); ---> my @elem = @{$_[1]}; Commented Oct 16, 2014 at 13:09

3 Answers 3

3

Don't use prototypes. Their purpose is to change parsing of the source which you don't need.

sub send_file
{
    my $addr = shift;
    my @elem = @_;
    ...
}

send_file($addr, @curfile);
Sign up to request clarification or add additional context in comments.

Comments

2

You should pass your array by reference instead:

#!/usr/bin/perl

use strict;
use warnings;

my $test_scalar = 10;
my @test_array  = qw(this is a test);

sub test($\@)
{
    my ($scalar, $array) = @_;

    print "SCALAR = $scalar\n";
    print "ARRAY  = @$array\n";
}

test($test_scalar, @test_array);

system 'pause';

Output:

SCALAR = 10
ARRAY  = this is a test
Press any key to continue . . .

EDIT:

If you would like to do the same thing without passing by reference change your $$ to $@ and use shift so the first argument doesn't end up included in your array. Passing arrays by reference is better coding practice though . . . This is just to show you how it can be done without passing by reference:

#!/usr/bin/perl

use strict;
use warnings;

my $test_scalar = 10;
my @test_array  = qw(this is a test);

sub test($@)
{
    my ($scalar, @array) = @_;

    print "SCALAR = $scalar\n";
    print "ARRAY  = @array\n";
}

test($test_scalar, @test_array);

system 'pause';

This will get you the same output.

You can also get rid of the $@ altogether if you would like it really isn't necessary.

4 Comments

@mpapec: My example wasn't inaccurate. Try it out, an array reference is just a scalar and will be processed correctly by the subroutine if the array reference is declared using "$" in the function prototype. It doesn't matter whether you use "$" or "\@" in the prototype, both will act the same. If you use the ref() function on an array reference passed in using both methods it will return "ARRAY" in both cases. It recognizes the input as an array reference either way.
@mpapec: If you don't believe me just run my code and see that it acts the same :)
I was referring to second example. First was correct in functionality.
Ohhhhhh I see, I meant to say $scalar = shift instead of $scalar = $_[0] that way the scalar would be taken out of "@_" before the array was populated. Nice catch, I just saw all of the format changes to the first one and assumed that it was the one you were talking about.
1

Why my @elem is empty ?

Your @elem is not empty, it has exactly two elements. First is value of $addr and second is size/number of elements in @curfile array. This is due $$ prototype definition which requires two scalars, so scalar @curfile is passed as second parameter.

How could I get back the array without loosing everything ?

Since you're not using prototype advantages, just omit prototype part,

sub send_file {
    my ($addr, @elem) = @_;
    ...
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.