adding another key into perl hash reference is not working - perl-data-structures

Thanks in advance and your answer is most helpful.
I am fetching two arrayofhashref using $sth->fetchall_arrayref({}) from Database let's a and b where
a= [
{
'name' => 'test',
'id' => '10',`enter code here`
},
{
'name' => 'foo',
'id' => '22',
}
];
b= [
{
'dept' => 'IT',
'mob' => '880978'
},
{
'dept' => 'CSE',
'mob' => '877687'
},
];
The o/p should be :
a= [
{
'name' => 'test',
'id' => '10',
b= [
{
'dept' => 'IT',
'mob' => '880978'
},
{
'dept' => 'CSE',
'mob' => '877687'
},
];
},
{
'name' => 'foo',
'id' => '22',
b= [
{
'dept' => 'IT',
'mob' => '880978'
},
{
'dept' => 'CSE',
'mob' => '877687'
},
];
}
];
I did like :
my $count = 0;
foreach my $row (#$a){
$a->[$count]{b} = $b;
$count++;
}
If I pass $b as "heloo" it is working fine , but how can I pass this array of hash ref ?

I am not sure if I uderstood your question, but your code seems to be working.
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More tests => 1;
my $a = [
{
'name' => 'test',
'id' => '10',
},
{
'name' => 'foo',
'id' => '22',
}
];
my $b = [
{
'dept' => 'IT',
'mob' => '880978'
},
{
'dept' => 'CSE',
'mob' => '877687'
},
];
#The o/p should be :
my $wanted = [
{
'name' => 'test',
'id' => '10',
b => [
{
'dept' => 'IT',
'mob' => '880978'
},
{
'dept' => 'CSE',
'mob' => '877687'
},
],
},
{
'name' => 'foo',
'id' => '22',
b => [
{
'dept' => 'IT',
'mob' => '880978'
},
{
'dept' => 'CSE',
'mob' => '877687'
},
],
}
];
#I did like :
my $count = 0;
for my $row (#$a){
$a->[$count]{'b'} = $b;
$count++;
}
is_deeply($a, $wanted, 'data structures equal');
gives:
1..1
ok 1 - data structures equal

Related

How can I set up a proxy to get the requests from selenium using Browsermob::Proxy in perl?

