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


in reply to Re^12: The current state of Perl6
in thread The current state of Perl6

I've no idea what that does and I've been coding Perl for some time now. That's one of the problems of Perl... probably the reason why it will succumb

Replies are listed 'Best First'.
Re^14: The current state of Perl6
by BrowserUk (Patriarch) on Apr 27, 2010 at 09:52 UTC
    I've no idea what that does and I've been coding Perl for some time now. That's one of the problems of Perl... probably the reason why it will succumb

    Ah! So, according to you, Perl will "succumb" because you don't understand something, and can't be bothered to work it out. That's not "one of the problems with Perl"; it's one of the problems with some of the people that program in Perl.

    Let me see if I can't break it down for you. Not the code; your resistance to understanding it.

    perl -e"open $fh[$_],'>','split.'.$_ for 0..9" -nle"print {$fh[substr $_,3,1]} $_" big.log

    I'm going to assume that you've used perl one-liners before--say perl -le" print for 1 .. 10 "? (If not see perlrun.)

    Well this command consists of 2 -e"..." arguments. They are executed in turn in the order you see them:

    1. The first -e"open $fh[$_],'>','split.'.$_ for 0..9"

      Opens 10 files, called split.0 through split.9, for output, and stores their handles in global array @fh.

    2. The second -nle"print {$fh[substr $_,3,1]} $_" big.log
      • uses the -n switch to ask perl to read the file big.log one line at a time

        and call the code between the "...", with $_ set to each line in turn.

      • And the bit in quotes print {$fh[substr $_,3,1]} $_

        prints each line ($_) to one of the files opened above.

      • Which file gets which lines is decided by extracting one character from the line: substr $_,3,1,

        which will be a digit in '0' .. '9',

      • And using that to index into the array of filehandles we opened { $fh[ ... ] }.

        And the reason the $fh{ ... ] is in curlies, is because otherwise perl would not recognise the array element as a target filehandle and would instead print its value (and the line) to STDOUT.

    There's nothing magical nor complicated nor obscure about any of that. It's all well documented and quite intuative once you've read & understood it. And it is exactly why so many sysadmins use Perl. Because it allows them to do off-the-cuff tasks--like splitting up a log file for easier handling--right there at the terminal without even breaking their main train of thought.

    If they had to use C, or assembler or Java or C++, they'd have to start an editor, lookup apis, compile, debug, test, iterate. And by the time they'd finshed and got back to what they were doing, they've forgotten why they needed to do it in the first place.

    With just a little reading and a little practice, they type that one-liner and run it, without losing their train of thought, so intuative and well thought through are Perl(5)'s features.

    But that didn't happen by accident. It is the result of

    • the experience of what is wrong (and right) with other "scripting tools" by the designer;
    • an iterative design & development process over many cycles;
    • (perhaps) a designer who has an inate feel for when something isn't quite right and the balls to stick it out until it is;

    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.

      I was going to reply to Re^13: The current state of Perl6 and say Basic debugging checklist, its for you too

      But, all the code seems to be inside the LOOP

      $ perl -MO=Deparse,-p -e"open $fh[$_],'>','split.'.$_ for 0..9;" -nl +e"print {$fh[substr $_,3,1]} $_" big.log BEGIN { $/ = "\n"; $\ = "\n"; } LINE: while (defined(($_ = <ARGV>))) { chomp($_); open($fh[$_], '>', ('split.' . $_)) foreach (0 .. 9); print({$fh[substr($_, 3, 1)];} $_); } -e syntax OK

      I only do one -e and use BEGIN{} blocks for one-time global stuff

        I have no explanation for you. It is too long ago for me to remember making the post; though I remembered posting the over expanded version, hence referencing it in the new thread.

        I didn't think I would have posted it if it didn't work; but I've just tried it on the oldest Perl I have still kicking around -- 5.8.6 -- and get exactly the same compilation errors as I do for my current build; which strongly suggests it never did worked.

        This does work:

        E:\test>c:\DELL\bin\perl5.8.6.exe -e"BEGIN{ open $fh[$_],'>','split.'. +$_ for 0..9 }" -nle"print {$fh[substr $_,3,1]} $_" junk.log E:\test>dir Volume in drive E is New Volume Volume Serial Number is 1821-B83A Directory of E:\test 28/01/2015 02:20 <DIR> . 28/01/2015 02:20 <DIR> .. 28/01/2015 02:13 1,900 junk.log 28/01/2015 02:20 190 split.0 28/01/2015 02:20 190 split.1 28/01/2015 02:20 190 split.2 28/01/2015 02:20 190 split.3 28/01/2015 02:20 190 split.4 28/01/2015 02:20 190 split.5 28/01/2015 02:20 190 split.6 28/01/2015 02:20 190 split.7 28/01/2015 02:20 190 split.8 28/01/2015 02:20 190 split.9 11 File(s) 3,800 bytes 2 Dir(s) 1,624,181,084,160 bytes free

        Though as you suggested, using 2 -e's is unnecessary.

        My best guess is that I was trying to work around the 70 character code section wrap point when posting; and thought splitting the command into an -e and an -nle would achieve the desired affect; but never actually tried it.

        Update:FWIW, IMO, the fact that an -e, that precedes an -nle, gets amalgamated into the latter's loop; is a bug.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        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". I'm with torvalds on this
        In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked
    A reply falls below the community's threshold of quality. You may see it by logging in.