Click to See Complete Forum and Search --> : Perl: Help wanted: another method for returning strings


TheLinuxDuck
09-18-2001, 03:44 PM
Let's say I have a sub:

sub myFunction()
{
return "This string is part of ".
"the return info";
}

:let's say I call it as:

print myFunction, "-Taa daa!!\n";

This works.

However, I don't want to use the '.' concatenation operator. I'd LIKE to be able to do something like:

sub myFunction
{
my($buffer);
print $buffer "This is a part of ",
"the return stuff";
return $buffer;
}


This does not work. but, I'd like to do something similar.

I COULD use join, but the problem is that the sub does lots of error checking and testing and builds a string based on passed parameters, so join wouldn't be the best method.

Why, do you say, do I care? Because I prefer to use the comma separated print list. I'm deranged, according to FoBoT.

YaRness
09-18-2001, 04:00 PM
umm

sub nameitwhatyouwant
{
my $foo;
$foo .= $_ foreach (@_);
return $foo;
}
then put it in another file, and "use yourfile".

then pretend it doesn't exist, and definitely doesn't have a concat operator :D

YaRness
09-18-2001, 04:05 PM
just cuz i'm bored


sub whatever
{
my $foo;
$foo =~ s/$/$_/ foreach (@_)
return $foo
}

TheLinuxDuck
09-18-2001, 04:30 PM
Originally posted by YaRness:
<STRONG>just cuz i'm bored


sub whatever
{
my $foo;
$foo =~ s/$/$_/ foreach (@_)
return $foo
}
</STRONG>

Now THAT I like!! (^= The only negative part is that "dadada foreach()" is 5.6.0 specific, unless I'm totally stupid. But, that;s not a big deal cuz I use 5.6.1. So there!

Word. I like it! You, YaR, have made me happy!

Say.. what is the CPU cycle difference from using that s/// to using a concat operator?

jemfinch
09-19-2001, 08:10 AM
Is there any real difference between what you want to do and what you are doing?

I can't tell. It must be a Perl thing :D

Jeremy

YaRness
09-19-2001, 08:30 AM
tld: i have no idea what the performance difference is. torture the answer outta jeremy or something. i would guess the s/// operator is much less efficient than the concatenate operator, because it hasta find what your looking for AND glue in the substitution, instead of just glueing on the concatenation. but unless yer doing some seriously heavy processing, who cares! if yer worried, you can always find some modules to load for doing benchmarking.

jem: the difference is the new way satisfies TLD. re-writing a very convienient and simple operator just for personal preference and sake of experimentation is definitely a perl thing.

TheLinuxDuck
09-19-2001, 09:32 AM
YaR has hit very close to the truth.. I have come to like the formatting of using a comma separated list with print. It just feels good to me. I have come to dislike the concat operator. I think it looks funny. I guess the concat operator is better than the s///, because of exactly what you mentioned.

I'm just being silly. And picky.

Perl does that to a duck... (^=