Difference between revisions of "Perl"

From Colettapedia
Jump to navigation Jump to search
Line 79: Line 79:
 
==Getopt::Long==
 
==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.
 
* 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.
 +
<pre>
 +
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);
 +
</pre>

Revision as of 21:47, 8 March 2010

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
  • 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);