mikfire has asked for the wisdom of the Perl Monks concerning the following question:
My first thought is do something like this:
I do not like the call syntax I am left with:package snafu; sub foo { my $bar = shift; # Interesting things happen $$bar = $baz; if ( $things_worked ) { return SUCCESS_CODE; else { return FAILURE_CODE; } }
because I find the extra slash somewhat jarring - it breaks the flow of the code.unless ( $fsck->foo(\$variable) == SUCCESS_CODE ) { die "That was quite grim"; }
The other way I know of doing pass-by-reference in perl is like this:
It gives me a nicer calling syntaxpackage snafu; sub foo { # interesting things happen $_[0] = $baz; if ( $things_worked ) { return SUCCESS_CODE; else { return FAILURE_CODE; } }
but I cannot help feeling that the @_ trick is somehow evil and not a very nice thing to do to a variable.unless ( $fsck->foo($variable) ) { die "A terrible way to go"; }
Is there a preferred way of doing this? In the experience of the other Monks, which makes more sense? Is the @_ trick really an evil thing or am I being squeamish? Have I missed something obvious?
TIA,
mikfire
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Pass by reference: is it good form to grope @_?
by chipmunk (Parson) on Dec 15, 2000 at 23:55 UTC | |
by Fastolfe (Vicar) on Dec 16, 2000 at 06:26 UTC | |
|
Re: Pass by reference: is it good form to grope @_?
by mirod (Canon) on Dec 16, 2000 at 00:00 UTC | |
|
Re: groping @_ (judiciously)
by Russ (Deacon) on Dec 16, 2000 at 00:27 UTC | |
|
Re: Pass by reference: is it good form to grope @_?
by merlyn (Sage) on Dec 15, 2000 at 23:59 UTC | |
|
Re: Pass by reference: is it good form to grope @_?
by Dominus (Parson) on Dec 18, 2000 at 21:35 UTC | |
|
Re: Pass by reference: is it good form to grope @_?
by mikfire (Deacon) on Dec 18, 2000 at 20:11 UTC |