CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner: Re: [hwnd] Delete unwanted hash keys: Edit Log



hwnd
User

Jul 18, 2013, 9:28 PM


Views: 1346
Re: [hwnd] Delete unwanted hash keys

BillKSmith, going off your code a bit. What If I did something like so?


Code
use strict;  
use warnings;

sample_class->sample_method(
file => 'file.txt',
type => 'AOH',
head => 1,
);

sample_class->sample_method( file => 'file.txt' );

sample_class->sample_method(
file => '',
type => 'AOH',
head => 1,
);

sample_class->sample_method(
file => 'file.txt',
type => '',
head => 1,
);


my %rules = ( file => 'file.txt', type => 'AOH', head => 1 );

sample_class->sample_method( \%rules );


package sample_class;
use Data::Dumper;

sub sample_method {
my $self = shift;
my ($tmp, $ref);

@_ = ( @_ == 1 and
ref $_[0] eq 'HASH' ) ? $tmp = $_[0] :
!(@_ % 2) ? $tmp = +{@_} : undef;

@{$ref}{qw(file type head)} = (
$tmp->{file},
defined $tmp->{type} ? $tmp->{type} : 'AOH',
defined $tmp->{head} ? $tmp->{head} : 1,
);
undef $tmp;

croak("Illegal arg! Argument 'file' is required")
if !defined $ref->{file} or !$ref->{file};

croak("Illegal arg! Argument 'type' needs a structure type!")
if !$ref->{type};

print Dumper $ref;
}

__OUTPUT__
$VAR1 = {
'head' => 1,
'file' => 'file.txt',
'type' => 'AOH'
};

$VAR1 = {
'head' => 1,
'file' => 'file.txt',
'type' => 'AOH'
};

Illegal arg! Argument 'file' is required at test.pl line 14

Illegal arg! Argument 'type' needs a structure type! at test.pl line 14

$VAR1 = {
'head' => 1,
'file' => 'file.txt',
'type' => 'AOH'
};



(This post was edited by hwnd on Jul 18, 2013, 9:39 PM)


Edit Log:
Post edited by hwnd (User) on Jul 18, 2013, 9:39 PM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives