Is it possible to conditionally pass options to a method in perl? - perl

Similar to this question regarding Ruby, I'd like to conditionally pass parameters to a method. Currently, I have it configured as follows:
my $recs = $harvester->listAllRecords(
metadataPrefix => 'marc21',
metadataHandler => 'MARC::File::SAX',
set => 'pd',
from => $from,
until => $until,
);
What I'd like is to be able to conditionally pass the from and/or until parameters, depending on previous code. This is not syntactically correct, but something like this:
from => $from if ($from),
until => $until if ($until),
or this:
if ($from) {from => $from,}
if ($until) {until => $until,}
Is this possible, and if so, how would I go about doing it?

You could use the ternary ?: operator with list operands:
my $recs = $harvester->listAllRecords(
metadataPrefix => 'marc21',
metadataHandler => 'MARC::File::SAX',
set => 'pd',
$from ? (from => $from) : (),
$until ? (until => $until) : (),
);
It may also be worth knowing about the "conditional list include" pseudo-operator, which in this case would work like
...
(from => $from) x !!$from,
(until => $until) x !!defined($until),
...
but the ternary operator expression is probably easier to read for most people.

The other option is to build a list (or hash) of args and then call the method:
my %args = (
metadataPrefix => 'marc21',
metadataHandler => 'MARC::File::SAX',
set => 'pd',
);
$args{from} = $from if $from;
$args{until} = $until if $until;
my $recs = $harvester->listAllRecords(%args);

Related

Perl: How to get the value of an attribute of an object

