Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

turning off warnings for a function's params?

73 views
Skip to first unread message

David Storrs

unread,
Apr 24, 2005, 10:50:01 PM4/24/05
to The Perl6 Language List
I image we've all written logging code that looks something like this
(Perl5 syntax):

sub foo {
my ($x,$y) = @_;
note("Entering frobnitz(). params: '$x', '$y'");
...
}

This, of course, throws an 'uninitialized value in concatenation or
string' warning when your test suite does this:

is( foo(undef, undef), undef, "foo(undef, undef) gives undef" );

In a testing environment, I don't want to see this warning. In a
production environment, I do. Furthermore, when I want it gone, I
want it gone from every instance of C<note>, without having to change
something in every location. I suppose I could change all my logging
calls to look like this:

{
if ( $DEBUG ) { no warnings 'uninitialized'; note("...."); }
else { note("...."); }
}

But that's really ugly, takes up a lot of space, is confusing, and is
redundant.

How would I best solve this problem in Perl6?

--Dks


--
dst...@dstorrs.com

Luke Palmer

unread,
Apr 25, 2005, 7:18:11 AM4/25/05
to The Perl6 Language List
David Storrs writes:
> I image we've all written logging code that looks something like this
> (Perl5 syntax):
>
> sub foo {
> my ($x,$y) = @_;
> note("Entering frobnitz(). params: '$x', '$y'");
> ...
> }
>
> This, of course, throws an 'uninitialized value in concatenation or
> string' warning when your test suite does this:
>
> is( foo(undef, undef), undef, "foo(undef, undef) gives undef" );
>
> How would I best solve this problem in Perl6?

Of course, no ordinary definition of a note() sub will work, since the
concatenation happens before note is even touched. However, a macro
could do it. It might go something like this:

macro note(Perl::Expression $expr)
is parsed(/$<expr> := <Perl.arglist(:(Str))>/)
{
$expr.compile(:warnings(0));
}

Luke

David Storrs

unread,
Apr 25, 2005, 1:00:16 PM4/25/05
to The Perl6 Language List
On Mon, Apr 25, 2005 at 05:18:11AM -0600, Luke Palmer wrote:
> David Storrs writes:
> > sub foo {
> > my ($x,$y) = @_;
> > note("Entering frobnitz(). params: '$x', '$y'");
> > ...
> > }
> > This, of course, throws an 'uninitialized value in concatenation or
> > string' warning when your test suite does this:
> > is( foo(undef, undef), undef, "foo(undef, undef) gives undef" );
> > How would I best solve this problem in Perl6?
>
> Of course, no ordinary definition of a note() sub will work, since the
> concatenation happens before note is even touched.

Exactly; that's why I asked "how would I solve this", instead of "how
would I write note()".


> However, a macro could do it. It might go something like this:
>
> macro note(Perl::Expression $expr)
> is parsed(/$<expr> := <Perl.arglist(:(Str))>/)
> {
> $expr.compile(:warnings(0));
> }
>
> Luke

Cool. But that seems to turn off all warnings during the compilation
of the expression--I only want to get rid of the (expected)
'uninitialized' warning. Will there be a way to do finer-grained
control?

--Dks
--
dst...@dstorrs.com

Juerd

unread,
Apr 25, 2005, 1:10:49 PM4/25/05
to The Perl6 Language List
David Storrs skribis 2005-04-25 10:00 (-0700):

> Cool. But that seems to turn off all warnings during the compilation
> of the expression--I only want to get rid of the (expected)
> 'uninitialized' warning. Will there be a way to do finer-grained
> control?

compile("no warnings :undef; $expr").


Juerd
--
http://convolution.nl/maak_juerd_blij.html
http://convolution.nl/make_juerd_happy.html
http://convolution.nl/gajigu_juerd_n.html

Piers Cawley

unread,
Apr 27, 2005, 5:39:43 PM4/27/05
to The Perl6 Language List
David Storrs <dst...@dstorrs.com> writes:

Write an appropriate macro:

warns(is( foo(undef, undef), undef, "foo(undef, undef) gives undef"),
"uninitialized value in concatenation or string");


That way you get to ensure that the warning gets thrown correctly if undef is
passed, but you don't get the warning mucking up your test output.

0 new messages