Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > sub returning nothing

Reply
Thread Tools

sub returning nothing

 
 
Gunnar Strand
Guest
Posts: n/a
 
      06-08-2004
Hi,

IANAPG (I Am Not A Perl Guru), but I found something which got me a
little worried. This has probably been discussed ad nauseum, but I
haven't found anything about it in the FAQ. sub's returning nothing
can be dangerous in some contexts. Consider this code:

#!/usr/bin/perl
use strict;
use warnings;

sub isok {
if ( $_[0] ) {
print "Ok\n";
} else {
print "Nok\n";
}
}

sub myvoid {
}

isok( myvoid(), 1 );
isok( ! myvoid(), 1 );

This script will print

Ok
Ok

and not any 'Nok'. I assume that's because 'nothing' is magically
removed from @_, leaving only the '1', right? I am trying out
the Test::Unit suite and had an empty sub in the code under test
while using assert in my test case, and all tests passed:

$self -> assert( $target -> the_test(), "Test OK" );
$self -> assert( ! $target -> the_test(), "Test NOK" );

IMHO, it would have been nice if perl could have issued some kind
of warning, since relying on 'nothing' as a return value seems...
well, not-so-common. Maybe perl is making heavy use of it internally?

Regards,

/Gunnar
 
Reply With Quote
 
 
 
 
Jeff 'japhy' Pinyan
Guest
Posts: n/a
 
      06-08-2004
On Tue, 8 Jun 2004, Gunnar Strand wrote:

>sub isok {
> if ( $_[0] ) {
> print "Ok\n";
> } else {
> print "Nok\n";
> }
>}
>
>sub myvoid {
>}
>
>isok( myvoid(), 1 );
>isok( ! myvoid(), 1 );
>
>This script will print
>
>Ok
>Ok
>
>and not any 'Nok'. I assume that's because 'nothing' is magically
>removed from @_, leaving only the '1', right? I am trying out


Because isok() isn't enforcing any calling context on its arguments,
myvoid() is called in list context (since it's in the argument list to a
function), and nothingness in list context is (), and empty list.

--
Jeff Pinyan RPI Acacia Brother #734 RPI Acacia Corp Secretary
"And I vos head of Gestapo for ten | Michael Palin (as Heinrich Bimmler)
years. Ah! Five years! Nein! No! | in: The North Minehead Bye-Election
Oh. Was NOT head of Gestapo AT ALL!" | (Monty Python's Flying Circus)

 
Reply With Quote
 
 
 
 
Brian McCauley
Guest
Posts: n/a
 
      06-08-2004
Gunnar Strand <(E-Mail Removed)> writes:

> Hi,
>
> IANAPG (I Am Not A Perl Guru), but I found something which got me a
> little worried. This has probably been discussed ad nauseum,


Yes I can recall one really nausiating discussion of this.

> but I
> haven't found anything about it in the FAQ. sub's returning nothing
> can be dangerous in some contexts. Consider this code:
>
> #!/usr/bin/perl
> use strict;
> use warnings;
>
> sub isok {
> if ( $_[0] ) {
> print "Ok\n";
> } else {
> print "Nok\n";
> }
> }
>
> sub myvoid {
> }


That is not a subroutine returning nothing.

That is an empty function.

In a list context an empty function retunrns its arguments.

In a scalar context it returns undef.

How ever I wouldn't consider this to be defined bevaviour. I would be
inclined to consider the behaviour of an empty function wrt what it
returns to be undefined.

So let us instead consider a function that returns nothing.

sub myvoid {
return;
}


> isok( myvoid(), 1 );
> isok( ! myvoid(), 1 );
>
> This script will print
>
> Ok
> Ok
>
> and not any 'Nok'. I assume that's because 'nothing' is magically
> removed from @_, leaving only the '1', right?


No magic. If you call a function in a list context and it returns
'nothing' then nothing is an empty list. If you append a 1 to an
emplt list you get a single elemement list containing the value 1.

> I am trying out
> the Test::Unit suite and had an empty sub in the code under test
> while using assert in my test case, and all tests passed:
>
> $self -> assert( $target -> the_test(), "Test OK" );
> $self -> assert( ! $target -> the_test(), "Test NOK" );
>
> IMHO, it would have been nice if perl could have issued some kind
> of warning, since relying on 'nothing' as a return value seems...
> well, not-so-common.


I'm affraid that calling functions in a list context is common in
perl.
 
Reply With Quote
 
Brian McCauley
Guest
Posts: n/a
 
      06-08-2004
Brian McCauley <(E-Mail Removed)> writes:

> Gunnar Strand <(E-Mail Removed)> writes:
>
> > Hi,
> >
> > IANAPG (I Am Not A Perl Guru), but I found something which got me a
> > little worried. This has probably been discussed ad nauseum,

>
> Yes I can recall one really nausiating discussion of this.


