Hidden Features Of Perl, PHP, Javascript, C, C++, C#, Java, Ruby, Python, And Others [Collection Of Incredibly Useful Lists]
Sunday, June 21st, 2009
Updated: June 23rd, 2009
Introduction
StackOverflow is an amazing site for coding questions. It was created by Joel Spolsky of joelonsoftware.com, Jeff Atwood of codinghorror.com, and some other incredibly smart guys who truly care about user experience. I have been a total fan of SO since it went mainstream and it's now a borderline addiction (you can see my StackOverflow badge on the right sidebar).
The Story
Update 6/21/09 9:30pm PST: This server is currently under very heavy load (10-200), even with caching plugins enabled. Please bear with me as I try to resolve the situation.
Feel free to bookmark this page and return to it later when the fires have been put out.
Update 6/21/09 11:37pm PST: I think I've got the situation under control now. The load is between 0 and 3 now and pages load relatively fast. I will be posting about the getting redditted/delicioused experience later.
Update 6/23/09 12:06am PST: Added jQuery, Greasemonkey, Ruby on Rails, and Objective-C, broke databases into their own section, and sorted everything alphabetically.
Update 6/23/09 7:10pm PST: Added Scala, Lua, TCL, F#, Regex, and HTTP.
So, one day someone at StackOverflow started a "Hidden features of" post about a famous language (I don't feel like finding out which one was first exactly), and it turned out to be so popular that other posts in the same series started popping up.
Such questions were quickly turned into community wikis, for the purposes of harvesting and organizing information coming from the best developers on the planet and voted by users of the site. There are literally hundreds of answers, sorted by votes.
Hidden Features Of
Programming Languages
Hidden features of ASP.NET
Hidden features of C
Hidden features of C++
Hidden features of C#
Hidden features of D
Hidden features of Delphi
Hidden Features of F#
Hidden features of Java
Hidden features of JavaScript
Hidden features of Haskell
Hidden features of Lua
Hidden features of Objective-C
Hidden features of Perl
Hidden features of PHP
Hidden features of Python
Hidden features of Ruby
Hidden Features of Ruby on Rails
Hidden features of Scala
Hidden Features of TCL
Hidden features of VB.Net
Databases
Hidden features of MySQL
Hidden features of Oracle
Hidden features of PostgreSQL
Hidden features of SQL Server
Other
Hidden features of Bash - also see my bash cheatsheet.
Hidden features of CSS
Hidden features of Eclipse
Hidden features of Greasemonkey
Hidden features of HTML
Hidden features of HTTP
Hidden features of jQuery
Hidden features of mod_rewrite
Hidden Features of RegEx
Hidden features of Visual Studio (2005-2008)
I will try to maintain this list, adding new languages that join the series as I find them. Now go learn something new!
[Perl] How To Get The Path Of An Included Library (.pm), Regardless Of Current Directory
Thursday, March 5th, 2009
Updated: June 9th, 2009
Problem
While writing a 1093985th Perl script the other day I was facing the following dilemma:
- Let’s say there is a local library, called TheUberLib.pm. It is so uber that most of my scripts, located all over the machine, include it.
- Now, let’s also say that there’s an even more uberly important binary called run_me_now_or_you_will_die but the only way to find it is by using a relative path to the aforementioned TheUberLib.pm, for example ../bin (RELATIVE TO TheUberLib.pm).
- I don’t want to hardcode the path to run_me_now_or_you_will_die because it can be different on multiple machines and the code wouldn’t be robust enough – all I know is that the path is relative to an included library.
So how does one get the path to an included TheUberLib.pm (in my case to deduce the path to run_me_now_or_you_will_die), regardless of the script location and the current directory?
I started looking at all possible ways of getting this information.
- At first I looked at $0. $0 returns the path to the Perl script itself (LibraryPath/lib_path.pl) and is completely useless here.
- The FindBin module that I sometimes use was also of no help as it also deals with the caller script path.
Solution
After digging around for a bit, this is the method I came up with that did exactly what I wanted:
1 2 | use File::Basename;
my $path_to_uber_lib = dirname($INC{'TheUberLib.pm'}); |
and the clean way to get the final path to run_me_now_or_you_will_die is:
1 2 3 | my $relative_path_to_bin_dir = "../the_path_we_want/run_me_now_or_you_will_die";
my $path_to_deadly_bin = Cwd::realpath("$path_to_uber_lib/$relative_path_to_bin_dir");
print "The path to deadly bin is: $path_to_deadly_bin\n"; |
which prints /tmp/LibraryPath/some_path1/the_path_we_want/run_me_now_or_you_will_die.
Here is the full program with all directories that you can check out from SVN:
svn co http://beerpla.net/svn/public/Perl/LibraryPath/
or just browse the code here: http://beerpla.net/svn/public/Perl/LibraryPath/
Explanation
The %INC hash contains all included libraries, with library names as keys and full paths as values.
The dirname() function is part of File::Basename and returns just the directory part of the path.
Finally, Cwd::realpath() is a function that resolves relative and symbolic links to canonical absolute ones.
And there you have it.
Programming Comics: xkcd - 11th-Grade Activities
Friday, December 19th, 2008
Hey, I can relate to today’s xkcd! It hits close to home:
And some more traveling down the Perl related memory lane (yeah, these are old, I know):
My Notes On Learning Python Coming From Perl
Thursday, October 9th, 2008
Updated: November 5th, 2008
I’m learning Python. Since I’m currently primarily a Perl coder, I decided it’d be a good idea to post and keep track of my experiences coming from heavy Perl. I have a few reasons for learning Python, which are:
- to see what else is out there and broaden my horizons.
- to keep my brain active by feeding it new tasty information every day.
- to become a more valuable employee and potential future candidate.
- to learn a new language, especially one used by giants like Google.
- to learn a new OO language, as I’m tired of Perl’s hacky OO.
- to build GUI applications. GUI in Perl is a big pain. I’ll see about Python.
So, without further ado, here are the notes [WORK IN PROGRESS], split by virtual chapters that I made up myself:
Chapter 1.
- I’m using Eclipse for most of my development. E.P.I.C. is the plugin that adds Perl support and it works quite well. For Python, I downloaded the Python plugin called Pydev. It seems to work quite well as well. The only thing so far I wouldn’t use Eclipse for is PHP because PHPEd trumps all competition (though there’s an Eclipse PHP plugin called PHPEclipse). A quick way to see the plugins you have installed just click Help->About.
- Tuples are just immutable lists? a = (‘b’, ‘c’) is immutable while a = [‘b’, ‘c'] is just what a regular list in Perl is. I’m not sure what to make of it yet and whether it was worth it giving up such important syntax as () for that.
- I hate ActiveState’s PythonWin console (the editor itself). Ctrl-Up for history and regular Up for traveling around the code? WTF? Though I do like that there are intellisense-like features in it.
- I miss the $, @, %, etc in front of variables. It’s so easy to tell what is a variable and what is not, what type a variable is, as well as insert it right inside quotes.
- I began to understand how many things in Perl I took for granted.
- A hash is called a dictionary in Python (created with { } ). Hash keys are separated from values via “:” instead of Perl’s “=>”. For example, dict = {“a” : “b”, “c” : “d”}. In Perl “=>” and “,” are the same, but in Python “:” and “,” are very different and cannot be used interchangeably.
- At this point I really like the OO feel of typing in a dictionary name, followed by a dot and getting a list of class methods as opposed to Perl’s non-OO approach. Since I’ve been looking to get back into a more OO language than Perl but not as OO as Java, I think Python may be the right fit. We’ll see…
- Now the book (Wrox Beginning Python) mentions the dict.__contains__() function, which looks internal to me. Why did they not instead use the dict.has_key() function? They look like they do exactly the same thing. Why did they start from functions that are supposed to be hidden in the first place?
- There are special values True, False, and None, which correspond to 1, 0, and undef (?) in Perl.
- list.remove(VAL) function looks useful. I don’t think Perl has anything like it. It removes the first occurrence of value VAL in list.
- If you list.append() another list, it appends a sub-list object, while list.extend() actually extends the list with another list. In Perl list.append() would correspond to pushing an array ref and list.extend() would be pushing an array itself.
- You can compare 2 sequences (lists, tuples, dictionaries) with ==, just like numbers. In Perl, if you try @a == @b, only array lengths will be compared.
- string concatenation is done via “+” vs Perl’s “.”.
1
str_final = str1 + " " + str2
- The print function has a couple of quirks in Python. print implicitly outputs a space between expressions, and it also implicitly outputs \n after the last expression, unless the last expression is followed by a trailing comma (,).
1 2
print "hi", "I'm", print "still on the same line", "but will end the line now"
- Variable substitution is separated from the print statement by “%” for some reason, compared to Perls’ “,”.
1
print "some string: %s" % str1
- One can do something like
1
a, b, c = x
if x is a sequence of 3 items. This would assign a, b, and cy to the items of x, respectively. In Perl, similarly, you could do
1
($a, $b, $c) = @x
- A shortcut for string repetition is
1
print str1 * 10
vs Perl's
1
print $str1 x 10
Chapter 2.
- Here we go: conditional statements if, elif, and else. This is what I’ve been waiting for – as there are no braces needed around conditional statements, loops, etc, indentation plays the role of controlling context. But for *****s sake, PHP has elseif, Perl has elsif, now elif? What’s next, eif? Anyway, a conditional statement is followed by a “:” and then an indented block of lines.
- I like how I don’t need to put parentheses after if. This is similar to Perl’s notation where if comes after the main statement. However, parentheses are mandatory otherwise in Perl.
- Loops have while and for.
Where’s foreach? (panic… noooooooooooooooooooooooo!!! Perl spoiled me.)Actually, I was wrong. It’s exactly the opposite – Python’s for is exactly like Perl’s foreach (thanks Travis!). Rather, Python doesn’t have the c-style for. To break out of a loop, break is used (phew, at least this is expected) vs Perl’s last and to continue, continue is used vs Perl’s next. - Oh, so back to indentation again. Any inconsistent indentation is accompanied by an IndentationError (Inconsistent dedent in Pydev Eclipse) compile time error. I think I like this rule in general, because it encourages proper white space in the code but I have not decided yet. This makes me kind of worried – what if some code gets slightly scrambled and some spaces get lost, for example copy-pasting from a random Internet site or if my blog code displaying tools lose indentation. In Eclipse, a simple Ctrl-Shift-F, which runs Perltidy, can fix any indentation problems and inconsistencies. I don’t know Python, I don’t know…
- This is interesting: a loop can have an else statement. If the loop does not end via break, the else statement triggers. I could see this usage for example: if I’m TODOlooking for something in a hash, you can break the else statement can print “Not found” and act on it.
- Slicing multiple consecutive values in lists is done via “:” rather than Perl’s “..”. Compare list[1:3] vs Perl’s @list[1..3]. . What's interesting is an optional 3rd parameter to the slice, which specifies a stride. Thus, list[::2] would produce a sequence with items that have an even index in list and list[::-1] would reverse the list. Quite handy and original.
- if no action is performed by something that requires a level of indentation (for example, an if statement that does nothing), a special keyword pass needs to be used. In Perl, it would just be an empty set of parentheses.
1 2 3 4
if some_event_that_needs_no_action: pass else: # do stuff
Chapter 3.
- Python seems to have proper exception handling support. A breath of fresh air from Perl’s hacky evals. Try with try, catch with except, and finally with finally. What is the generic Error name that you can use to catch all errors?
- One can raise an error by using a raise keyword.
Chapter 4.
- Functions are defined using the def keyword, as opposed to Perl’s sub.
- Function documentation (docstring) is simply a string that follows the function definition. It is what intellisense-enabled IDEs use (and probably some sort of python documenters). This information is also available as function_name.__doc__. I really like this as I think Perl’s POD style is hideous. I’ll have to see how powerful this docstring can get, because perlpod, though hideous, supports a variety of formatting options.
- dir(object) shows all properties of object. I’m guessing this is another way intellisense IDEs get the information to show a user. For example,
1 2 3
>>> dir(my_func) ['__call__', '__class__', '__delattr__', '__dict__', '__doc__', '__get__', '__getattribute__', '__hash__', '__init__', '__module__', '__name__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__', 'func_closure', 'func_code', 'func_defaults', 'func_dict', 'func_doc', 'func_globals', 'func_name']
- A function can have default parameters, similar to PHP. Perl 5 doesn’t have such construct, it’s only introduced in Perl 6. For example
1
def do_search(string, start = "."):
- A function can be defined within another function and will only be available in the latter’s scope. I am fairly sure Perl allows you to do the same, though I see this functionality as very insignificant.
- type() allows one to determine the type of a variable. For instance, to see if a variable is a dictionary
1
if type(dict) == type({}):
Chapter 5.
- A class is defined using a keyword class vs Perl’s package. Talking about Perl’s class implementation causes vomiting reaction in me, and I’m really hoping Python’s is better. Python is supposed to be a proper OO language and that is partly why I’m learning it. However, one thing is upsettingly common between Python and Perl and that is self in Python and $self in Perl are the first parameters in class member functions, rather than being implied, like in C++ or PHP. So one has to write def add(self, num_list) rather than def add(num_list). Is it because Python is so OO to the bone that it has to have everything declared and nothing implied?
- At first sight, a few things are kind of upsetting actually. The examples in the book don’t define member variables – they just assign them outside of the class definition. Whaaaat? How is this different from Perl then?
- A function is public by default. To make a function private, one needs to prefix it with __. However, if it also ends with __, it’s not private but instead just considered internal (kind of like hidden but not really). An internal function is technically supposed to be used by member methods only but nothing prevents anyone outside the class definition to call it (see the __contains__() example in chapter 1). That’s a bit ambiguous to say the least but still better than Perl’s hacks related to private functions.
- Python doesn’t support protected functions.
- At this point I’m starting to read O’Reilly Python In A Nutshell in parallel with Wrox Beginning Python that I’ve been reading so far. It is much more concise and to the point. Wrox assumes I’m a total noob, even beyond noob. I highly recommend the O’Reilly book.
[WORK IN PROGRESS]
How To Install The Latest SOAP::Lite Using Perl CPAN
Wednesday, April 30th, 2008
Apparently it's not straightforward to install SOAP::Lite, even using CPAN.
Check this out.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | cpan[1]> install SOAP::Lite
CPAN: Storable loaded ok (v2.18)
Going to read /root/.cpan/Metadata
Database was generated on Tue, 29 Apr 2008 18:29:45 GMT
CPAN: YAML loaded ok (v0.66)
Going to read /root/.cpan/build/
............................................................................DONE
Found 149 old builds, restored the state of 109
Warning: Cannot install SOAP::Lite, don't know what it is.
Try the command
i /SOAP::Lite/
to find objects with matching identifiers.
CPAN: Time::HiRes loaded ok (v1.9713) |
Huh? Okay…
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | cpan[2]> i /SOAP::Lite/ Module ResourcePool::Command::SOAP::Lite::Call (MWS/ResourcePool-Resource-SOAP-Lite-1.0101.tar.gz) Module ResourcePool::Factory::SOAP::Lite (MWS/ResourcePool-Resource-SOAP-Lite-1.0101.tar.gz) Module ResourcePool::Resource::SOAP::Lite (MWS/ResourcePool-Resource-SOAP-Lite-1.0101.tar.gz) Module SOAP::Lite::Deserializer::XMLSchema1999 (MKUTTER/SOAP-Lite-0.71.04.tar.gz) Module SOAP::Lite::Deserializer::XMLSchema2001 (MKUTTER/SOAP-Lite-0.71.04.tar.gz) Module SOAP::Lite::Deserializer::XMLSchemaSOAP1_1 (MKUTTER/SOAP-Lite-0.71.04.tar.gz) Module SOAP::Lite::Deserializer::XMLSchemaSOAP1_2 (MKUTTER/SOAP-Lite-0.71.04.tar.gz) Module SOAP::Lite::InstanceExporter (SMEISNER/SOAP-Lite-InstanceExporter-0.02.tar.gz) Module SOAP::Lite::Packager (MKUTTER/SOAP-Lite-0.71.04.tar.gz) Module SOAP::Lite::Simple (LLAP/SOAP-Lite-Simple-1.9.tar.gz) Module SOAP::Lite::Simple::DotNet (LLAP/SOAP-Lite-Simple-1.4.tar.gz) Module SOAP::Lite::Simple::Real (LLAP/SOAP-Lite-Simple-1.4.tar.gz) Module SOAP::Lite::Utility (BRYCE/SOAP-Lite-Utility-0.01.tar.gz) Module SOAP::Lite::Utils (MKUTTER/SOAP-Lite-0.71.04.tar.gz) 14 items found |
Wtf? Let's try something else.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | cpan[8]> i /SOAP.*Lite/ Distribution BRYCE/SOAP-Lite-Utility-0.01.tar.gz Distribution BYRNE/SOAP/SOAP-Lite-0.60a.tar.gz Distribution DYACOB/SOAP-Lite-ActiveWorks-0.10.tar.gz Distribution DYACOB/SOAP-Lite-SmartProxy-0.11.tar.gz Distribution LLAP/SOAP-Lite-Simple-1.4.tar.gz Distribution LLAP/SOAP-Lite-Simple-1.9.tar.gz Distribution MKUTTER/SOAP-Lite-0.71.04.tar.gz Distribution MWS/ResourcePool-Resource-SOAP-Lite-1.0101.tar.gz Distribution SMEISNER/SOAP-Lite-InstanceExporter-0.02.tar.gz Module Catalyst::Action::SOAP::DocumentLiteral (DRUOSO/Catalyst-Controller-SOAP-0.8.tar.gz) Module Catalyst::Action::SOAP::DocumentLiteralWrapped (DRUOSO/Catalyst-Controller-SOAP-0.8.tar.gz) Module Catalyst::Action::SOAP::RPCLiteral (DRUOSO/Catalyst-Controller-SOAP-0.8.tar.gz) Module Catalyst::Controller::SOAP::DocumentLiteralWrapped (DRUOSO/Catalyst-Controller-SOAP-0.8.tar.gz) Module Net::DRI::Transport::HTTP::SOAPLite (PMEVZEK/Net-DRI-0.85.tar.gz) Module ResourcePool::Command::SOAP::Lite::Call (MWS/ResourcePool-Resource-SOAP-Lite-1.0101.tar.gz) Module ResourcePool::Factory::SOAP::Lite (MWS/ResourcePool-Resource-SOAP-Lite-1.0101.tar.gz) Module ResourcePool::Resource::SOAP::Lite (MWS/ResourcePool-Resource-SOAP-Lite-1.0101.tar.gz) Module = SOAP::Lite::Deserializer::XMLSchema1999 (MKUTTER/SOAP-Lite-0.71.04.tar.gz) Module = SOAP::Lite::Deserializer::XMLSchema2001 (MKUTTER/SOAP-Lite-0.71.04.tar.gz) Module = SOAP::Lite::Deserializer::XMLSchemaSOAP1_1 (MKUTTER/SOAP-Lite-0.71.04.tar.gz) Module = SOAP::Lite::Deserializer::XMLSchemaSOAP1_2 (MKUTTER/SOAP-Lite-0.71.04.tar.gz) Module SOAP::Lite::InstanceExporter (SMEISNER/SOAP-Lite-InstanceExporter-0.02.tar.gz) Module = SOAP::Lite::Packager (MKUTTER/SOAP-Lite-0.71.04.tar.gz) Module SOAP::Lite::Simple (LLAP/SOAP-Lite-Simple-1.9.tar.gz) Module SOAP::Lite::Simple::DotNet (LLAP/SOAP-Lite-Simple-1.4.tar.gz) Module SOAP::Lite::Simple::Real (LLAP/SOAP-Lite-Simple-1.4.tar.gz) Module SOAP::Lite::Utility (BRYCE/SOAP-Lite-Utility-0.01.tar.gz) Module = SOAP::Lite::Utils (MKUTTER/SOAP-Lite-0.71.04.tar.gz) 28 items found |
Aha! It's hiding under a Distribution. Tricky, tricky.
1 2 | cpan install MKUTTER/SOAP-Lite-0.71.04.tar.gz |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | CPAN.pm: Going to build M/MK/MKUTTER/SOAP-Lite-0.71.04.tar.gz
We are about to install SOAP::Lite and for your convenience will provide
you with list of modules and prerequisites, so you'll be able to choose
only modules you need for your configuration.
XMLRPC::Lite, UDDI::Lite, and XML::Parser::Lite are included by default.
Installed transports can be used for both SOAP::Lite and XMLRPC::Lite.
Press to see the detailed list.
Feature Prerequisites Install?
----------------------------- ---------------------------- --------
Core Package [*] Scalar::Util always
[*] Test::More
[*] URI
[*] MIME::Base64
[*] version
[*] XML::Parser (v2.23)
Client HTTP support [*] LWP::UserAgent always
Client HTTPS support [*] Crypt::SSLeay [ yes ]
Client SMTP/sendmail support [ ] MIME::Lite [ no ]
Client FTP support [*] IO::File [ yes ]
[*] Net::FTP
Standalone HTTP server [*] HTTP::Daemon [ yes ]
Apache/mod_perl server [ ] Apache [ no ]
FastCGI server [ ] FCGI [ no ]
POP3 server [*] MIME::Parser [ yes ]
[*] Net::POP3
IO server [*] IO::File [ yes ]
MQ transport support [ ] MQSeries [ no ]
JABBER transport support [ ] Net::Jabber [ no ]
MIME messages [*] MIME::Parser [ yes ]
DIME messages [*] IO::Scalar (v2.105) [ no ]
[ ] DIME::Tools (v0.03)
[ ] Data::UUID (v0.11)
SSL Support for TCP Transport [ ] IO::Socket::SSL [ no ]
Compression support for HTTP [*] Compress::Zlib [ yes ]
MIME interoperability w/ Axis [ ] MIME::Parser (v6.106) [ no ]
--- An asterix '[*]' indicates if the module is currently installed.
Do you want to proceed with this configuration? [yes]
Checking if your kit is complete...
Looks good
Writing Makefile for SOAP::Lite
cp lib/SOAP/Packager.pm blib/lib/SOAP/Packager.pm
cp lib/XML/Parser/Lite.pm blib/lib/XML/Parser/Lite.pm
...
Writing /usr/lib/perl5/site_perl/5.10.0/i686-linux/auto/SOAP/Lite/.packlist
Appending installation info to /usr/lib/perl5/5.10.0/i686-linux/perllocal.pod
MKUTTER/SOAP-Lite-0.71.04.tar.gz
/usr/bin/make install -- OK |
The latest version of SOAP::Lite is installed, time to pat yourself on the back and write some code to actually use it.

(2 rating, 2 votes)






beer planet is Artem Russakovskii's blog. Artem is a software engineer at