Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^14: The current state of Perl6

by BrowserUk (Pope)
on Apr 27, 2010 at 09:52 UTC ( #837068=note: print w/ replies, xml ) Need Help??


in reply to Re^13: 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

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.


Comment on Re^14: The current state of Perl6
Select or Download Code
Replies are listed 'Best First'.
Re^15: The current state of Perl6
by Anonymous Monk on Jan 28, 2015 at 01:05 UTC

    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
Re^15: The current state of Perl6
by Anonymous Monk on Apr 28, 2010 at 20:01 UTC
    it's a language for sysadmins then, not for programmers. that's why it's not going to succeed in programming, but in glueing stuff together and continuing to be a mediocre scripting language.

      Your arguments are getting weaker. Just be you can write it that way, it does mean you have to.

      You could write it as below. And if that makes you feel that you've done a "proper job", then you're welcome to, but it'll just take you longer, be no more "reliable"; and is no more good or "proper programming" than the one-liner.

      #! perl use strict; use warnings; use constant { FILENAMEROOT => 'split.', OPEN_MODE_OUTPUT => '>', OPEN_MODE_INPUT => '<', SELECTOR_OFFSET => 3, SELECTOR_LENGTH => 1, ZERO => 0, ONE => 1, TEN => 10, }; ############################ # Main program starts here # ############################ ################################ # An array to hold filehandles # ################################ my @fileHandles; ###################################### # iterate from 0 to 9 # # compose a filename incorporating # # the number of the split file # # open the file for output # # check that it opend successfully # # push the filehandle onto the array # # of filehandles index by the number # ###################################### for( my $index = ZERO; $index < TEN; $index = $index + ONE ) { ## A varible to hile the composed filename my $fileName = FILENAMEROOT . $index; # another to hold the file handle my $fileHandle; # open the file my $rc = open $fileHandle, OPEN_MODE_OUTPUT, $fileName; # check it opened successfully, or die if( $rc == 0 ) { die "Couldn't open " . $fileName . " for output; rc = " . $rc . "; Errortext = " . $!; } # else add it to the array of filehandles else { push @fileHandles, $fileHandle; } } die "You must supply an input filename" unless scalar @ARGV == 1; # get the name of the input file my( $inputFileName ) = @ARGV; die "The filename you supply must exist" unless -e $inputFileName; die "The file must contain something" if -z $inputFilename; # open the input file $rc = open my $inputFileHandle, INPUT_MODE, $inputFileName; # Did it open? Die if not. if( not $rc == 0 ) { die "couldn't open $inputFileName: $!"; } # iterate over the input file one line at a time while( defined( my $inputLine = readline( $inputFileHandle ) ) ) { # extract the character by which we select # the output file to put this line in my $selectorCharacter = substr( $inputLine, SELECTOR_OFFSET, SELECTOR_LENGTH ); # check that it is a digit, otherwise die if( $selectorCharacter lt '0' or $selectorCharacter gt '9' ) { die "Selector character is not a digit: " . SelectorCharacter; } # selector character is okay else { # so get the appropriate filehandle from the array my $fileHandle = @fileHandles[ ord( $selector ) - ord( S$selectorCharacter ) ]; # check that we actually got a filehandle, or die if( not defined $fileHandle ) { die "Couldn't obtain fileHandle for Selector character " . $selectorCharacter; } # okay, we've got a filehandle else { # so output the file handle to the appropriate file print $fileHandle $inputLine; } } } # close the input file close $inputFileHandle; # iterate over the output filehandles array for( my $fileHandleIndex = 0; $fileHandleIndex < 10; $FileHandleIndex = $fileHandleIndex + ONE ) { # get the output filehandle my $fileHandle = $fileHandles[ $fileHandleIndex ]; # make sure that it hasn't gone walkies in teh last 3 milliseconds if( not defined $fileHandle ) { # and die if it has (leaving all the other filehandles unclose +d; but what can you do?) die "Couldn't obtain fileHandle for index " . $fileHandleIndex +; } # okay, we;ve got a filehandle else { # so now we can close it. (Shoudl we check that it actually cl +osed successfully?) close $fileHandle; } } ###################################################################### +######## ###################################################################### +######## ## + ## ## EEEEE N N DDDD PPPP RRRR OOO GGG RRRR A M + M ## ## E NN N D D P P R R O O G G R R A A M +M MM ## ## E N N N D D P P R R O O G R R A A M + M M ## ## EEEE N NN D D PPPP RRRR O O G GG RRRR AAAAA M + M ## ## E N N D D P R R O O G G R R A A M + M ## ## E N N D D P R R O O G G R R A A M + M ## ## EEEEE N N DDDD P R R OOO GGG R R A A M + M ## ## + ## ###################################################################### +######## ###################################################################### +######## exit;

      More just a wannabe C programmer using Perl...

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://837068]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (8)
As of 2015-09-03 18:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred temperature scale is:










    Results (123 votes), past polls