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

I have a functions that creates a SQL statement. It creates the statement fine but the parent function does not get the SQL string. Perl -V 5.8.4 Thanks for the suggestions.
#build a dynamic sql statement my $Sql_str = &BuildSQLString($cMssFactBag); #I print here I get no SQL statement sub BuildSQLString { ... $dynam_Sql_stmt = join(" ",$dynam_Sql_stmt, "from $p_str_tblName where + RECORDXFEREDTOFILE = 'I'"); #I print here I a SQL statement return $dynam_Sql_stmt; }

Replies are listed 'Best First'.
Re: Subroutine Produces Output but Fails to Return to Parent
by imp (Priest) on Jan 18, 2007 at 23:04 UTC
    The code you provided works for me after I add a line to print the result, so your problem is elsewhere.

    You should always use both strict and warnings, as they will catch many common errors. In your example code you used mixed case in your variable names, which would be an easy place to introduce a typo.

    After adding strict and warnings you should reduce the code to the simplest form that still behaves incorrectly. Chances are you will find the error by doing this, and if not we will be happy to assist you further. But please confirm that the reduced version still demonstrates the error (and that it runs of course).

Re: Subroutine Produces Output but Fails to Return to Parent
by shigetsu (Hermit) on Jan 18, 2007 at 23:34 UTC
    It's hard to judge from your incomplete code excerpt what went wrong.
    It partly works for me though.

    Some remarks:

    #build a dynamic sql statement my $Sql_str = &BuildSQLString($cMssFactBag); print $Sql_str; #I print here I get no SQL statement

    So far, it looks not too bad, except that you shouldn't be using an ampersand (&) here, unless you know how it alters the behaviour of the subroutine called (circumvents prototypes; without arguments being called, also passes @_ implicitly)

    sub BuildSQLString { ... $dynam_Sql_stmt = join(" ",$dynam_Sql_stmt, "from $p_str_tblName where + RECORDXFEREDTOFILE = 'I'"); #I print here I a SQL statement return $dynam_Sql_stmt; }

    Where do $p_str_tblName, $dynam_Sql_stmt come into life? May we see what ... stands for?

    A few related hints:

  • The scoping of some variables is inadequate (see my, local & our)
  • The arguments weren't properly passed to or received by the subroutine (see perlsub)
  • There's a spelling error lurking somewhere (can be caught by strict)

    However, I recommend to enable strict & warnings in most circumstances.

      I am turning on strict and will use warnings. I will also remove the & before calling the build sql function. Thank you for the responses.
Re: Subroutine Produces Output but Fails to Return to Parent
by chargrill (Parson) on Jan 18, 2007 at 23:26 UTC

    Incidentally, you don't need to use join if you're just doing a simple concatenation (which it appears you are). One of these varients suffices:

    $dynam_Sql_stmt = "$dynam_Sql_stmt from $p_str_tblName where RECORDXFE +REDTOFILE = 'I'"); # or $dynam_Sql_stmt = $dynam_Sql_stmt . " from " . $p_str_tblName . " wher +e RECORDXFEREDTOFILE = 'I'");

    ... with my preference toward the former.

    But the advice from everyone else is quite good. We need more code, if only we see what the values of some of your variables are, as well as what code you've elided.

    You might just have some code that's incorrect case and it can be hard to see that. use strict; will catch those kinds of errors.



    --chargrill
    s**lil*; $*=join'',sort split q**; s;.*;grr; &&s+(.(.)).+$2$1+; $; = qq-$_-;s,.*,ahc,;$,.=chop for split q,,,reverse;print for($,,$;,$*,$/)
Re: Subroutine Produces Output but Fails to Return to Parent
by derby (Abbot) on Jan 18, 2007 at 23:05 UTC
Re: Subroutine Produces Output but Fails to Return to Parent
by liverpole (Monsignor) on Jan 18, 2007 at 23:09 UTC
    Hi vegasjoe,

    Can you show us what you do get when you print here?  And also what happens when you display it from within the subroutine?

    I that if you do something like the following, it should give you more clues ...

    use strict; use warnings; # ... #build a dynamic sql statement my $Sql_str = &BuildSQLString($cMssFactBag); print "Debug: Sql_str is: '$Sql_str'\n"; sub BuildSQLString { # ... $dynam_Sql_stmt = join(" ",$dynam_Sql_stmt, "from $p_str_tblName where RECORDXFEREDTOFILE = 'I'"); print "Debug: in BuildSQLString: '$dynam_Sql_stmt'\n"; return $dynam_Sql_stmt; }

    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/