Thread: Fatty's Perl Top 5 Fatty
Top 5 things for
perlnewbies to do/learn1. Use strict. Always. It forces certain proper behaviours on you and finds most common errors at compile time. #!/usr/bin/ perluse strict; my $foo; # predeclare the scalar $fpp = "test" # $fpp undefined because you can't type pro perly, this won't passperl-c2. Learnto use the data structures properly. Especially hashes. But especially refsmy $ref; # placeholder $ref->{foo} = "bar"; # hashref $ref->{foo}->[0] ="bar"; # now a hashref pointing at an arrayref 3. Data:umper - makes sense of fished up structures like the above $ cat /tmp/test.pl #!/usr/bin/ perluse strict; use Data:umper; my $foo; $foo->{bar}->[0] = "baz"; print Dumper $foo; $ perl/tmp/test.pl$VAR1 = { 'bar' => [ 'baz' ] }; 3. Learnto use theperldebugger. Putting prints in your code gets real old, real fastperl-d foo.pl4. Think about your code because it's really easy to make perlcode that sucks... Devel:rofile is a good module to see which functions get called the most and how much time they take. Avoid making the sameregexover and over -- save your variables or the results of expensive calculations in a structure.$foo =~ /^(d+)/; my $firstnumbers = $1; # whatever was in parens ($firstnumbers) = ($foo =~ /^(d+)/); # one line format, a regexreturns an array of ($1, $2, ...)In the last one don't forget to put $firstnumbers in parens because it casts it to an array (in scalar context $~ returns the number of matches, not the values) Similarly, try to anchor your regexps (^ $) to improve efficiency. Avoid .*, especially multiple .*'s in the sameregexp because it probably won't behave the way you think it will .*? is a non-greedy version, google "perlbacktracking" to find more5. Learnabout theperlone liners, especially -p/-n and -i.perl-p -i.bak -e 's/fish/fsck/g' *.phpwill replace fish with fsck in all of the . phpfiles, saving the old file with a .bak extensionperkiset
That's great stuff fatty - nice post
|
Thread Categories
Best of The Cache Home | ||
Search The Cache |
- Ajax
- Apache & mod_rewrite
- BlackHat SEO & Web Stuff
- C/++/#, Pascal etc.
- Database Stuff
- General & Non-Technical Discussion
- General programming, learning to code
- Javascript Discussions & Code
- Linux Related
- Mac, iPhone & OS-X Stuff
- Miscellaneous
- MS Windows Related
- PERL & Python Related
- PHP: Questions & Discussion
- PHP: Techniques, Classes & Examples
- Regular Expressions
- Uncategorized Threads