Let's be philosophical. Please discuss the pros and cons of each of these approaches;

approach 1

$notice = 'please be sure that XXX is installed first'; print $notice;

approach 2 (most english-like)

sub notice { 'please be sure that XXX is installed first' } print notice;

approach 3(most coarse-grained)

sub print_notice { 'please be sure that XXX is installed first' } print_notice;

my feeling is that approach 3 is the worst because it is not re-usable - it did not couple the core capability to print with the core capability to define strings, hence this monolithic approach prohibits print_notice from being reused for other notification possibilities (unless print returns what it prints, but even then it looks funny to have the word print there in other contexts : length print_notice vs  length notice vs.  length $notice

Replies are listed 'Best First'.
Re: print_notice or print $notice or print notice?
by chromatic (Archbishop) on Oct 14, 2001 at 21:37 UTC
    Which is better, a chainsaw, an awl, or a rasp?

    Which of these constructs is preferable?

    # 1 print 2 + 2; # 2 my $result = 2 + 2; print $result; # 3 print 4; # 4 use constant FOUR => (2 + 2); print FOUR; # 5 sub sum { $_[0] + $_[1] } print sum(2, 2);
    With so little context, this is a meaningless question. What are you trying to accomplish? Answer that, and it's possible to evaluate these approaches.

    (Code reuse is all well and good, but I'm not going to write an object oriented framework for a one-liner designed to strip out high bit characters.)

    Update (Mon Oct 15 16:06:13 UTC 2001): Okay, this is unnecessarily short and terse. I apologize. Here are some questions I would ask before giving judgment:

    • Is this a cut-down example of the type of thing that might be done?
    • In what type of program does this run?
    • How often might this message be displayed?
    • Who is expected to see this message?
    • Are there other, similar messages?
    • Is this in a program or a module?
Re: print_notice or print $notice or print notice?
by Maclir (Curate) on Oct 14, 2001 at 22:16 UTC
    Or this:
    use constant NOTICE => 'please be sure that XXX is installed first'; # many lines later print NOTICE;
    And, if ever internationalisation becomes an issue, you can change the text to suit:
    if ($language eq 'perlmonks_terse') { use constant NOTICE => 'do perlfaq XXX, use strict; and use CGI;'; } elsif ($language eq 'aussie') [ use constant NOTICE => 'install the bloody XXX first, you dopey dr +ongo'; } else { use constant NOTICE => 'please be sure that XXX is installed first +'; }
      Unfortunatly, that method for changing the text for internationalization won't work. use constant is evaluated at compile-time, thus if code is compiled, a use constant in it will take effect, to illustrate this:

      #!/usr/bin/perl -w use strict; use constant TEST => 'outside'; if (0) { use constant TEST => 'if(0)'; } print TEST, "\n"; __END__ outputs: Constant subroutine TEST redefined at ... if(0)
      use constant NOTICE => { aussie => 'install bloody ...', perlmonks => 'perldoc XXX...', portuguese => 'que ?' }; print NOTICE->{$language};

      The use statements are compile time so trying to define constants based on runtime data as in your example is not going to work as you might expect. You'd want to wrap up any code to determine the $notice in a BEGIN block and follow it up with a use constant NOTICE => $notice;

Re: print_notice or print $notice or print notice?
by greywolf (Priest) on Oct 14, 2001 at 21:33 UTC
    I prefer approach 1 for the following reasons.
    a) It's easy to read.
    b) It allows you to change the value $notice on a whim.

    I've never really thought of using approach 2 or 3 before. It would seem that they are not re-usable. I suppose approach 2 could print a variable passed in as a parameter rather than a string. That would make it re-usable but it would be very cumbersome to call it.

    mr greywolf
Re: print_notice or print $notice or print notice?
by grinder (Bishop) on Oct 15, 2001 at 13:14 UTC

    Approach 3, if indeed it is supposed to read...

    sub print_notice { print 'please be sure that XXX is installed first' } print_notice;

    ...is a no-no in my book. It does two things, it defines a scalar, and prints it out. In my experience, sooner or later you will want to grab that string, to feed it to split or lc, and you will be out of luck, because the damned thing will get printed to STDOUT.

    That leaves approaches 1 and 2. Approach 2 gets my nod, because if the worst comes to the worst, you can always set a breakpoint in the sub (or add some debug print) and find out from whence it is being called. And, as is alluded to elsewhere, it lends itself to i18n and l10n.

    --
    g r i n d e r
Re: print_notice or print $notice or print notice?
by cLive ;-) (Prior) on Oct 15, 2001 at 09:27 UTC
    Er, is it me, or does No. 3 not do anything as it stands? Did you mean:
    sub print_notice { print 'please be sure that XXX is installed first' } print_notice;

    cLive ;-)

Re: print_notice or print $notice or print notice?
by tadman (Prior) on Oct 18, 2001 at 03:28 UTC
    I would propose:

    approach 2a (English-like, Custom)

    sub complaint { "Please be sure that $_[0] is installed first" } print complaint("XXX");
    The problem with #3 is that you can't tell it where to go. If you want to warn, you can't. It's going to print wherever it feels like, even if that's to the console and you're using a TK application with windows. The 'complaint' function just returns an error message.

    Later, you might do this:
    sub complaint { my %r = ( en => "Please be sure that $_[0] is installed first", fr => "Veuillez źtre sūr que $_[0] est installé premier" ); $r{$lang} } # ... $lang = "en"; print complaint("XXX");
    Please excuse my Babelfrench.