Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Re: print 42

by BooK (Curate)
on Jan 18, 2001 at 14:10 UTC ( #52737=note: print w/replies, xml ) Need Help??

in reply to print 42

It doesn't work for me. Prints:
Not enough arguments for unpack at line 2, near "qw&u >>7,@2D%U='ET96YR;VAY96P@(%!R>6-E2')A:RP*&) "

Replies are listed 'Best First'.
Re: Re: print 42
by chipmunk (Parson) on Jan 18, 2001 at 21:59 UTC
    There are actually two things happening here that cause this code to require 5.6.

    The prototype for unpack wants the first argument (the format) to be a scalar value. The first argument is actually evaluated in a scalar context.

    @args = ('a5', 'qwert'); unpack @args; # not okay; equivalent to unpack 2 +; unpack $args[0], @args[1..$#args]; # okay, format arg followed by dat +a args
    The reason why Anonymous Monk's code works in 5.6 but not in 5.005 is due to a change in the implementation of qw//. As you know, qw/STRING/ is equivalent to split ' ', 'STRING'. In 5.005, the split is performed at runtime, but in 5.6, the split occurs at compile time! Thus, given this code: unpack qw/a5 qwert/;
    perl5.005 compiles it as: unpack split ' ', 'a5 qwert';
    which puts split in a scalar context, and leaves unpack with only one argument.
    perl5.6, on the other hand, compiles it as: unpack 'a5', 'qwert';
    and unpack is happy.

    In conclusion, here's a version of Anonymous Monk's JAPH which runs in earlier versions of perl:

    ($#=unpack u, q&>>7,@2D%U='ET96YR;VAY96P@(%!R>6-E2')A:RP*&) =~s:y(.)(.)(.)(.)(.)(.):\3\5\1\6\2\4:g; print 42;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://52737]
and the shadows deepen...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2017-02-24 10:57 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (354 votes). Check out past polls.