I have been the whole day working on this and this and I got a little bit frustrated for not being able to make it work.
I started using a standalone selenium server as follows:
java -Dwebdriver.chrome.driver="C:\some_path\chromedriver.exe" -jar "C:\some_path\selenium-server-standalone-3.141.59.jar"
Then I downloaded the browsermob-proxy-2.1.4 and just executed the ./bin/browsermob-proxy.bat
Then I battled for some hours to find the properties in chrome that would not complain because I am using a proxy and showed the exception NET::ERR_CERT_AUTHORITY_INVALID:
So I ended up with this code:
use strict;
use warnings;
use Data::Dumper;
use Browsermob::Proxy;
use Browsermob::Server;
use Selenium::Remote::Driver;
use Selenium::Waiter qw/wait_until/;
my $server = Browsermob::Server->new(
server_port => 8080
);
my $proxy = $server->create_proxy;
my $driver = Selenium::Remote::Driver->new('browser_name' =>'chrome',
proxy => $proxy->selenium_proxy(1),
'extra_capabilities' => {
'goog:chromeOptions' => {
'args' => [
'ignore-ssl-errors=yes',
'proxy-auto-detect',
],
'prefs' => {
'profile' => {
'accept_ssl_certs' => 'true',
'accept_insecure_certs' => 'true',
},
'session' => {
'restore_on_startup' => 4,
'urls_to_restore_on_startup' => [
'http://www.google.com',
'http://docs.seleniumhq.org'
]},
'first_run_tabs' => [
'http://www.google.com',
'http://docs.seleniumhq.org'
]
}
}
});
$proxy->create_new_har(
payload => {
initialPageRef => 'payload is optional'
},
captureHeaders => 'true',
captureContent => 'true',
captureBinaryContent => 'true'
);
print("Getting stackoverflow\n");
wait_until{$driver->get("https://www.stackoverflow.com")};
validate_site($driver);
sleep(20);
$driver->quit;
sub validate_site{
my ($driver) = #_;
print "Har:\n";
print Dumper($proxy->har);
}
But now I just get the following output, which looks like if the proxy would not capture anything:
Getting stackoverflow
Har:
$VAR1 = {
'log' => {
'creator' => {
'version' => '2.1.4',
'name' => 'BrowserMob Proxy',
'comment' => ''
},
'entries' => [],
'version' => '1.2',
'comment' => '',
'pages' => [
{
'comment' => '',
'pageTimings' => {
'comment' => ''
},
'startedDateTime' => '2020-10-01T18:21:20.171+02:00',
'id' => 'Page 0',
'title' => 'Page 0'
}
]
}
};
What is funny, is that if I comment the line autodected proxy, I get the following:
Getting stackoverflow
Har:
$VAR1 = {
'log' => {
'pages' => [
{
'comment' => '',
'id' => 'Page 0',
'title' => 'Page 0',
'startedDateTime' => '2020-10-01T18:19:15.850+02:00',
'pageTimings' => {
'comment' => ''
}
}
],
'entries' => [
{
'serverIPAddress' => 'some_ip',
'startedDateTime' => '2020-10-01T18:19:15.900+02:00',
'response' => {
'comment' => '',
'status' => 0,
'headers' => [],
'content' => {
'comment' => '',
'size' => 0,
'mimeType' => ''
},
'bodySize' => -1,
'statusText' => '',
'cookies' => [],
'redirectURL' => '',
'httpVersion' => 'unknown',
'_error' => 'Unable to connect to host',
'headersSize' => -1
},
'pageref' => 'Page 0',
'timings' => {
'connect' => 257,
'wait' => 0,
'ssl' => -1,
'receive' => 0,
'send' => 0,
'blocked' => 0,
'comment' => '',
'dns' => 0
},
'time' => 258,
'request' => {
'comment' => '',
'queryString' => [],
'cookies' => [],
'headers' => [],
'headersSize' => 0,
'bodySize' => 0,
'url' => 'https://www.stackoverflow.com',
'method' => 'CONNECT',
'httpVersion' => 'HTTP/1.1'
},
'cache' => {},
'comment' => ''
},
{
'time' => 258,
'cache' => {},
'request' => {
'httpVersion' => 'HTTP/1.1',
'bodySize' => 0,
'headersSize' => 0,
'url' => 'https://www.stackoverflow.com',
'method' => 'CONNECT',
'headers' => [],
'cookies' => [],
'comment' => '',
'queryString' => []
},
'comment' => '',
'serverIPAddress' => 'some_ip',
'response' => {
'_error' => 'Unable to connect to host',
'headersSize' => -1,
'httpVersion' => 'unknown',
'cookies' => [],
'redirectURL' => '',
'content' => {
'size' => 0,
'comment' => '',
'mimeType' => ''
},
'bodySize' => -1,
'statusText' => '',
'headers' => [],
'status' => 0,
'comment' => ''
},
'startedDateTime' => '2020-10-01T18:19:15.900+02:00',
'pageref' => 'Page 0',
'timings' => {
'dns' => 0,
'blocked' => 0,
'comment' => '',
'receive' => 0,
'send' => 0,
'connect' => 257,
'wait' => 0,
'ssl' => -1
}
},
{
'cache' => {},
'request' => {
'cookies' => [],
'comment' => '',
'queryString' => [],
'httpVersion' => 'HTTP/1.1',
'bodySize' => 0,
'headersSize' => 344,
'url' => 'http://www.gstatic.com/generate_204',
'method' => 'GET',
'headers' => [
{
'name' => 'Host',
'value' => 'www.gstatic.com'
},
{
'name' => 'Proxy-Connection',
'value' => 'keep-alive'
},
{
'value' => 'no-cache',
'name' => 'Pragma'
},
{
'name' => 'Cache-Control',
'value' => 'no-cache'
},
{
'value' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36',
'name' => 'User-Agent'
},
{
'name' => 'Accept-Encoding',
'value' => 'gzip, deflate'
},
{
'value' => 'en-US,en;q=0.9',
'name' => 'Accept-Language'
}
]
},
'time' => 161,
'comment' => '',
'serverIPAddress' => 'some_ip',
'timings' => {
'connect' => 46,
'ssl' => -1,
'wait' => 21,
'receive' => 38,
'send' => 1,
'blocked' => 0,
'comment' => '',
'dns' => 53
},
'startedDateTime' => '2020-10-01T18:19:16.176+02:00',
'response' => {
'cookies' => [],
'redirectURL' => '',
'comment' => '',
'status' => 204,
'headers' => [
{
'name' => 'Content-Length',
'value' => '0'
},
{
'value' => 'Thu, 01 Oct 2020 16:19:16 GMT',
'name' => 'Date'
}
],
'httpVersion' => 'HTTP/1.1',
'content' => {
'comment' => '',
'size' => 0,
'encoding' => 'base64',
'text' => '',
'mimeType' => ''
},
'bodySize' => 0,
'statusText' => 'No Content',
'headersSize' => 85
},
'pageref' => 'Page 0'
}
],
'creator' => {
'name' => 'BrowserMob Proxy',
'comment' => '',
'version' => '2.1.4'
},
'version' => '1.2',
'comment' => ''
}
};
So although I get the exception in the browser and I can'T do anything with selenium it seems to work.
Anyone can give me a hint on what is going on?
Thank you!

