develooper Front page | perl.perl5.porters | Postings from May 2012

[perl #9528] Bug when testing array returned by sort operator

Thread Next
From:
Brian Fraser via RT
Date:
May 25, 2012 17:37
Subject:
[perl #9528] Bug when testing array returned by sort operator
Message ID:
rt-3.6.HEAD-7788-1337992647-1328.9528-15-0@perl.org
On Wed Aug 04 21:03:53 2004, davidnicol wrote:
> 
>  here's a
> patch that adds a runtime sort-in-scalar-context warning, which, had it
> existed in June of 2002, would have reported the situation.
> 
> Before:
> ./perl -wle 'sub foo { return sort("dog", "cat", "fish") } $x = foo();\
>  print $x'
> Use of uninitialized value $x in print at -e line 1.
> 
> After:
> ./perl -wle 'sub foo { return sort("dog", "cat", "fish") } $x = foo();\
>  print $x'
> sort called in dynamic non-array context, try ()=sort ... at -e line 1.
> Use of uninitialized value $x in print at -e line 1.
> 
> 
> 
> 
> --- perl-current/pp_sort.c      Sat Jul 31 11:44:58 2004
> +++ dynamic-sort-in-scalar-warning/pp_sort.c    Mon Aug  2 23:01:55 2004
> @@ -1508,8 +1508,13 @@
>      void (*sortsvp)(pTHX_ SV **array, size_t nmemb, SVCOMPARE_t cmp)
>        = Perl_sortsv;
>  
>      if (gimme != G_ARRAY) {
> +        Perl_warner(
> +          aTHX_ packWARN3(WARN_VOID,WARN_AMBIGUOUS,WARN_SYNTAX),
> +           "sort called in dynamic non-array context,"
> +          " try ()=sort ..."
> +       );
>         SP = MARK;
>         RETPUSHUNDEF;
>      }
>  
> 
> 

Some bikeshedding here: If the warning the applied, I think that it
should be consistent and merely say "Useless use of sort in scalar
context". "dynamic non-array" context is a mouthful, and nothing in the
docs ever refers to something like that; It would only cause more confusion.

--hugmeir


---
via perlbug:  queue: perl5 status: open
https://rt.perl.org:443/rt3/Ticket/Display.html?id=9528

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About