ramjamman has asked for the wisdom of the Perl Monks concerning the following question:

Just returning to perl learning after a few years and I have a problem that searching the net is not helping me very much.

I am trying to pass an array back from a subroutine but I am losing the data along the way. Any help would be very much appreciated!! Here is the relevant bit of my code

if ($ff1) {my @a_ff1 = &GetRow("$ff1")}; sub GetRow { my ($set) = $_[0]; my $sth = $dbh->prepare("SELECT `filter_partnumber`,`filter_fram` +,`filter_fleetguard`,`filter_notes` FROM `filter_desc` WHERE `filter_ +id` = $set ") or die $DBI::errstr; $sth->execute() or die $DBI::errstr; my @ary = $sth->fetchrow_array(); $sth->finish; return @ary; }

The subroutine array (@ary) has the desired data if checked by printing before return. But it does not get assigned to @a_ff1 in the if statement.

Replies are listed 'Best First'.
Re: Passing an array back from a subroutine
by kennethk (Abbot) on Aug 02, 2010 at 21:06 UTC
    The problem is likely a scoping issue. If you execute the code

    if ($ff1) { my @a_ff1 = &GetRow("$ff1") }

    @a_ff1 is scoped to the if loop. Can we see more of the code surrounding your assignment? Do you see the expected result if you run

    if ($ff1) { my @a_ff1 = &GetRow("$ff1"); print join "\n", @a_ff1; }

    in place of your existing assignment? See Variable scoping in perlintro.

Re: Passing an array back from a subroutine
by toolic (Bishop) on Aug 02, 2010 at 21:10 UTC
    What does @a_ff1 have in it?
    use Data::Dumper; if ($ff1) {my @a_ff1 = &GetRow("$ff1"); print Dumper(\@a_ff1)};

    This works as expected for me. Start with this, then keep adding code until the problem arises:

    use strict; use warnings; use Data::Dumper; my @a_ff1 = &GetRow("foo"); print Dumper(\@a_ff1); sub GetRow { my @ary = 0 .. 3; return @ary; } __END__ $VAR1 = [ 0, 1, 2, 3 ];

    See also Basic debugging checklist

Re: Passing an array back from a subroutine
by pemungkah (Priest) on Aug 02, 2010 at 21:10 UTC
    Remember that @_ff1 will go poof outside that if block (because you used my inside the block, the variable's only defined in that scope or scopes inside it).

    My telepathic powers tell me you're not using strict. If you were, you'd get an error trying to access @a_ff1 outside of the if block.

Re: Passing an array back from a subroutine
by ahmad (Hermit) on Aug 02, 2010 at 21:07 UTC

    First of all, As you are already using prepared statements ... Then I'd suggest using placeholders which requires changing `filter_id` = $set to `filter_id` = ? then you can supply the value of filter_id to the execute method

    You don't actually need to call your subroutine with the '&' the, use it only if you want to get the value of @_

    Finally, I don't think you have any problem with your code and it should be assigning the value correctly.

Re: Passing an array back from a subroutine
by ramjamman (Sexton) on Aug 02, 2010 at 21:45 UTC
    Thanks everyone for your prompt replies it was as suggested a scoping issue with the my in the if statement. It is good to see perl is still alive and well after all these years.
      Thank you too because i myself benefited from this site.
Re: Passing an array back from a subroutine
by Generoso (Prior) on Aug 03, 2010 at 17:00 UTC

    Fond several tings that where missing and/or misplaced.
    compare your code with mine it works.

    my $ff1 = 3; my @a_ff1; if ($ff1) {@a_ff1 = &GetRow("$ff1");}; print @a_ff1,"\n"; sub GetRow { my ($set) = $_[0]; my $sth = $dbh->prepare("SELECT `filter_partnumber`,`filter_fram` +,`filter_fleetguard`,`filter_notes` FROM `filter_desc` WHERE `filter_ +id` = ? ") or die $DBI::errstr; $sth->execute($set) or die $DBI::errstr; my @ary = $sth->fetchrow_array(); $sth->finish; return @ary; }