How to add 'sub elements' in Zend Form

I have a Zend Form with several fields for the user to complete. In the admin system I want to display the exact same form but with additional Elements next to the existing ones where the administrator will be able to provide feedback to the user's input.
Below you can see the code I am using to create the form that the user sees.
Before posting this question I had a look on Zend Form Decorator but I did not understand if that's what I need to resolve this issue.
public function __construct()
{
parent::__construct('user-feedback-form');
$this->setAttribute('method', 'post');
$this->setAttribute('role', 'form');
$this->add([
'name' => 'name',
'type' => Text::class,
'attributes' => [
'id' => 'name',
'required' => true,
'readonly' => false,
],
]);
$this->add([
'name' => 'surname',
'type' => Text::class,
'attributes' => [
'id' => 'surname',
'required' => true,
'readonly' => false,
],
]);
$this->add([
'name' => 'age',
'type' => Number::class,
'attributes' => [
'id' => 'age',
'required' => true,
'readonly' => false,
],
]);
}
To reuse certain parts of a form zend provides the fieldsets.
Instead of adding the elements to your form add them toa filedset and add the fieldset to the form.
class UserFeedbackFieldset extends Zend\Form\Fieldset
{
public function init()
{
$this->add([
'name' => 'name',
'type' => Text::class,
'attributes' => [
'id' => 'name',
'required' => true,
'readonly' => false,
],
]);
$this->add([
'name' => 'surname',
'type' => Text::class,
'attributes' => [
'id' => 'surname',
'required' => true,
'readonly' => false,
],
]);
$this->add([
'name' => 'age',
'type' => Number::class,
'attributes' => [
'id' => 'age',
'required' => true,
'readonly' => false,
],
]);
}
}
Then in your forms add the fieldset:
class UserFeedbackForm extends Zend\Form\Form
{
public function __construct()
{
parent::__construct('user-feedback-form');
$this->setAttribute('method', 'post');
$this->setAttribute('role', 'form');
}
public function init()
{
$this->add([
'type' => UserFeedbackFieldset::class,
'name' => 'user',
'options' => [
'use_as_base_fieldset' => true,
],
]);
}
}
class AdminUserFeedbackForm extends Zend\Form\Form
{
public function __construct()
{
parent::__construct('user-feedback-form');
$this->setAttribute('method', 'post');
$this->setAttribute('role', 'form');
}
public function init()
{
$this->add([
'type' => UserFeedbackFieldset::class,
'name' => 'user',
'options' => [
'use_as_base_fieldset' => true,
],
]);
$this->add([
'name' => 'someotherfield',
'type' => Text::class,
'attributes' => [
'id' => 'someotherfield',
'required' => true,
'readonly' => false,
],
]);
}
}
Then you can use the other form on your admin page instead of the original one.

'$project' not working in my mongo aggregate query

