Perl

From Colettapedia
Jump to: navigation, search

General

  • fileno() - Returns the file descriptor for a filehandle, or undefined if the filehandle is not open.
  • shutdown(SOCKET, HOW) - Shuts down a socket connection in the manner indicated by HOW

Perlvar

  •  %SIG / $SIG{expr}
  • The hash %SIG contains signal handlers for signals. For example:
sub handler { # 1st argument is signal name
my($sig) = @_;
print "Caught a SIG$sig--shutting down\n";
close(LOG);
exit(0);
}

$SIG{'INT'} = \&handler;
$SIG{'QUIT'} = \&handler;
...
$SIG{'INT'} = 'DEFAULT'; # restore default action
$SIG{'QUIT'} = 'IGNORE'; # ignore SIGQUIT

Using Modules

  • perldoc perllocal = shows what modules have been installed via cpan or manually on the current system
  • get cpan if necessary and install (may also need Test::Simple and Digest::SHA to aid in downloading and checking of modules.
  • sudo cpan -i Foo::Bar (get name of Foo::Bar from cpan.org)
  • perldoc Foo::Bar = see user contributed perl documentation (man pages) of the module you just downloaded

Executables

  • cpan
    • ex: sudo cpan -i HTML::Tree
  • perldoc = Look up Perl documentation in Pod format.
    • -f perlfunc = look up specific builtin functions, ex: perldoc -f sprintf

Running Perl "perlrun"

  • perl -V = Summary of perl5 configuration.
  • perl -d and -D = debug options

Syntax

  • pass command line arguments using @ARGV, access using $ARGV[n], count with $#ARGV
  • indicate end of program using the "__END__" token
  • specify certain version of perl by statement "use 5.005_54;" or similar
  • if( -e $file_path ) - check to see if file exists
  • $#array doesn't give the length of array, it gives the last index, meaning number of indices, minus 1.

HTML::Tree module

  • my $tree = HTML::TreeBuilder->new_from_file($file_path_string);
    • FILE PATH STRING HAS TO BE FULL PATH, NOT USING ~/ NOTATION!!!
  • $tree->delete();
  • $tree->dump; # what we got in here?

Traversing

  • $e->tag = get element's tag name
  • $e->parent = get element's parent element
  • $e->attr('name') = get value of element's 'name' attribute
  • $h->descendents() vs $h->lineage()
  • $h->find( 'tag', ... ) = returns a list of elements at or under $h that have any of the specified tag names.
  • $h->look_down( ...criteria... ) = THE BIG ONE
    • looks down at the subtree starting at the given object ($h1), looking for elements that meet criteria you provide.
    • arg list is (key, value, key, value, ...)
    • if called in scalar (or $) context, gets the first element that matches and quits
    • if called in the vector context, returns all matching elements
my @wide_pix_images = $h->look_down( "_tag", "img", "alt", "pix!", sub { $_[0]->attr(’width’) > 350 });
    • If listing more than one key/value pair, there is an implicit AND. Have to use a sub if you need an "OR" operation.

Extracting Data

  • $h1->as_text = returns a string that contains all the text bits that are children (or otherwise descendants) of the given node.
  • $e->content_list = an array of all the elements, text and otherwise
  • $h1->as_HTML = return the current HTML element with all it descendants
  • Use index notation to pull out elements that have a certain numbered position
my $col3  = ( $row2−>look−down('_tag', 'td')   )[2];

Test::More

  • use Test::More;
  • BEGIN: { use_ok( Some::Module ); }
  • ok(expr, label_str );
  • is( val1, val2, string ); isnt(...);
  • like( val, qr/.../, explanation_str); unlike( ... );
  • plan tests => expression evaluation to number of tests;
  • skip block: SKIP: { skip( "explanation string", num_tests_in_block_to_skip ) if (expression); <some tests> }
  • todo block when you know test will fail: TODO: { <test code> }
  • cmp_ok( val1, operator_str, val2, test_name )
  • can_ok( module or object, list of methods) ... Checks to make sure the $module or $object can do these @methods (works with functions, too).
  • isa_ok() - Checks to see if the given "$object->isa($class)". Also checks to make sure the object was defined in the first place.
    • isa_ok($object, $class, $object_name);
    • isa_ok($subclass, $class, $object_name);
    • isa_ok($ref, $type, $ref_name);
  • subtest - an entirely encapsulated test in itself that looks like one test to the parent script.
    • HAVE TO SPECIFY "plan tests => 56" BEFORE OR "done_testing()" AFTER OR YOUR SUBTEST WILL FAIL INEXPLICABLY.
    • THE STRING SPECIFIED AS NAME OF THE SUBTEST MUST BE ENCLOSED IN DOUBLE NOT SINGLE QUOTES, OR YOUR TEST WILL FAIL AT COMPILE TIME.
use Test::More tests => 3;

pass("First test");

subtest ’An example subtest’ => sub {
  plan tests => 2;

  pass("This is a subtest");
  pass("So is this");
};

pass("Third test");
  • note( string ) - put this in the report, just like print, basically

Getopt::Long

  • In the option specification, the option name is followed by an equals sign "=" and the letter "s". The equals sign indicates that this option requires a value. The letter "s" indicates that this value is an arbitrary string. Other possible value types are "i" for integer values, and "f" for floating point values. Using a colon ":" instead of the equals sign indicates that the option value is optional. In this case, if no suitable value is supplied, string valued options get an empty string ’’ assigned, while numeric options are set to 0.
use Getopt::Long;
my $date_suffix = ""; 
my $base_test_dir = "../..";
GetOptions( "suffix=s"  => \$date_suffix, # =s implies the value is a string
            "basedir=s" => \$base_test_dir);

bless

use strict;
 
package Duck;
 
sub hatch {
        bless \(my $self), shift;
}
sub quack {
        print "Quaaaaaack!\n";
}
sub feathers {
        print "The duck has white and gray feathers.\n";
}
 
package Person;
 
sub accept_birth {
        bless \(my $self), shift;
}
sub quack {
        print "The person imitates a duck.\n";
}
sub feathers {
        print "The person takes a feather from the ground and shows it.\n";
}
 
package main;
 
sub in_the_forest
{
        my $duck = shift;
        $duck->quack();
        $duck->feathers();
}
 
my $duck = Duck->hatch();
my $person = Person->accept_birth();
 
in_the_forest( $duck );
in_the_forest( $person );
  • This function tells the thingy referenced by REF that it is now an object in the CLASSNAME package. If CLASSNAME is omitted, the current package is used. Because a bless is often the last thing in a constructor, it returns the reference for convenience. Always use the two-argument version if a derived class might inherit the function doing the blessing. See perltoot and perlobj for more about the blessing (and blessings) of objects.
  • Consider always blessing objects in CLASSNAMEs that are mixed case. Namespaces with all lowercase names are considered reserved for Perl pragmata. Builtin types have all uppercase names. To prevent confusion, you may wish to avoid such package names as well. Make sure that CLASSNAME is a true value.

One-liners