Saturday, August 20, 2011

Initialize (clear) an array

Initialize (clear) an array.

Solution

my @array = ();
 
 
Solution
$#array is the subscript of the last element of the array (which is one less than the length of the array, since arrays start from zero). Assigning to $#array changes the length of the array @array, hence you can destroy (or clear) all values of the array between the last element and the newly assigned position. By assigning -1, everything is destroyed and the array is cleared. I recommend the above solution to this one.

$#array = -1;

Thursday, August 4, 2011

How to shuffle the values in a hash?

use List::Util qw(shuffle);

%hash = (this => 0,
is => 1,
a => 2,
test => 3);

my @values = shuffle(values %hash);
map { $hash{$_} = shift(@values) } (keys %hash);

print "$_\t$hash{$_}\n" foreach keys %hash;

Perl : Numeric Range Pattern Matching (IP)

$ip=~/(\d+)\.(\d+)\.(\d+)\.(\
d+)/;
if ($1>=0 && $1<=255 && $2>=0 && $2<=255 && $3>=0 && $3<=255 && $4>=0
&& $4<=255)
{
}

OR


$ip=~/\b((2[0-5][0-5]\.)|(1?[0-9]?[0-9]\.)){3}((2[0-5][0-5])|(1?[0-9]?[0-9]))\b/

perl - Objects, Modules and Packages

Objects, Modules and Packages

Package basics

  • A package is a collection of methods
  • Lives in its own namespace
  • Package methods can be exported or called directly
    • Foo::bar()
    • Foo->bar()
    • bar() (if Foo exports it)

Module basics

  • A module is a file containing one or more packages
  • Most people use "module" and "package" interchangably

Object basics

  • An object is a blessed reference to a hash
    • It doesn't have to be a hash reference, but it's most common.
  • Blessing assigns a single class to an object
  • An object can be re-blessed
  • You can muck with the internals, but that doesn't mean you should

1;

  • Modules must end with a true value
  • It doesn't have to be 1
  • Packages don't have the same restriction

@ISA

Perl's object inheritance methods use @ISA to determine what classes a module inherits from. Years ago, inheritance was declared by modifying @ISA directly; now, most programs use the base pragma to declare inheritance.

The following are mostly equivalent:

    package Foo;
require Wango;
@ISA
= ( "Wango" );

package Foo;
use base "Wango";


source: http://perl101.org/objects.html

Perl References

References

References are scalars that refer to other variables

References are like pointers in C in that they refer to other variables. Create a reference with the \ operator.

    my $sref = \$scalar;
my $aref = \@array;
my $href = \%hash;
my $cref = \&subroutine;

The thing the reference point to is the "referent".

Dereference a reference with the appropriate sigil, preferably in squiggly braces.

    my $other_scalar = ${$sref};
my @other_array = @{$aref};
my %other_hash = %{$href};
&{$cref} # Call the referent.

Arrow pointer is easier way to dereference.

To access array and hashrefs, use the -> operator.

    my $stooge = $aref->[1];
my $stooge = $href->{Curly};

ref vs isa

  • A reference belongs to one class
  • You can check this class with ref
  • An object reference can inherit from other classes
  • You can ask an object if it inherited from a class with isa
  • Don't use ref without a good reason
  • isa is part of the UNIVERSAL package, so you can call it on objects
        my $mech = WWW::Mechanize->new;
    print "ok\n" if $mech->isa('LWP::UserAgent');

References to anonymous subroutines

Subroutines can be assigned to a variable, then called, allowing code references to be passed around and used at will. This can come in handy if, for example, you're writing a subroutine that needs to execute supplied code as part of its work.

    my $casefix = sub { return ucfirst lc $_[0] };

my $color = $casefix->("rED");
print "Color: $color\n"; # prints Red



source: http://perl101.org/references.html