On Feb 12, 2013, at 3:05 PM, Kevin Walzer <[email protected]> wrote:
> I'm an experienced developer in several other languages (Python, Tcl,
> AppleScript, JavaScript, C/Objective C), so I'm quite familiar with
> structuring a program--but as I work on learning Perl, I find it somewhat
> obscure, if not downright obfuscated. None of the other languages I've worked
> with have the equivalent of the $_ implicit var, for instance. Looking at
> some sample code online, I had to spend a considerable amount of time looking
> up the various bits with sigils and annotating them, cf:
>
> open (INPUT, "< $_"); ##input from default $_ var
> foreach (<INPUT>) {
> if(/$searchstring/i) { ##case-insenstive regex for $searchstring
> $_ = substr($_, 0, 60); ##trim string to 60 chars
> s/^\s*//; #trim leading space
> print "$File::Find::name\:$.\:\t$_\n"; #print filename
> followed by line number followed by tab followed by matching line
>
> }
> }
> close INPUT;
>
> Perhaps this is idiomatic to you, but it's very dense to me. And I have a
> decade of development experience.
There's nothing idiomatic about that. I'd write that code as:
# do whatever needed to get filename out of $_ into $filename here
open( my $INPUT , '<' , $filename ) or die( "Can't open $filename ($!)" );
foreach my $line ( <INPUT> ) {
if( $line =~ /$searchstring/i ) {
my $trimmed_line = substr( $line , 0 , 60 );
$trimmed_line =~ s/^\s*//; ## NOTE: Possible logic bug;
## $trimmed_line now may be < 60 chars
printf "%s:%s:\t%s\n" , $filename , $. , $trimmed_line;
}
}
close( $INPUT );
I might initially write the foreach loop and the regex with $_, but as soon as
I hit that substr, it would be named variables all the way.
> All kidding aside, perhaps one way the OP could obfuscate his code is to
> deploy it in a PAR file--how hard are those to unwrap? Does Perl have the
> equivalent of Python bytecode files, i.e. pyc, that are obfuscated? If not,
> the OP's options may be limited.
No such critter in Perl.
j.
--
John SJ Anderson // [email protected]
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/