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

Simple question.
What will dumper do if I ask it for @_ within a sub? e.g.
print Dumper(@_); or perhaps
print Dumper(\@_);

A different subroutine is calling dbAction()
That subroutine is passing an array into dbAction:
@params = ($sql,$_[0],$_[1]); dbAction(@params); sub dbAction(\@){ print Dumper(@_); print Dumper(\@_); }
The $sql may have two arguments on an update, but 6 on an insert, therefore I want to make sure that the references get placed accordingly to do the $dbh->prepare() and execute(). Using print statements
print "$_[0]\n"; print "$_[1]\n";
I can see that the data is there, but I want to use a for loop to make sure how many items are being passed in to the sub, and I get -1 when I ask for $#{@_}.
Dumper is breaking and I can't tell why.

Replies are listed 'Best First'.
Re: Dumper Won't print @_ ?
by Anonymous Monk on Jul 30, 2012 at 06:30 UTC

    What will dumper do if I ask it for @_ within a sub? e.g.

    T.I.T.S - Try It To See

    $ perl -l use Data::Dumper; my $sql = " junk "; Junk( $sql, 2,4 ); sub Junk { my @params = ($sql,@_); dbAction(@params); } sub dbAction(\@){ print Dumper(@_); print Dumper(\@_); } ^Z $VAR1 = ' junk '; $VAR2 = ' junk '; $VAR3 = 2; $VAR4 = 4; $VAR1 = [ ' junk ', ' junk ', 2, 4 ];

    Dumper is breaking and I can't tell why.

    That's because Dumper isn't breaking -- you've shown no proof

Re: Dumper Won't print @_ ?
by kingram (Acolyte) on Jul 30, 2012 at 07:04 UTC
    Jeezus. In theory I'm a decent programmer, but this is ridiculous.
    print Dumper(@_);
    What I had:
    print Dumper(@_):
    I think I need glasses....
    Once again, thanks for the extra eyes, on yet another stupid mistake.
Re: Dumper Won't print @_ ?
by Marshall (Canon) on Jul 30, 2012 at 07:16 UTC
    Dumper takes a reference to "something", could be a scalar, an array or even an arbitrary complex data structure.
    #!usr/bin/perl -w use strict; use Data::Dumper; my $sql = "some string"; dbAction($sql, 55, 66); sub dbAction # DO NOT use a prototype! { my (@inputs) = my ($sql, $parm1, $parm2) = @_; print Dumper (\@inputs); print Dumper (\@_); #this works also #but I would prefer to give #the @_ inputs a name #choice is yours print Dumper (\$sql, \$parm1, \$parm2); print Dumper (\$sql, \@inputs[1,2]); } __END__ prints: $VAR1 = [ 'some string', 55, 66 ]; $VAR1 = [ 'some string', 55, 66 ]; $VAR1 = \'some string'; $VAR2 = \55; $VAR3 = \66; $VAR1 = \'some string'; $VAR2 = \55; $VAR3 = \66;
Re: Dumper Won't print @_ ?
by Athanasius (Archbishop) on Jul 30, 2012 at 06:35 UTC

    sub dbAction is prototyped to take a reference to an array; but you call it with an array (not a reference). Update: See the reply by AnomalousMonk below; also Prototypes.

    Without the prototype:

    #! perl use strict; use warnings; use Data::Dumper; my @params = qw(homer marge bart lisa maggie); dbAction(@params); sub dbAction { print Dumper( @_), "\n"; print Dumper(\@_); }

    Outputs:

    $VAR1 = 'homer'; $VAR2 = 'marge'; $VAR3 = 'bart'; $VAR4 = 'lisa'; $VAR5 = 'maggie'; $VAR1 = [ 'homer', 'marge', 'bart', 'lisa', 'maggie' ];

    At least one of these is what you wanted?

    HTH,

    Athanasius <°(((><contra mundum

      sub dbAction is prototyped to take a reference to an array; but you call it with an array (not a reference).

      Actually,  dbAction() is prototyped to take a single explicit array (i.e., something with a  @ sigil) as an argument and pass a reference to the array to the function. It will refuse to take an array reference, which is a scalar – won't even compile.

      >perl -wMstrict -le "use Data::Dump; ;; sub S (\@) { my ($array_ref) = @_; dd $array_ref; } ;; my @ra = (qw(foo bar baz), [ 1, 2, 3 ]); S(@ra); " ["foo", "bar", "baz", [1, 2, 3]]