Sorry, hit send too soon before I looked up the thread on Google...

http://groups.google.com/groups?thre...ocal.mitre.org


--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
 
Reply With Quote
 
Gunnar Strand
Guest
Posts: n/a
 
      06-08-2004
Brian McCauley wrote:
> Gunnar Strand <(E-Mail Removed)> writes:

[...]
>>sub myvoid {
>>}

>
>
> That is not a subroutine returning nothing.
>
> That is an empty function.
>
> In a list context an empty function retunrns its arguments.


It returns an empty list in a list context according to perlmod,
and perl -e 'sub a{};print a(1, 2, 3)' prints nothing.

> In a scalar context it returns undef.
>
> How ever I wouldn't consider this to be defined bevaviour. I would be
> inclined to consider the behaviour of an empty function wrt what it
> returns to be undefined.
> So let us instead consider a function that returns nothing.
>
> sub myvoid {
> return;
> }
>
>>isok( myvoid(), 1 );
>>isok( ! myvoid(), 1 );
>>
>>This script will print
>>
>>Ok
>>Ok
>>
>>and not any 'Nok'. I assume that's because 'nothing' is magically
>>removed from @_, leaving only the '1', right?

>
>
> No magic. If you call a function in a list context and it returns
> 'nothing' then nothing is an empty list. If you append a 1 to an
> emplt list you get a single elemement list containing the value 1.


Thanks for the clarifaction. I didn't think about the list
context for the sub arguments.

>>I am trying out
>>the Test::Unit suite and had an empty sub in the code under test
>>while using assert in my test case, and all tests passed:
>>
>>$self -> assert( $target -> the_test(), "Test OK" );
>>$self -> assert( ! $target -> the_test(), "Test NOK" );
>>
>>IMHO, it would have been nice if perl could have issued some kind
>>of warning, since relying on 'nothing' as a return value seems...
>>well, not-so-common.

>
>
> I'm affraid that calling functions in a list context is common in
> perl.


Agreed. But it is going to make it more difficult to write test
cases for methods erroneously using return; (or empty) instead of
a valid value, as some tests will have to create the needed
context (or at least when using Test::Unit::TestCase.)

Thanks for the link to the discussion, that was interesting reading.

Cheers,

/Gunnar
 
Reply With Quote
 
Brian McCauley
Guest
Posts: n/a
 
      06-09-2004
Gunnar Strand <(E-Mail Removed)> writes:

> Brian McCauley wrote:
> > Gunnar Strand <(E-Mail Removed)> writes:

> [...]
> >>sub myvoid {
> >>}

> > That is not a subroutine returning nothing.
> > That is an empty function.
> > In a list context an empty function retunrns its arguments.

>
> It returns an empty list in a list context according to perlmod,
> and perl -e 'sub a{};print a(1, 2, 3)' prints nothing.


You are right, it does in 5.8. I was unaware that it has changed.

> > How ever I wouldn't consider this to be defined bevaviour. I
> > would be inclined to consider the behaviour of an empty function
> > wrt what it returns to be undefined.


Which, of course, is now bourne out to be very good advice wrt 5.6
because the behaviour in 5.8 is indeed different.

You say the new behaviour is documented in perlmod? I think you meant
perlsub.

--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      06-09-2004
Brian McCauley <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> Gunnar Strand <(E-Mail Removed)> writes:
>
> > Brian McCauley wrote:
> > > Gunnar Strand <(E-Mail Removed)> writes:

> > [...]
> > >>sub myvoid {
> > >>}
> > > That is not a subroutine returning nothing.
> > > That is an empty function.
> > > In a list context an empty function retunrns its arguments.

> >
> > It returns an empty list in a list context according to perlmod,
> > and perl -e 'sub a{};print a(1, 2, 3)' prints nothing.

>
> You are right, it does in 5.8. I was unaware that it has changed.


It doesn't for me in 5.8.1, that is, it prints "123". (I'll check
5.8.3 later). I cannot imagine that an incompatible change like this
would be made. Could some shell deviltry be obscuring the output?

> > > How ever I wouldn't consider this to be defined bevaviour. I
> > > would be inclined to consider the behaviour of an empty function
> > > wrt what it returns to be undefined.

>
> Which, of course, is now bourne out to be very good advice wrt 5.6
> because the behaviour in 5.8 is indeed different.
>
> You say the new behaviour is documented in perlmod? I think you meant
> perlsub.


I can't find the *old* (and, I suspect, still current) behavior described
in the corresponding version of perldoc, and I was never aware of it
until now. (Speak of not knowing about fundamental language details...)
It could be taken as a consequence of the "last expression evaluated"
rule, though the argument list is nowhere visibly evaluated in the sub.

Anno
 
Reply With Quote
 
Gunnar Strand
Guest
Posts: n/a
 
      06-09-2004
