Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
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
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 chilling in the Monastery: (13)
As of 2014-10-21 20:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (110 votes), past polls