This is my Documentation format
{
"_id" : ObjectId("5af56909b8be3925cf199924"),
"pid" : "9354496457",
"vid" : NumberLong("34756636617"),
"title" : "Ascolour Barnard Stripe Tank Tee-(5032)",
"handle" : "ascolour-barnard-stripe-tank-tee-5032",
"image" : "https://cdn.shopify.com/s/files/1/1919/3401/products/5032_barnard_stripe_tank_natural_black_5.jpg?v=1493879380",
"color" : "XL",
"size" : "NATURAL/BLACK",
"width" : null,
"activity" : "",
"brand" : "Ascolour",
"style" : "",
"feature" : "",
"type" : "Adult Singlets",
"price" : 1100,
"compare_at_price" : 1100,
"qty" : 1,
"sku" : "5032",
"visible" : 1
}
{
"_id" : ObjectId("5af56909b8be3925cf199925"),
"pid" : "9354496457",
"vid" : NumberLong("34756636681"),
"title" : "Ascolour Barnard Stripe Tank Tee-(5032)",
"handle" : "ascolour-barnard-stripe-tank-tee-5032",
"image" : "https://cdn.shopify.com/s/files/1/1919/3401/products/5032_barnard_stripe_tank_natural_black_5.jpg?v=1493879380",
"color" : "2XL",
"size" : "NATURAL/BLACK",
"width" : null,
"activity" : "",
"brand" : "Ascolour",
"style" : "",
"feature" : "",
"type" : "Adult Singlets",
"price" : 1100,
"compare_at_price" : 1100,
"qty" : 1,
"sku" : "5032",
"visible" : 1
}
here i search "type"=>"adult Singlets" with distinct "pid" and project all columns in the documents
so i write query:
$cursor = $songs->aggregate([
['$project' => ['_id' => 1, 'title' => 1, 'size' => 1]],
['$match' =>
['$and' => [
['$or' => [
['title' => $regex],
['size' => $regex],
['color' => $regex],
['brand' => $regex],
['activity' => $regex],
['style' => $regex],
['type' => $regex],
['feature' => $regex],
['width' => $regex],
]
],
['visible' => ['$eq' => 1]],
['qty' => ['$gt' => 0]],
]
]
],
['$group' => ["_id" => '$pid']],
['$limit' => 10]
]);
but its not show me anything and if i remove this line
['$project' => ['_id' => 1, 'title' => 1, 'size' => 1]]
then its show me all distinct pid. but i need to show all column w.r.t distinct pid
Thanks in advance :)
by this query i solved it
$cursor = $songs->aggregate([
['$match' =>
['$and' => [
['$or' => [
['title' => $regex],
['size' => $regex],
['color' => $regex],
['brand' => $regex],
['activity' => $regex],
['style' => $regex],
['type' => $regex],
['feature' => $regex],
['width' => $regex],
]
],
['visible' => ['$eq' => 1]],
['qty' => ['$gt' => 0]],
]
]
],
['$group' =>
['_id' => '$pid',
'originalId' => ['$first' => '$_id'],
'title' => ['$first' => '$title'],
'brand' => ['$first' => '$brand'],
'pid' => ['$first' => '$pid'],
'image' => ['$first' => '$image'],
'size' => ['$first' => '$size'],
'color' => ['$first' => '$color'],
'activity' => ['$first' => '$activity'],
'style' => ['$first' => '$style'],
'type' => ['$first' => '$type'],
'feature' => ['$first' => '$feature'],
'width' => ['$first' => '$width'],
'handle' => ['$first' => '$handle'],
'sku' => ['$first' => '$sku'],
'visible' => ['$first' => '$visible'],
'qty' => ['$first' => '$qty'],
'compare_at_price' => ['$first' => '$compare_at_price'],
]
],
['$project' => [
'_id' => '$pid',
'title' => 1,
'brand' => 1,
'pid' => 1,
'image' => 1,
'size' => 1,
'color' => 1,
'activity' => 1,
'style' => 1,
'type' => 1,
'feature' => 1,
'width' => 1,
'handle' => 1,
'image' => 1,
'pid' => '$_id',
'sku' => 1,
'visible' => 1,
'qty' => 1,
'compare_at_price' => 1,
]
],
['$limit' => 10]
]);

Not added to data to table

