in reply to Can we do without auxiliary variable here?

rovf:

I rather like:

#!/usr/bin/perl use warnings; use strict; my $prefix="prefix: "; sub printpre { unshift @_, $prefix; print @_; } printpre "apple ", "banana ", "tomfoolery ", "\n";

...roboticus

When your only tool is a hammer, all problems look like your thumb.

Replies are listed 'Best First'.
Re^2: Can we do without auxiliary variable here?
by chromatic (Archbishop) on Feb 28, 2011 at 22:20 UTC

    Why not write that without the auxiliary array manipulation there?

      chromatic:

      Ha, ha! It never occurred to me!

      #!/usr/bin/perl use strict; use warnings; my $prefix="Prefix: "; sub print_prefix { print $prefix, @_; } print_prefix "alpha ", "beta ", "gamma\n";

      Sigh. When I read your reply, I thought, "Hmm, does unshift return a reference to the array?", so a first I thought you were prompting me to do something like:

      print unshift @_, $prefix;

      But when I checked the docs, I could see that it wouldn't fly. Then I understood what you meant. Ah, well! I'm always amused at how my mind works: it seems I frequently complicate simple things. ;^)

      ...roboticus

      When your only tool is a hammer, all problems look like your thumb.

Re^2: Can we do without auxiliary variable here?
by ikegami (Patriarch) on Feb 28, 2011 at 23:19 UTC
    It seems to me the OP has a logger and he wants to include the prefix at the start of every line, not the start of every message. Those aren't the same if there's a possibility of multi-line messages.
Re^2: Can we do without auxiliary variable here?
by rovf (Priest) on Mar 01, 2011 at 11:20 UTC

    This would not cope when there are "\n" in between. See the comment by ikegami.

    -- 
    Ronald Fischer <ynnor@mm.st>

      rovf:

      Yeah, I clearly missed on that one. I then tried:

      sub print_prefix { local $/="\nPrefix: "; print @_; }

      But that didn't work. On first guess, I thought it would do OK. After reading the documentation, I thought it would print it at least once. But what I got was...nothing. It never printed prefix at all. The only other methods I could think of had already been covered.

      ...roboticus

      When your only tool is a hammer, all problems look like your thumb.