Anno Siegel wrote:
> Brian McCauley <(E-Mail Removed)> wrote in comp.lang.perl.misc:
>
>>Gunnar Strand <(E-Mail Removed)> writes:
>>
>>
>>>Brian McCauley wrote:
>>>
>>>>Gunnar Strand <(E-Mail Removed)> writes:
>>>
>>>[...]
>>>
>>>>>sub myvoid {
>>>>>}
>>>>
>>>>That is not a subroutine returning nothing.
>>>>That is an empty function.
>>>>In a list context an empty function retunrns its arguments.
>>>
>>>It returns an empty list in a list context according to perlmod,
>>>and perl -e 'sub a{};print a(1, 2, 3)' prints nothing.

>>
>>You are right, it does in 5.8. I was unaware that it has changed.

>
>
> It doesn't for me in 5.8.1, that is, it prints "123". (I'll check
> 5.8.3 later). I cannot imagine that an incompatible change like this
> would be made. Could some shell deviltry be obscuring the output?
>


I am using v5.8.4 built for i386-linux-thread-multi, so it
sounds like it have changed very recently.

>>>>How ever I wouldn't consider this to be defined bevaviour. I
>>>>would be inclined to consider the behaviour of an empty function
>>>>wrt what it returns to be undefined.

>>
>>Which, of course, is now bourne out to be very good advice wrt 5.6
>>because the behaviour in 5.8 is indeed different.
>>
>>You say the new behaviour is documented in perlmod? I think you meant
>>perlsub.

>
>
> I can't find the *old* (and, I suspect, still current) behavior described
> in the corresponding version of perldoc, and I was never aware of it
> until now. (Speak of not knowing about fundamental language details...)
> It could be taken as a consequence of the "last expression evaluated"
> rule, though the argument list is nowhere visibly evaluated in the sub.


Brian's correct, it's in the perlsub page (mental lapse on
my part .

Regards,

/Gunnar
 
Reply With Quote
 
Tassilo v. Parseval
Guest
Posts: n/a
 
      06-09-2004
Also sprach Gunnar Strand:

> Anno Siegel wrote:
>> Brian McCauley <(E-Mail Removed)> wrote in comp.lang.perl.misc:
>>
>>>Gunnar Strand <(E-Mail Removed)> writes:


>>>>It returns an empty list in a list context according to perlmod,
>>>>and perl -e 'sub a{};print a(1, 2, 3)' prints nothing.
>>>
>>>You are right, it does in 5.8. I was unaware that it has changed.

>>
>>
>> It doesn't for me in 5.8.1, that is, it prints "123". (I'll check
>> 5.8.3 later). I cannot imagine that an incompatible change like this
>> would be made. Could some shell deviltry be obscuring the output?
>>

>
> I am using v5.8.4 built for i386-linux-thread-multi, so it
> sounds like it have changed very recently.


This appears to have been changed in 5.8.2. 5.8.1 is the last release
where empty functions return their arguments for me.

Tassilo
--
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus}) !JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexi ixesixeseg;y~\n~~dddd;eval
 
Reply With Quote
 
Brian McCauley
Guest
Posts: n/a
 
      06-09-2004
"Tassilo v. Parseval" <(E-Mail Removed)> writes:

> Also sprach Gunnar Strand:
>
> > Anno Siegel wrote:
> >> Brian McCauley <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> >>
> >>>Gunnar Strand <(E-Mail Removed)> writes:

>
> >>>>It returns an empty list in a list context according to perlmod,
> >>>>and perl -e 'sub a{};print a(1, 2, 3)' prints nothing.
> >>>
> >>>You are right, it does in 5.8. I was unaware that it has changed.
> >>
> >> It doesn't for me in 5.8.1, that is, it prints "123". (I'll check
> >> 5.8.3 later). I cannot imagine that an incompatible change like this
> >> would be made.


I don't think any sane person would claim that the old behaviour was
defined so I don't really see that compatability is an issue.

> > I am using v5.8.4 built for i386-linux-thread-multi, so it
> > sounds like it have changed very recently.

>
> This appears to have been changed in 5.8.2. 5.8.1 is the last release
> where empty functions return their arguments for me.


But, strangely, 5.8.2 is not the first (even) release where empty
functions don't return their arguments. They didn't in 5.8.0 either.

--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
re module: Nothing to repeat, but no sre_constants.error: nothing torepeat ? Devin Jeanpierre Python 2 02-14-2012 01:33 PM
Death To Sub-Sub-Sub-Directories! Lawrence D'Oliveiro Java 92 05-20-2011 06:50 AM
Returning Nothing from Function James ASP .Net 9 04-21-2008 01:53 PM
Recognising Sub-Items and sub-sub items using xslt Ben XML 2 09-19-2007 09:35 AM
Problems with IE returning nothing in responseText furby Javascript 5 09-18-2006 02:40 PM



Advertisments