in reply to Re: Problem creating a function
in thread Problem creating a function

Modifying $_[0] is bad practice. Also what happens if you cleaner("This will crash your code with a can't modify read only value error")

cheers

tachyon

s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Replies are listed 'Best First'.
Re: Re: Re: Problem creating a function
by jdporter (Paladin) on Dec 25, 2002 at 13:44 UTC
    Modifying $_[0] is bad practice.
    That's fine advice for newbies, but it's a white lie. Many languages support OUT parameters. Including Perl. At worst, it merits an encouragement to document the technique if used.
    what happens if you cleaner("This will crash your code with a can't modify read only value error")
    Perl helpfully tells you that you used the function incorrectly. So you got and fix your code. What's the big deal?

    jdporter
    ...porque es dificil estar guapo y blanco.

      What's the big deal.

      Here is an example. You are calling cleaner in some sub that is not always called in the CGI. You will get a 500 when you do and won't know about it until it happens because it is a runtime not compile time error. This script will crash 50% of the time:

      sub cleaner { $_[0] =~ s/foo/bar/; } if ( rand > 0.5 ) { cleaner('foo') } __DATA__ Modification of a read-only value attempted at script line 1. # someti +mes
      cheers

      tachyon

      s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

        <sarcasm>
        Using die is bad practice. Here is an example. If you use it you will get a 500 when you do and won't know about it until it happens because it is a runtime not compile time error. This script will crash 50% of the time:
        if ( rand > 0.5 ) { die "foo" }
        </sarcasm>

        Seriously what is your point? You can cause runtime errors with many Perl constructs and it doesn't necessary mean that using them is a bad practice. OUT parameters is a powerful language feature when used wisely.

        --
        Ilya Martynov, ilya@iponweb.net
        CTO IPonWEB (UK) Ltd
        Quality Perl Programming and Unix Support UK managed @ offshore prices - http://www.iponweb.net
        Personal website - http://martynov.org

        It may crash 50% of the time, but it only takes one crash to alert the programmer to her misuse of the function. It's incumbent on the programmer to know how to call the API correctly. Especially if it's her own.

        The simple fact is that perl is a dynamic language, and provides many essential features that can only be fully realized at "run time". That means run time errors (i.e. exceptions) are going to happen, you can count on it, and if you're a half-way careful programmer, you're going to prepare for them to happen. If the fact that your program is a CGI makes this preparation a little more difficult, well then, tough. Deal with it. Trap $SIG{__DIE__} if you have to. But don't forbid run-time exceptions, because you can't anyway. What does   $foo->bar; do? There are several possibilities, including Can't locate object method "bar", and none of them can be caught at compile time. That's just the nature of the beast (Perl).

        jdporter
        ...porque es dificil estar guapo y blanco.