szabgab has asked for the wisdom of the Perl Monks concerning the following question:

my ($x, $y, $z) = f();
In code like above if f() returns more than 3 or less than 3 values perl would silently disregard this and go on doing its business. This cannot be checked at comile time but is there any way to get at least a warning about such cases in run time? Can that maybe turned into a FATAL error? If that could be done, then probably it would also work on the following code as well:
my ($x, $y, $z) = @_;

Replies are listed 'Best First'.
Re: Incorrect number of values in list assignment
by ikegami (Patriarch) on Sep 06, 2010 at 06:16 UTC
    ( my ($x, $y, $z) = f() ) == 3 or die();
Re: Incorrect number of values in list assignment
by Utilitarian (Vicar) on Sep 06, 2010 at 05:50 UTC
    A quick example may suggest an approach
    $ perl -e '@array=qw(0 1 2 3 4 5); $count = ($i, $j, $k)= @array; prin +t "$count\n";' 6

    print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."
Re: Incorrect number of values in list assignment
by BrowserUk (Patriarch) on Sep 06, 2010 at 06:13 UTC

    This requires the use of (shock!horror!) a global, but ...

    c:\test>perl -E" sub x{(1) x int(rand 5)}; scalar(our@a=x())==3 and my($x,$y,$z)=@a or die qq[Trouble [@a]\n]; say qq[x:$x y:$y z:$z] " Trouble [] c:\test>perl -E"sub x{(1) x int(rand 5)}; scalar(our@a=x())==3 and my( +$x,$y,$z)=@a or die qq[Trouble [@a]\n]; say qq[x:$x y:$y z:$z]" Trouble [1 1 1 1] c:\test>perl -E"sub x{(1) x int(rand 5)}; scalar(our@a=x())==3 and my( +$x,$y,$z)=@a or die qq[Trouble [@a]\n]; say qq[x:$x y:$y z:$z]" Trouble [] c:\test>perl -E"sub x{(1) x int(rand 5)}; scalar(our@a=x())==3 and my( +$x,$y,$z)=@a or die qq[Trouble [@a]\n]; say qq[x:$x y:$y z:$z]" x:1 y:1 z:1

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Incorrect number of values in list assignment
by chromatic (Archbishop) on Sep 06, 2010 at 06:14 UTC

    You'd have to write a custom op and install that with a special pragma to get the scoping right (and checking the pragma hints is the most difficult part in the custom op), but it's doable for someone with XS skills. I'm sure it'd break a lot of code though.

      I wonder if it would be possible to add optional warnings so the behavior of "use warnings" won't change but I'd be able to turn this warning on. Maybe with an additional line of "use warnings 'count_elemnts'".

        Sure, that's even easier to implement than a custom op—probably a dozen lines of code. I don't know if p5p would favor it though.

Re: Incorrect number of values in list assignment
by Utilitarian (Vicar) on Sep 06, 2010 at 05:50 UTC
    Double post, DELETED