in reply to Sub Routines and Building SQL statement

Read perlintro, you're missing a lot of basics , like @_ is an array, not a scalar, and lists are flattened

Like

use Data::Dumper; sub fudge { print scalar Dumper( \@_ ); } my @bar = ( 6,6,6 ); fudge(1); fudge(@bar, 1 ); __END__ $VAR1 = [ 1 ]; $VAR1 = [ 6, 6, 6, 1 ];

Replies are listed 'Best First'.
Re^2: Sub Routines and Building SQL statement
by Anonymous Monk on Nov 21, 2011 at 16:14 UTC
    Ok that was a very helpful. I've updated my sub and call like so
    sub process(){ my $processed = $_[0]; return sub { if ( @_ ){ print "Sub Process called\n"; my ($colname,$colval) = @_; $colval =~ s/^&//; # strip leading & - this only occurs in + the 'ADDITIONAL field' print "Colname: $colname\n"; print "Colvalue: $colval\n"; # detect ADDITIONAL with multiple key value pairs. if ( index($colval,'&') >= 0 ){ print "$colval contaisn & character - so it's the ADDI +TIONAL field with multiple key value pairs"; my @additional = split('&',$colval); foreach my $addval (@additional){ print "additional has pair: $addval\n"; my @addpair = split('=',$addval); print "$addpair[0] | $addpair[1]\n"; process->($addpair[0], $addpair[1]); # this doesn' +t get called. } } # detect ors if ( index($colval,'|') >= 0 ){ print "$colval contains pipe character for ORs"; } # detect wildcards if ( index($colval,'*') >= 0 ){ print "$colval contains whidcard character"; } } } }

    Calling it now like
    process->("TABLENAME",$TABLENAME) if defined($TABLENAME);

    But, now that I'm calling split for each additional key/value pair in the additional column, I see the values get displayed by the print statement, but the sub doesn't get called.
Re^2: Sub Routines and Building SQL statement
by Anonymous Monk on Nov 21, 2011 at 16:51 UTC
    is this because I'm calling a sub within the defining sub?

      Sort of. The sub is getting run, or the print statement wouldn't be executed. But you're not doing quite what you're trying to do with the higher-order stuff. Your outer sub returns the inner sub, but you aren't assigning it to anything, just executing it. You never call it directly. So you might as well just use an ordinary subroutine.

      To return an inner sub and call it later, you do something like this:

      sub get_a_processor { # will return a reference to a sub that processes return sub { # your processing stuff } } my $processor = get_a_processor(); # $processor points to inner sub $processor->($TABNAME); #calls the inner sub on $TABNAME

      Aaron B.
      My Woefully Neglected Blog, where I occasionally mention Perl.