http://www.perlmonks.org?node_id=435408


in reply to Perl oddities

Perl doesn't DWIM in:
sub add ($$) {$_[0] + $_[1]} my @arr = (3, 4); print add(@arry);
It issues a warning and prints 2, not the 7 I'd expect.

Perl doesn't DWIM in:

sleep 2.5;

You can tie some magical variables, and get the expected effects, but with other magical variables, no subroutines tied to them are ever called.

$ perl -wle 'print+(localtime())[5]' 105
C all over again.

brian, are you going to collect the results here, and give them to Larry before Perl6 comes out? ;-)

Replies are listed 'Best First'.
Re^2: Perl oddities
by TimToady (Parson) on Mar 01, 2005 at 20:00 UTC
    Perl 6's flattening rules are completely revised so that hashes and arrays don't actually have to care whether they're in scalar or list context until they're bound to parameters. Nevertheless, what you're saying there still won't work. If you want to treat an array as a list of parameters where Perl 6 is expecting a scalar, you have to use an explicit unary * to tell it that's what you want.

    Fractional seconds weren't supported by most versions of Unix when Perl was invented, but that has changed for the better over the years. In Perl 6 most times will be represented in floating-point seconds, so a fractional sleep should do what you expect.

    Tying and magic are done entirely differently in Perl 6 (through mixins and such), though you can never get entirely away from the fact that some semantics are going to hide other semantics unless you construct a class that specifically knows about both sets of inherited or composed semantics. However, it should work more like you expect, insofar as the latest layer of semantics should hide the built-in magic, and not vice versa.

    As for localtime, it was always intended to be a thin interface over C. But Perl 6 will give an OO interface to most of the list-returning functions, and you'll get at the values with methods on the returned object. So we can fix the year+1900 problem at least for the method call.

    I'm not brian, but I can probably pass these on to Larry before Perl 6 comes out... :-)

Re^2: Perl oddities
by merlyn (Sage) on Mar 01, 2005 at 19:41 UTC
    Perl doesn't DWIM in:
    sleep 2.5;
    But see:
    use Time::HiRes qw(sleep); ... sleep 2.5;
    You can't make core Perl do this, because the number of systems on which this worked was once miniscule compared to the number of systems on which it could never work.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

      merlyn,
      You can't make core Perl do this

      To the letter of the law, that's true. In spirit though, select(undef, undef, undef, 2.5); is usually close enough. It would be nice if you didn't have to use something so ugly with core Perl though.

      Cheers - L~R

      You can't make core Perl do this

      Did I say we should change Perl to allow this? No. Do you understand the premises of this thread? brian was asking for what people consider oddities of Perl thinking. Now I'm sure any oddity has its reason for being odd (after all, it's there the way it is because someone put it there, and not because Larry has a piece of radioactive material next to his computer and he's just watching how the rays mutate Perl) - but that's not the point.

      I expect Perl to DWIM (just like it does in many other things), and if on my system select undef, undef, undef, 2.5 waits for about 2500 milliseconds, then there's no reason to assume sleep 2.5 can't.

Re^2: Perl oddities
by itub (Priest) on Mar 01, 2005 at 15:55 UTC
    Just avoid prototypes and you will be happier. 99.9% of the time they aren't needed and often they hurt, like in this case.
Re^2: Perl oddities
by jplindstrom (Monsignor) on Mar 01, 2005 at 20:55 UTC
    sleep 2.5;
    works if you
    use Time::HiRes qw/sleep/;

    The alternative

    select(undef, undef, undef, 2.5);
    looks too much like a hack and isn't very clear.

    /J

    Update: I can't believe I didn't scroll down to see the existing answers to this question :)