Posts

In Perl keys get defined if they are accessed as hash, without checking for their existence

In the following code snippet, 'c' is not as key in 'foo' but is set to {} when the last check for foo->c->0 is called. / tmp ( ) $ cat foo . pl use strict ; use warnings ; use Data : : Dumper ; sub abcde { my $foo = { 'a' = > 1 , 'b' = > 2 } ; print Dumper ( $foo ) ; if ( exists $foo - > { c } & & defined $foo - > { c } - > { 0 } ) { print "C is defined" ; } print Dumper ( $foo ) ; if ( defined $foo - > { c } and defined $foo - > { c } - > { 0 } ) { print "C is defined" ; } print Dumper ( $foo ) ; if ( defined $foo - > { c } - > { 0 } ) { print "C is defined" ; } print Dumper ( $foo ) ; } abcde ( ) ; / tmp ( ) $ perl foo . pl $VAR1 = { 'a' = > 1 , 'b' = > 2 } ; $VAR1 = { 'a' = > 1 , ...

eventlet.GreenPool.waitall() just logs the exception but doesn't raise them

waitall() method in eventlet.GreenPool doesn't raise exceptions but just logs them, whereas in case of wait on the greenthread returned by spawn from evenlet.GreenPool raises the exception. # cat foo . py import eventlet pool = eventlet . GreenPool ( 1 ) def foo ( ) : raise ValueError gt = pool . spawn ( foo ) try: pool . waitall ( ) except Exception , e : print " pool.waitall Exception raised: %r " % e try: gt . wait ( ) except Exception , e : print " gt.wait() Exception raised: %r " % e # python foo . py Traceback ( most recent call last ) : File " /usr/lib/python2.7/site-packages/eventlet/hubs/hub.py " , line 346 , in fire_timers timer ( ) File " /usr/lib/python2.7/site-packages/eventlet/hubs/timer.py " , line 56 , in __call__ cb ( * args , * * kw ) File " /usr/lib/python2.7/site-packages/eventlet/greenthread.py " , line 194 , in main result = functi...

Using repr instead of str for parameters (not objects) for disambiguity

>>> for e in ( 0 , "0" , None , "None" , 1.23 , "1.23" , [ 1 , 2 ], { "foo" : "bar" }): ... print " %r " % e ... 0 '0' None 'None' 1.23 '1.23' [ 1 , 2 ] { 'foo' : 'bar' } When %s is used to interpolate to string there could be a bit of ambiguity introduced. >>> for e in ( 0 , "0" , None , "None" , 1.23 , "1.23" , [ 1 , 2 ], { "foo" : "bar" }): ... print " %s " % e ... 0 0 None None 1.23 1.23 [ 1 , 2 ] { 'foo' : 'bar' }

Redirect both stdout and stderr to dev null using > /dev/null 2>&1 for linux commands and scripts

/ tmp ( ) $ cat out_err_redirect_test . sh echo message to stdout echo message to stderr > & 2 / tmp ( ) $ bash out_err_redirect_test . sh message to stdout message to stderr # Message to stdout is redirected to dev null / tmp ( ) $ bash out_err_redirect_test . sh > / dev / null message to stderr # Message to stderr is redirected to dev null / tmp ( ) $ bash out_err_redirect_test . sh 2 > / dev / null message to stdout # Message to stderr and stdout is redirected to dev null / tmp ( ) $ bash out_err_redirect_test . sh > / dev / null 2 > / dev / null # Messaged to stdout is redirected to dev null , and stderr to configured stdout ( implies dev null ) / tmp ( ) $ bash out_err_redirect_test . sh > / dev / null 2 > & 1 # Message to stderr is redirected to stdout ( screen ) and stdout to dev null / tmp ( ) $ bash out_err_redirect_test . sh 2 > & 1 > / dev / null message to stderr

Tuples are expanded as multiple arguments when passed to print statement after %

Below the tuple (1, 2, 3) is getting expanded as 3 arguments and the format representation is expecting to have 3 %s or %r to be printed, > > > print " %r " % ( 1 , 2 , 3 ) Traceback ( most recent call last ) : File " <stdin> " , line 1 , in < module > TypeError: not all arguments converted during string formatting > > > print " %s " % ( 1 , 2 , 3 ) Traceback ( most recent call last ) : File " <stdin> " , line 1 , in < module > TypeError: not all arguments converted during string formatting > > > print " %s " % str ( ( 1 , 2 , 3 ) ) ( 1 , 2 , 3 )

Loop over an array with index and element in the array

Perl looping of an array with index as well as the item stored in variables, /tmp$ cat loop_with_index.pl  my @arr=(1,2,3,4); while (my ($i, $x) = each(@arr)) {     print "$i: $x\n"; } /tmp$ perl loop_with_index.pl  0: 1 1: 2 2: 3 3: 4

Perl constant hashrefs are actually functions

To get the keys of a constant hashref you first need to learn that a constant is actually a function and hence use () to get the keys from it INPUT: $ cat perlConstant.pl use Data::Dumper; use constant FOOHASH => { "foo" => "bar", }; print FOOHASH->{"foo"}."\n"; foreach $key (keys %{FOOHASH()}) { FOOHASH->{$key} = "bar1"; } print FOOHASH->{"foo"}."\n"; foreach $key (keys %{FOOHASH}) { FOOHASH->{$key} = "bar2"; } print FOOHASH->{"foo"}."\n"; OUTPUT: $ perl perlConstant.pl bar bar1 bar1