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

hello monks:

I am try to create a simple subroutine. Basically copied the example from Learning Perl pg 59. The error message is:
Can't modify constant item in list assignment at loadBofATradesB.pl line 156, near "@_;" This is my subroutine
sub fieldString { my($strIn, $typeIn); #this is line 156 ($strIn, typeIn) = @_; $strIn = trim(strIn); $rtnVal = undef; if ( length($strIn) == 0 ) { $rtnVal = 'NULL'; } else { if (typeIn == 'N') { $retVal = $strIn; } else { $retVal = "'$strIn'"; } } $retVal; } # Perl trim function to remove whitespace from the start and end of th +e string sub trim($) { my $string = shift; $string =~ s/^\s+//; $string =~ s/\s+$//; return $string; }

I do not understand my mistake.

kd

Replies are listed 'Best First'.
Re: will not compile
by ikegami (Patriarch) on Sep 18, 2007 at 16:43 UTC

    Other bugs:

    • "'$strIn'" is critically flawed. (What if $strIn contains a quote...)
    • $rtnVal and $retVal are never declared.
    • Aren't $rtnVal and $retVal suppose to be the same variable?
    • You're using == for lexical comparison. Use eq for that.
    • use strict; is probably missing
    • Unrequired use of a prototype. Prototypes are best avoided, so sub trim($) { ... } should be sub trim { ... }.
    • Sounds like you should be using placeholders (e.g. SELECT * FROM table WHERE field = ?) instead of writing your own quoting function.
      many thanks to all of you
      after a break I see those silly mistakes.
      Maybe I need to take a walk or better yet a nap
      kd
Re: will not compile
by toolic (Bishop) on Sep 18, 2007 at 16:26 UTC
    I think it's a typo: use $typeIn instead of typeIn everywhere.
Re: will not compile
by ikegami (Patriarch) on Sep 18, 2007 at 16:36 UTC

    If made the mistake of not using use strict;, you won't see the much more revealing message

    Bareword "typeIn" not allowed while "strict subs" in use.