In the below example, how would i print out the thread id?
$r_event = {
'type' => 'READ_' . $task . '_STARTED',
'timestamp' => $timestamp,
'threadid' => $threadId,
'fdn' => $fdn
};
You have a hash reference there (and those are often used as objects and that's what JSON calls an "object"). You can use the -> to dereference it and put the key you want in curlies:
print $r_event->{'threaded'};
This is just like a normal hash. Note the % and the parens instead of curlies:
%r_event = (
'type' => 'READ_' . $task . '_STARTED',
'timestamp' => $timestamp,
'threadid' => $threadId,
'fdn' => $fdn
);
In that case it's just $r_event{'threaded'} with no arrow since there's no reference.
My book Intermediate Perl covers this and you'll also find it in perlref.

Perl Popup_Menu Selection

I try to popup_menu select with if/else. But it isn't work with if/else.
The Select for Workflow or Tasktyp change with differ lists.
Workflow Group or Tasktyp for Type:
my %datatype = (
'workflow_group' => 'Workflow Group',
'tasktype_group' => 'TaskType',
);
Workflow Group or Tasktype for Value:
my %workflow_group = (
'one' => 'Contract',
'two' => 'Exchange',
'three' => 'Delivery',
'four' => 'Event',
);
my %tasktype_group = (
'one' => 'Contract',
'two' => 'Router',
'three' => 'DocSender',
'four' => 'Transformer',
);
Script with Variable (if/else):
print "<TR>";
print $lqcgi->td({-width=>'10%',},
$lqcgi->h3('Type'),
$lqcgi->popup_menu(-name=>'type',
-values=>[qw/workflow_group tasktype_group/],
-labels=>\%datatype,
-default=>'type_select'));
my $types = $lqcgi->param('Workflow Group');
print "<TD><h3>Value</h3>";
if ($types){
print $lqcgi->popup_menu(-name=>'value',
-values=>[qw/one two three four/],
-labels=>\%workflow_group,
-default=>'workflow'),
$lqcgi->submit(-type => 'tasktype_start_command', -value => 'Start', -onclick => 'javascript:()'),
$lqcgi->submit(-type => 'tasktype_stop_command', -value => 'Stop', -onclick => 'javascript:()');
}
else{
print $lqcgi->popup_menu(-name=>'value',
-values=>[qw/one two three four/],
-labels=>\%tasktype_group,
-default=>'tasktype'),
$lqcgi->submit(-type => 'tasktype_start_command', -value => 'Start', -onclick => 'javascript:()'),
$lqcgi->submit(-type => 'tasktype_stop_command', -value => 'Stop', -onclick => 'javascript:()');
}
print "</TD></TR>";
Notice: foreach my $type(#types), if (exists $types{$type}), if (defined $types{$type}isn't working! Thank for your help!
Here is find wrong:
Looks like your code is actually be working from a Perl/CGI perspective, but it may not be what you are expecting. I have tested your code with both $types = "Workflow Group" (any defined value would work) and $types = "".
If $types is equal to anything, the %workflow_group popup is shown. If $types is "" the %tasktype_group popup is shown. So the if/else conditions are working. And this leads me to conclude that your
my $types = $lqcgi->param('Workflow Group');
has no value. I am not sure whether you expect the $lqcgi->param('Workflow Group') value to be determined when the user chooses a value for the Type popup, and then automatically change the Value popup. If that is the case, you would likely need to do modify the Value popup after the Type choice is made using something like Javascript. Hope this helps.

phpseclib createKey() using own primes

Is it possible to generate private and public key in PKCS#1 format using phpseclib and my own primes? I mean I already have p and q and I want to generate both keys. I am trying to do something like this:
$p = new Math_BigInteger(...);
$q = new Math_BigInteger(...);
$custom_primes = serialize(array('primes'=>array(1=>$p,2=>$q)));
extract($rsa->createKey(512,10,$custom_primes));
But this gives me the fatal error:
Fatal error: Call to a member function divide() on a non-object in /volume1/web/phpseclib/Crypt/RSA.php on line 705
I checked this and it is trying to divide:
list($temp) = $lcm['top']->divide($lcm['bottom']);
Obviously I am not setting up lcm in my $custom_primes structure as I only have my two primes. So the question is: is it possible at all in phpseclib?
It looks like you're trying to use phpseclib's partial key functionality to achieve this. Problem is that expects more than just primes. See this, for example:
return array(
'privatekey' => '',
'publickey' => '',
'partialkey' => serialize(array(
'primes' => $primes,
'coefficients' => $coefficients,
'lcm' => $lcm,
'exponents' => $exponents
))
);
$lcm is defined, initially, like this:
$lcm = array(
'top' => $this->one->copy(),
'bottom' => false
);
So maybe try doing that as well. You can probably strip out all of the calculation functions
from phpseclib, do them yourself and then pass $partial into phpseclib and let it generate a key in whatever format you want it generated in.

How to store Hash of Hashes in Moose?

i was wondering, what is the best way to store Hash of Hashes in Moose. Lets take for example a Hash like this:
my %hash = ('step1' => {'extraction' => \$object1,
'analysis' => \$object2},
'step2' => {'extraction' => \$object3,
'analysis' => \$object4});
but i want to save this one in a moose attribute. How should i organize the access (reading, writing) on this. Examples on the net are mostly for "flat" hashes. But then you can use helpers like Moose::Meta::Attribute::Native::Trait::Hash. Is there something similar for hash of hashes?
Reason for this is, that i want to iterate over the step-keys and access the object-instances in that. Or is there a better, more Moose-like way to do this?
Thanks in advance!!!
You can store a hash of hashes in a Moose object in pretty much the same way as you would store any other hash:
has steps => ( is => 'ro', isa => 'HashRef' );
You can, however, be more specific to declare it as the specific kind of hash you need to store as a way to verify that anything stored in that slot is the right kind of thing:
has steps => ( is => 'ro', isa => 'HashRef[HashRef[Object]]' );
Depending on the data, I might also change Object here to the class name. You can get even fancier and use MooseX::Types and MooseX::Types::Structured to specify an even more exacting structure.
As for helpers to to step over your structure, I don't know of anything in Moose or MooseX to do that. If you know the structure of your data, it's probably best to just implement a subroutine to do what you need yourself. Your code will likely perform better and do what you need better than any generic traversal.
Edit/Additional Info: Each Moose attribute creates an accessor method no your class which returns the stored value, so accessing the data is:
# Assuming we put the attribute in a package named StepTool
my $step_tool = StepTool->new(
steps => { 'step1' => {'extraction' => \$object1,
'analysis' => \$object2},
'step2' => {'extraction' => \$object3,
'analysis' => \$object4} },
);
# To do something one of the values
do_something($step_tool->steps->{step1}{extraction});
# To iterate over the structure, could be done in a method on StepTool
for my $step_name (keys %{ $step_tool->steps }) {
my $step = $step_tool->steps->{ $step_name };
for my $action_name (keys %$step) {
my $object = $step->{ $action_name };
do_something($object);
}
}
# If doing the above as a method, $self is the Moose object, so...
sub traverse_steps {
my ($self) = #_;
for my $step_name (keys %{ $self->steps }) {
... # just like above
}
}
And one other note, you could still use traits => [ 'Hash' ] and add some handles to give yourself some additional helpers, if you want.
If the data structure is more free form than that, you might want to look into something like Data::Visitor to iterate over your structure in your subroutine. (I have had some difficult to debug, weird problems with Data::Visitor, so I try to avoid it when I can.)
There is also a type-safe approach inspired by Moose: How to get an array of objects? Traits?
There is a class to hold the outer hash (StepTool::Steps) that has traits => ['Hash']. This approach can be nested infinitely deep using e.g. Arrays and Hashes:
package StepTool;
use Moose;
has 'steps' => (
'is' => 'rw',
'isa' => 'StepTool::Steps',
'default' => sub { StepTool::Steps->new() },
);
package StepTool::Steps;
use Mouse;
has '_steps' => (
is => 'ro',
isa => 'HashRef[StepTool::Step]',
traits => ['Hash'],
default => sub { {} },
handles => {
# You'll probably want a fuller set here...
get => 'get',
set => 'set',
keys => 'keys',
}
);
package StepTool::Step;
use Mouse;
has 'extraction' => (
is => 'rw',
);
has 'analysis' => (
is => 'rw',
);
package main;
my $object1 = bless {}, 'Foobar1';
my $object2 = bless {}, 'Foobar2';
my $object3 = bless {}, 'Foobar3';
my $object4 = bless {}, 'Foobar4';
my $stepTool = StepTool->new();
# set up step1 one field at a time.
$stepTool->steps->set('step1', StepTool::Step->new());
# I have no idea why the OP wants references to objects
# everywhere but he does...
$stepTool->steps->get('step1')->extraction(\$object1);
$stepTool->steps->get('step1')->analysis(\$object2);
# set up step2 all at once
$stepTool->steps->set('step2', StepTool::Step->new(
extraction => \$object3,
analysis => \$object4
));
# or, less elegantly, initialize an entire StepTool:
my $stepTool2 = StepTool->new(
steps => StepTool::Steps->new(
_steps => {
step1 => StepTool::Step->new(
extraction => \$object1,
analysis => \$object2
),
step2 => StepTool::Step->new(
extraction => \$object3,
analysis => \$object4
),
}
),
);
printf "step1->analysis is a ref to an instance of class: %s\n",
ref(${$stepTool->steps->get('step1')->analysis});

How do I best make triggered accessors with defaults in Moose?

I have a situation where I'd like to cache some calculations for use
later. Let's say I have a list of allowed values. Since I'm going to
be checking to see if anything is in that list I'm going to want it as
a hash for efficiency and convenience. Otherwise I'd have to grep.
If I'm using Moose it would be nice if the cache was recalculated each
time the list of allowed values is changed. I can do that with a
trigger easy enough...
has allowed_values => (
is => 'rw',
isa => 'ArrayRef',
trigger => sub {
my %hash = map { $_ => 1 } #{$_[1]};
$_[0]->allowed_values_cache(\%hash);
}
);
has allowed_values_cache => (
is => 'rw',
isa => 'HashRef',
);
And the two will stay in sync...
$obj->allowed_values([qw(up down left right)]);
print keys %{ $obj->allowed_values_cache }; # up down left right
Now let's say I want a default for allowed_values, simple enough
change...
has allowed_values => (
is => 'rw',
isa => 'ArrayRef',
trigger => sub {
my %hash = map { $_ => 1 } #{$_[1]};
$_[0]->allowed_values_cache(\%hash);
},
default => sub {
return [qw(this that whatever)]
},
);
...except setting the default doesn't call the trigger. To get it to
DWIM I need to duplicate the caching.
has allowed_values => (
is => 'rw',
isa => 'ArrayRef',
trigger => sub {
$_[0]->cache_allowed_values($_[1]);
},
default => sub {
my $default = [qw(this that whatever)];
$_[0]->cache_allowed_values($default);
return $default;
},
);
sub cache_allowed_values {
my $self = shift;
my $values = shift;
my %hash = map { $_ => 1 } #$values;
$self->allowed_values_cache(\%hash);
return;
}
The Moose docs are explicit about trigger not getting called when
the default is set, but it gets in the way. I don't like the
duplication there.
Is there a better way to do it?
I was recently faced with this, and after asking on the #moose channel, was told to handle it this way:
Mark cache_allowed_values as a lazy_build, have _build_cache_allowed_values reference the current allowed_values, and put a write-trigger on allowed_values that clears cache_allowed_values.
That way, no matter what order the values are asked for or saved, they'll always be right with the least amount of work.
Example:
has cache_allowed_values => (is => 'ro', lazy_build => 1);
sub _build_cache_allowed_values {
return { map { $_ => 1 } #{shift->allowed_values} };
}
has allowed_values => (
is => 'rw',
trigger => sub { shift->clear_cache_allowed_values },
default => ...,
);
I think you really want allowed_values to be a separate data structure with the efficiency and ordering properties you desire. Since it doesn't look like you care about the ordering, why not:
has 'allowed_values' => (
traits => ['Hash'],
isa => HashRef[Bool],
default => sub { +{} },
handles => {
_add_allowed_value => 'set',
remove_allowed_value => 'delete',
value_is_allowed => 'exists',
allowed_values => 'keys',
},
);
method add_allowed_value(Str $value){
$self->_add_allowed_value( $value, 1 );
}
In general, anything not specific to the class being implemented should probably be implemented elsewhere. Making arrays have faster lookup times is not really the job of whatever class you are writing, so it should be implemented elsewhere, and this class should use that class. (In the simple case, like the hash above, maybe it's OK to ignore this rule. But if it were any more complicated, you would definitely want to factor it out.)
Edit:
If you want the user to think this is a list, how about:
use MooseX::Types::Moose qw(Bool ArrayRef HashRef);
use MooseX::Types -declare => ['ListHash'];
subtype ListHash, as HashRef[Bool];
coerce ListHash, from ArrayRef, via { +{ map { $_ => 1 } #$_ } };
has 'allowed_values' => (
# <same as above>
isa => ListHash,
writer => 'set_allowed_values',
coerce => 1,
);
Now you can set allowed_values like:
my $instance = Class->new( allowed_values => [qw/foo bar/] );
$instance->set_allowed_values([qw/foo bar baz/]);
And access them like:
my #allowed_values = $instance->allowed_values;
... if $instance->value_is_allowed('foo');
And modify them:
$instance->remove_allowed_value('foo');
$instance->add_allowed_value('gorch');
This hides any underlying implementation details from the user.
BTW, is building the hash actually and using it significantly faster than a linear scan over 3 elements?