I'm trying to validate the form.
I do not understand why form validation does not take place. Logically, it should work with the simple presence of symbols, but in fact, a simple test:
if (! $form->isValid()) {
echo 'not valid in check';
return ['form' => $form];
}
returned " echo 'not valid in check' "
What is the problem?
Input filter are very simple:
public function getInputFilter()
{
if ($this->inputFilter) {
return $this->inputFilter;
}
$inputFilter = new InputFilter();
$inputFilter->add([
'name' => 'id',
'required' => true,
'filters' => [
['name' => ToInt::class],
],
]);
$inputFilter->add([
'name' => 'text',
'required' => true,
'filters' => [
['name' => StripTags::class],
['name' => StringTrim::class],
],
'validators' => [
[
'name' => StringLength::class,
'options' => [
'encoding' => 'UTF-8',
'min' => 1,
'max' => 1000,
],
],
],
]);
$inputFilter->add([
'name' => 'title',
'required' => true,
'filters' => [
['name' => StripTags::class],
['name' => StringTrim::class],
],
'validators' => [
[
'name' => StringLength::class,
'options' => [
'encoding' => 'UTF-8',
'min' => 1,
'max' => 100,
],
],
],
]);
$this->inputFilter = $inputFilter;
return $this->inputFilter;
}
Form? for added post:
class PostForm extends Form
{
public function __construct($name = null)
{
// We will ignore the name provided to the constructor
parent::__construct('post');
$this->add([
'name' => 'id',
'type' => 'hidden',
]);
$this->add([
'name' => 'title',
'type' => 'text',
'options' => [
'label' => 'Title',
],
]);
$this->add([
'name' => 'text',
'type' => 'textarea',
'options' => [
'label' => 'Text',
],
]);
$this->add([
'name' => 'submit',
'type' => 'submit',
'attributes' => [
'value' => 'Go',
'id' => 'submitbutton',
],
]);
}
}
Add action, if it need:
public function addAction()
{
$form = new PostForm();
$form->get('submit')->setValue('Add');
$request = $this->getRequest();
if (! $request->isPost()) {
return ['form' => $form];
}
// echo 'test';
$post = new Post();
$form->setInputFilter($post->getInputFilter());
$form->setData($request->getPost());
// echo 'before valid';
if (! $form->isValid()) {
echo 'not valid in check';
return ['form' => $form];
}
echo '\n';
echo 'valid';
$post->exchangeArray($form->getData());
$this->table->savePost($post);
return $this->redirect()->toRoute('post');
}
public function addAction()
{
---------------
if (!$form->isValid()) {
echo 'not valid in check';
return ['form' => $form];
}
---------
}
Validation:
public function getInputFilter()
{
if ($this->inputFilter) {
return $this->inputFilter;
}
$inputFilter = new InputFilter();
$inputFilter->add([
'name' => 'id',
'required' => true,
'filters' => [
['name' => ToInt::class],
],
]);
$inputFilter->add([
'name' => 'text',
'required' => true,
'filters' => [
['name' => StripTags::class],
['name' => StringTrim::class],
],
'validators' => [
[
'name' => StringLength::class,
'options' => [
'encoding' => 'UTF-8',
'min' => 1,
'max' => 1000,
],
],
],
]);
$inputFilter->add([
'name' => 'title',
'required' => true,
'filters' => [
['name' => StripTags::class],
['name' => StringTrim::class],
],
'validators' => [
[
'name' => StringLength::class,
'options' => [
'encoding' => 'UTF-8',
'min' => 1,
'max' => 100,
],
],
],
]);
$inputFilter->add([
'name' => 'disc',
'required' => true,
'filters' => [
['name' => StripTags::class],
['name' => StringTrim::class],
],
'validators' => [
[
'name' => StringLength::class,
'options' => [
'encoding' => 'UTF-8',
'min' => 1,
'max' => 100,
],
],
],
]);
$this->inputFilter = $inputFilter;
return $this->inputFilter;
}

Yii GroupGridView buttons

I use this extension(GroupGridView) in my yii project.
Now, I need help. How to add crud buttons for the grouped elements, to get something like this:
I don't really know is it right sollution.
I've created component GroupButtonColumn:
class GroupButtonColumn extends CButtonColumn {
public $name = 'buttons';
public $value; }
And my view
$this->widget('ext.groupGridView.GroupGridView', array(
'dataProvider' => $model->search(),
'summaryText' => false,
'filter' => $model,
'mergeColumns' => ['t_id', 'buttons'],
'columns' => [
[
'name' => 't_id',
'header' => '№',
'value' => 'CHtml::link($data->t_id, ["/task/view", "id"=>$data->t_id])',
'filter' => false,
'type' => 'raw'
],
/* ... */
[
'class' => 'GroupButtonColumn',
'template' => '{update}, {delete}',
'buttons' => [
'update' => [
'url' => '$this->grid->controller->createUrl("/task/update", array("id"=>$data->t_id))'
],
'delete' => [
'url' => '$this->grid->controller->createUrl("/task/delete", array("id"=>$data->t_id))'
]
]
],
],
));
Try this way:
'mergeColumns' => array('project_id', 'project_id')
/* ... */
'columns' => [
'project_id',
'id',
/* ... */
[
'name' => 'project_id',
'header' => 'Action',
'type'=>'raw',
'value' => function($data){
return 'Start';
}
],