in reply to understanding eval in HPOJ

This looks at first glance that it was intended as a polyglot incantation. That means it is meant for BOTH bash and for perl to read and act accordingly. These are very ugly and fickle, especially if something changes in the operating environment.

There's a related example in perlrun, which may shed some light. The key features of these perl/shell polyglots are that they

IF perl runs it, AND the internationalization $ENV{LANG} isn't set to the POSIX C setting, THEN the script will try to RESTART the script via the shell, with the appropriate environmental fix.

IF on the otherhand, a shell runs it, THEN the if line is completely ignored since it doesn't get that far. The exit/exec pair see to that.

The tortured use of confusing clauses like eval 'somestring' are just a side effect to ensure that it does the right thing in both cases, errors and warnings be damned.

On Linux and many Un*x varieties, the kernel reads the shebang line and decides that the perl interpreter must be used if not otherwise specified. On DOS/Windows, and on some Un*x varieties, there is no such assumption that the shebang must be inspected. So these polyglots are sometimes used to ensure that the right interpreter is run. (CMD.EXE/PERL.EXE polyglots are even uglier.)

(Sorry I'm not at a station where I can play with this further.)

--
[ e d @ h a l l e y . c c ]

Replies are listed 'Best First'.
Re: Re: understanding eval in HPOJ
by GhodMode (Pilgrim) on Apr 06, 2004 at 04:27 UTC

    I found a solution which I should've tried initially. I started by putting this in there after the variable was supposedly set:

    system( 'echo "LANG=|$LANG|" | /bin/mail "LANG environment variable" m +yuser' );

    Then I rebooted and found that it was not set like it should've been. It was set to "en_US.UTF-8", which I think it gets from the file /etc/sysconfig/i18n. I actually thought it might not even be getting past the goofy line because the first part is an exit.

    I commented the two goofy lines and put the BEGIN block that I mentioned in the earlier post in there and rebooted again. I was pleased to see that my email says LANG=|C| and I checked the process listing and found that my printer daemon is running.

    I don't fully understand what's happening in there, but thanks to halley's explanation, I get the idea. I think my solution will only work in my environment and is not as versatile as the original author intended. I'll read perlrun in a minute to see if I understand better.

    Thanks to all for the brain cells. I'll post the problem and my solution to hpoj-devel and post here if they give me a Perlish solution and/or explanation.

    --
    -- GhodMode