Perl Tricks and Tips ==================== Dumping a hash that's not a reference to a hash ----------------------------------------------- print Dumper(%ENV); # dumps as an array print Dumper(\%ENV); # make it a ref Definedness Vs. True/False -------------------------- $someKey is defined if it contains 0, "0", "" but evaluates false. The Peppler Favorite (Debugging Web Stuff) ------------------------------------------ eval { # pesky weird code here that's giving you trouble }; warn "$@
" if $@ This gives you warnings in the web server's log on pesky code that's just giving you a blank screen. Determine Current Function Name (Useful for Logging Error Messages) ------------------------------------------------------------------- $this_function = (caller(0))[3]; $caller[0] - $package $caller[1] - $filename $caller[2] - $line $caller[3] - $subr - name of frame's function including package $caller[4] - $has_args - whether has args $caller[5] - $want_array - whether function was called list,scalar, void context @caller = caller(0); current function caller(1); caller caller(2); caller's caller Globals ------- __LINE__ - current Line # __FILE__ - currenty file name __PACKAGE__ - currenty package name ------------------------------------------------------------------------- Running perl from the command line. % man perlrun Though the below is probably what you want to do. -i[extension] specifies that files processed by the "<>" construct are to be edited in-place. It does this by renaming the input file, opening the out- put file by the original name, and selecting that output file as the default for print() statements. The extension, if supplied, is used to modify the name of the old file to make a backup copy, following these rules: If no extension is supplied, no backup is made and the current file is overwritten. If the extension doesn't contain a "*", then it is appended to the end of the current filename as a suffix. If the extension does con- tain one or more "*" characters, then each "*" is replaced with the current filename. In Perl terms, you could think of this as: ($backup = $extension) =~ s/\*/$file_name/g; This allows you to add a prefix to the backup file, instead of (or in addition to) a suffix: $ perl -pi 'orig_*' -e 's/bar/baz/' fileA # backup to 'orig_fileA' Or even to place backup copies of the original files into another directory (provided the directory already exists): $ perl -pi 'old/*.orig' -e 's/bar/baz/' fileA # backup to 'old/fileA.orig' These sets of one-liners are equivalent: $ perl -pi -e 's/bar/baz/' fileA # overwrite current file $ perl -pi '*' -e 's/bar/baz/' fileA # overwrite current file $ perl -pi '.orig' -e 's/bar/baz/' fileA # backup to 'fileA.orig' $ perl -pi '*.orig' -e 's/bar/baz/' fileA # backup to 'fileA.orig' From the shell, saying $ perl -p -i.orig -e "s/foo/bar/; ... " is the same as using the program: ------------------------------------------------------------------------- De-referencing an anonymous array my $self = { 'passes' => [ 'element1, 'element2' ] } foreach my $element (@{$self->{passes}}) { } ------------------------------------------------------------------------- for (1..2) { ... } shortcut. for (my $i =1; $i <= 2; $i++) { .. } ------------------------------------------------------------------------- * Include Library Path: Include lib options in Perl. Can be in @INC, PERL5LIB (env variable), use lib, perl -I.