in reply to Working with references to scalars

I find myself frequently being an advocate for non-obfuscated code - I think your original code
my $string = get_string(); put_string_ref(\$string);
is very simple and clear and easily understood. I'm sure some people will disagree with me here, but IMHO doing
put_string_ref(\get_string())
is at least somewhat obfuscated. It kind of begs the question "What am I passing to put_string_ref? Is it a reference to the get_string subroutine, or is it a reference to the string that get_string returns?". It is not absolutely clear looking at that code *what* is being passed to put_string_ref.

Again, just my opinion, but your original code is better since it is very clear about what it is doing - self documenting code is best. If you need to spend time figuring out what it does, then chances are it could have been written in a more understandable and more easily maintainable way.

HTH.

Replies are listed 'Best First'.
Re: Re: Working with references to scalars
by cees (Curate) on May 09, 2003 at 19:52 UTC

    I agree that my example wouldn't be a good case of clear coding style, but I was only trying to create a simple example. How I will use it like this:

    use HTML::Template(); use HTML::FillInForm(); use CGI(); my $string = '<b><TMPL_VAR test></b>'; my $query = new CGI; my $template = new HTML::Template(scalarref => \$string); my $fif = new HTML::FillInForm; print $fif->fill(scalarref => \$template->output(), fobject => $query);

    In this case it clearly states that the fill method expects a reference to a scalar. I prefer the above code over this (the old way I always used)

    my $html = $template->output(); print $fif->fill(scalarref => \$html, fobject => $query);