Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search

[REBOL] Re: Perl is to stupid to understand this 1 liner.

From: joel:neely:fedex at: 15-Dec-2001 5:12

Hi, Carl, Carl Read wrote:
> I planned to use a rule at the start, but then noticed that just a > check of length would do the trick. But then Joel changed the > rules... (; >
Moi? "Changed"? Mais non! Just "clarified" for those who don't read REs (but can't everybody read regular expressions? ;-)
> > > Well, on the other hand > > not so straightforward as your Perl example ... > > Perhaps Joel would now like to write a version in Perl that's > designed to be as clear as possible as apposed to as short as > possible? >
To the everyday Perl hacker, what I published actually is both! No, really! ;-) The only things I could do to obviousify the script would be to write an explicit read/print loop (that's what the -p switch does) and embed comments in the RE that is the match pattern for the substitution. That would give us something like the following: 8<---------- #!/usr/bin/perl -w while (<>) { # loop over all input (all file arguments) s/ # substitute in current line for this pattern... \b # boundary (e.g. whitespace or beginning of line) ( # begin subpattern \d{3} # exactly three digits - # followed by a hyphen )? # end subpattern and make it optional \d{3} # exactly three digits - # followed by a hyphen, \d{4} # exactly four more digits, and \b # a boundary (whitespace or end of line) /####/gx; # ... four octothorps wherever possible print; # print current line after substitution(s) } 8<---------- but the addition of all the comments is hardly an improvement to a Perl programmer. That would be like showing someone who knows elementary algebra a paragraph of text that describes the quadratic formula, instead of simply writing (pardon the ASCII art...) ____________ + / 2 -b - / b - 4 a c V ------------------- 2 a The notation really is intended to be minimalist; the price of using it is taking a little time to learn something new, as is the case with all programming languages. I suspect the person that has never seen REBOL before would find the comparable PARSE rule less than obvious as well. Of course, I could define variables to hold the parts of the RE and give them mnemonic names ... 8<---------- #!/usr/bin/perl -w my $areacode = '(\d{3}-)?'; # 3 digits and hyphen, optional my $exchange = '\d{3}-'; # 3 digits and hyphen my $localine = '\d{4}'; # 4 digits my $phonenbr = "$areacode$exchange$localine"; while (<>) { # loop over all input s/\b$phonenbr\b/####/gx; # hiding phone numbers in each line print; # and print the line } 8<---------- ... but anyone who knows Perl will see that I had to do something subtle to make that work. Perhaps that would be more readable to some? What would one do in REBOL to make the PARSE rules more obvious to someone who doesn't speak REBOL as a native? -jn- -- The hardest problem in computer science is finding a problem to solve your solution. -- Aaron Watters joel(dot(FIX(PUNCTUATION(neely(at(fedex(dot(com