Perl hacks

From test.coolscript.org
Jump to navigation Jump to search


LWP[edit]

File download[edit]

use LWP::Simple;
getstore("http://ftp.hitchhiker.com/data/currency.zip", "c:\\curbla.zip");


Manip[edit]

DateCalc[edit]

use Date::Manip;
$bla=&UnixDate(DateCalc("2013-05-31","+ 1 days",\$err),"%Y-%m-%d")
my $pageDate = "201402190000";
$pageDate = UnixDate("$pageDate","%A %d %B %Y %H:%M");
print "$pageDate";

Array[edit]

Return array from function[edit]

#!/usr/bin/perl
use strict;
use Data::Dumper;
my @ret=&retTest;
print "=>$ret[0]\n";
print "=>$ret[1]\n";
sub retTest {
 my @subRet;
 @subRet=("a","b");
 return @subRet;
}

Hash[edit]

Double nested[edit]

Double nested sample,

for( @{$_->{FaresResponsePassenger}} ){

Experts Exchange[edit]

I got the following hash by a soap response, example:

$VAR1 = {

   'Response' => {
     'Category' => [
                   {
                    'ID' => '12345',
                    'Articel' => {
                      'Segment' => [
                                   {
                                          'Number' => '1',
                                   },
                                   {
                                          'Number' => '2',
                                   }
                                    ]                                                                                                                    
                                 }                                                                                      
                    },
                    {
                     'ID' => '6789',
                     'Articel' => {
                       'Segment' => [
                                    {
                                          'Number' => '1',
                                    },
                                    {
                                         'Number' => '2',
                                    }
                                   ]
                                  }                                                                                      
                    }
                    ]
     },
   };

my $n=0; $n += grep$_->{Number},@{$_->{Articel}{Segment}} for @{$VAR1->{Response}{Category}};

ExpertsExchnage1=[edit]

This is a follow up question from ID 25120698. I got the following hash by a soap response, example:

$VAR1 = {

   'Response' => {
     'Category' => [
                   {
                    'ID' => '123',
                    'Articel' => {
                      'Segment' => [
                                   {
                                          'Number' => '1',
                                   },
                                   {
                                          'Number' => '2',
                                   }
                                    ]                                                                                                                    
                                 }                                                                                      
                    },
                    {
                     'ID' => '456',
                     'Articel' => {
                       'Segment' => [
                                    {
                                          'Number' => '3',
                                    },
                                    {
                                          'Number' => '4',
                                    }
                                   ]
                                  }                                                                                      
                    },
                    {
                     'ID' => '789',
                     'Articel' => {
                       'Segment' => {
                                          'Number' => '5',
                                    }
                                  }                                                                                      
                    }


                    ]
     },
   };



you can check the ref is ARRAY or HASH and depend on which you can print it.

for my $v (@{$VAR1->{Response}{Category}}){

    if(ref($v->{Articel}{Segment}) eq "ARRAY"){ ### Check it is ARRAy or HASH
      for my $w (@{$v->{Articel}{Segment}}){
       print "$w->{Number}\n";
     }

} else { for my $w (keys (%{$v->{Articel}{Segment}})){ ###Get the element as HASH ref and print print "$v->{Articel}{Segment}{$w}\n"; } } }


EE3[edit]

Hi i'm looking for a regex to split the filename from a path, eg $_ = '/path/to/my//file/space /my music - name.mp3';

how do i get the filename (my music - name.mp3) and the path (/path/to/my//file/space /) into a variable, i'm trying to do something like $MP3= '/path/to/my//file/space /my music - name.mp3'; ($var,$value) = split(/\/([^.]*)/ig,$MP3,2);

i'm lost, any idea ?

thx


$_ = '/path/to/my//file/space /my music - name.mp3'; my ($var,$value) = /^(.*\/)(.*)$/;


FILE STAT[edit]

Get the mod date of a file

use File::stat qw(:FIELDS); 
my $stat = stat($_) or die "No File :$!";  #PS: So behandelt man Filstats!
$tmpsize = $st_size;
$tmpmod = $st_mtime;

Sort[edit]

Sort scalar

@splunkfiles = sort {$b cmp $a} @splunkfiles;


Close DB Handle[edit]

$sql->finish;  #Close DB Handle

Socket receive[edit]

  #Thanks!
  #http://stackoverflow.com/questions/14444539/perl-client-socket-recv-vs-when-server-socket-is-invoking-send-multiple-ti	
  my $buf;
  do {
   defined $socket->recv($buf, 8129) or die "recv: $!";
   #print qq(received: "$buf"\n);
   $RecData.=$buf;
 } while ($buf !~ /~~/);


Console autoflush[edit]

#Use autoflush with STDOUT:
local $| = 1; # Or use IO::Handle; STDOUT->autoflush;