I dumped out a Perl variable called $prefs and got this:
$VAR1 = bless( {
'USERID' => 1286,
'PREFS' => {
'1' => {
'VALUE' => 1,
'OTHERS_POST' => 1,
'CLIENTS_POST' => 1,
'ASSIGNED_TASKS' => 1
}
},
'dbh' => bless( {
'_sth' => bless( {}, 'DBI::st' ),
'_dbh' => bless( {}, 'DBI::db' )
}, 'Taskman::DB' )
}, 'USystems::UserPrefs' );
I'm pretty new to Perl, and I was wondering if someone can break down on whether or not it is possible to access specific data within this variable.
Like if I wanted to do an if statement such as
if (OTHERS_POST == 1) {
// code }
How would I get to the actual OTHERS_POST inside the $prefs variable
$prefs->{PREFS}->{1}->{OTHERS_POST};
Related
I know there are many questions already with this kind of subject, but as far as I know (perl beginner so I could be wrong), I'm not using an array so I don't understand where this output comes from
$VAR1 = {
'BridgeMode' => {
'Ten-GigabitEthernet1/0/5' => {
'Description' => 'poort1',
'Duplex' => 'F(a)',
'Interface' => 'Ten-GigabitEthernet1/0/5',
'Link' => 'UP',
'PVID' => '100',
'Speed' => '10G(a)',
'Type' => 'A',
'Vlan100' => {
'UntaggedPorts' => [
'Ten-GigabitEthernet1/0/5'
]
},
'vlanID' => [
'Vlan100'
]
},
Above is the content of my dumper and this is my print statement:
my $untaggedInterface = $data{BridgeMode}{"Ten-GigabitEthernet1/0/5"}{Vlan100}{UntaggedPorts} ;
print "Untagged: $untaggedInterface \n" ;
I would expect that the print statement would print "Ten-GigabitEthernet1/0/5" but instead it shows this:
Untagged: ARRAY(0x24a8ec0)
edit - it is possible that there exists an tagged and an untagged:
$VAR1 = {
'BridgeMode' => {
'Ten-GigabitEthernet1/0/12' => {
'Description' => 'poort5',
'Duplex' => 'F(a)',
'Interface' => 'Ten-GigabitEthernet1/0/12',
'Link' => 'UP',
'PVID' => '100',
'Speed' => '10G(a)',
'Type' => 'H',
'Vlan100' => {
'UntaggedPorts' => [
'Ten-GigabitEthernet1/0/12'
]
},
'Vlan107' => {
'TaggedPorts' => [
'Ten-GigabitEthernet1/0/12'
]
},
edit: printing the content of the array
my #untaggedInterface = $data{BridgeMode}{"Ten-GigabitEthernet1/0/5"}{Vlan100}{UntaggedPorts} ;
print join(", ", #untaggedInterface) ;
stil gives
ARRAY(0x1c03a68)
You would get the expected result if you had the following, i.e. a string instead of a string array:
'UntaggedPorts' => 'Ten-GigabitEthernet1/0/12'
Otherwise, you must specify the index of the array element:
my $untaggedInterface = $data{BridgeMode}{"Ten-GigabitEthernet1/0/5"}{Vlan100}{UntaggedPorts}[0];
I'v been trying to figure out if I can change an appender's filter at run-time that I've defined via a configuration file.
log4perl.filter.M1 = Log::Log4perl::Filter::LevelMatch
log4perl.filter.M2 = Log::Log4perl::Filter::LevelMatch
log4perl.filter.M1.LevelToMatch = INFO
log4perl.filter.M1.AcceptOnMatch = true
log4perl.filter.M2.LevelToMatch = WARN
log4perl.filter.M2.AcceptOnMatch = true
log4perl.filter.MyBoolean0 = Log::Log4perl::Filter::Boolean
log4perl.filter.MyBoolean0.logic = M1
log4perl.filter.MyBoolean1 = Log::Log4perl::Filter::Boolean
log4perl.filter.MyBoolean1.logic = M1 || M2
log4perl.appender.SCREEN.Filter = MyBoolean0
I'd like to change this filter from MyBoolean0 for the SCREEN to MyBoolean1, but do it after my program has started running.
Poking at the APPENDER_BY_NAME hash for SCREEN using Data::Dumper shows the following:
$VAR1 = bless( {
'appender' => bless( {
'Filter' => 'MyBoolean0',
'color' => {
...
...
'filter' => bless( {·
'params' => {·
'M3' => bless( {·
'LevelToMatch' => 'ERROR',
'name' => 'M3',
'AcceptOnMatch' => 1
}, 'Log::Log4perl::Filter::LevelMatch' ),
'M1' => bless( {·
'LevelToMatch' => 'INFO',
'name' => 'M1',
'AcceptOnMatch' => 1
}, 'Log::Log4perl::Filter::LevelMatch' ),
'M2' => bless( {·
'LevelToMatch' => 'WARN',
'name' => 'M2',
'AcceptOnMatch' => 1
}, 'Log::Log4perl::Filter::LevelMatch' )
},
'name' => 'MyBoolean0',
'eval_func' => sub { "DUMMY" },
'logic' => 'M1 || M2 || M3'
}, 'Log::Log4perl::Filter::Boolean' ),
'warp_message' => undef,
'name' => 'SCREEN'
}, 'Log::Log4perl::Appender' );
But mucking with this HASH seems hackish to me. Is there a better way to change an appender's filters?
You may use undocumented appender's property filter:
$Log::Log4perl::Logger::APPENDER_BY_NAME{'SCREEN'}->filter(
Log::Log4perl::Filter::by_name('MyBoolean1')
);
Also you may use two appenders:
log4perl.appender.SCREEN0.Filter = MyBoolean0
log4perl.appender.SCREEN1.Filter = MyBoolean1
And change it in runtime:
$logger->remove_appender('SCREEN0', 1);
$logger->add_appender(
Log::Log4perl::Config::create_appender_instance(
$Log::Log4perl::Config::OLD_CONFIG,
'SCREEN1',
\%Log::Log4perl::Logger::APPENDER_BY_NAME
)
);
I'm a non-programmer attemting to retrieve useful info from our InfoBlox DHCP boxes. I've installed the Perl API and can make some use of it.
I've got an output from the Data::Dumper "thingie" that appears to have some of the info I want. I'd like to directly reference some of that data but I'm unsure how.
print Dumper(\$object)
Here is part of the Data::Dumper output;
$VAR1 = \bless( {
'network' => '10.183.1.0/24',
'override_lease_scavenge_time' => 'false',
'enable_ifmap_publishing' => 'false',
'low_water_mark_reset' => '10',
'use_lease_time' => 0,
'use_enable_option81' => 0,
'network_container' => '/',
'override_ddns_ttl' => 'false',
'rir' => 'NONE',
'network_view' => bless( {
<snip> --------------------------------------
'extattrs' => {
'Use' => bless( {
'value' => 'Voip'
}, 'Infoblox::Grid::Extattr' )
},
<snip> --------------------------------------
'members' => [
bless( {
'ipv4addr' => '10.85.9.242',
'name' => 'ig3-app3.my.net'
}, 'Infoblox::DHCP::Member' ),
bless( {
'ipv4addr' => '10.85.9.210',
'name' => 'ig3-app1.my.net'
}, 'Infoblox::DHCP::Member' ),
bless( {
'ipv4addr' => '10.85.9.226',
'name' => 'ig3-app2.my.net'
}, 'Infoblox::DHCP::Member' )
],
'override_ignore_client_identifier' => 'false',
'email_list' => undef,
'rir_registration_status' => '??
}, 'Infoblox::DHCP::Network' );
How do I view the elements? ie ...
print $object{members->name};
print $object{members->ipv4addr};
print $object{extattrs->Use->value};
I've found the API dox insufficiant for my skill level:) The data I'd like to pull remains just out of reach.
my #retrieved_objs = $session->search (
object => "Infoblox::DHCP::Network",
network => '.*\.*\.*\..*',
);
foreach $object ( #retrieved_objs ) {
my $network = $object->network;
my $comment = $object->comment;
my $extattrs = $object->extattrs;
my $options = $object->options;
print $network, " network ", $comment, " ", $extattrs, " ", $options, "\n";
}
-------- output ---
10.183.2.0/24 network HASH(0x6a2f038) ARRAY(0x1d20eb0)
10.192.1.0/24 network HASH(0x9df6540) ARRAY(0x9df5468)
10.192.2.0/24 network HASH(0xa088fc8) ARRAY(0xa089718)
You shouldn't try to access the internal values of an object directly. The module - in this case Infoblox::DHCP::Network will provide methods that allow you to read or manipulate the values properly.
I'm working on some code to query F5 load balancers using the BigIP::iControl module.
Right now, I'm getting the following output when doing a Dumper on a variable I get back from a particular function.
I've having a lot of trouble iterating of this object.
How could I go about iterating over this and only taking out the monitor_status for each member?
$VAR1 = [
bless( [
bless( {
'monitor_status' => 'MONITOR_STATUS_UP',
'member' => bless( {
'address' => '127.0.0.0.1',
'port' => '8085'
}, 'Common::IPPortDefinition' )
}, 'LocalLB::PoolMember::MemberMonitorStatus' ),
bless( {
'monitor_status' => 'MONITOR_STATUS_UP',
'member' => bless( {
'address' => '127.0.0.0.1',
'port' => '8085'
}, 'Common::IPPortDefinition' )
}, 'LocalLB::PoolMember::MemberMonitorStatus' ),
bless( {
'monitor_status' => 'MONITOR_STATUS_DOWN',
'member' => bless( {
'address' => '127.0.0.0.1',
'port' => '8085'
}, 'Common::IPPortDefinition' )
}, 'LocalLB::PoolMember::MemberMonitorStatus' ),
bless( {
'monitor_status' => 'MONITOR_STATUS_DOWN',
'member' => bless( {
'address' => '127.0.0.0.1',
'port' => '8085'
}, 'Common::IPPortDefinition' )
}, 'LocalLB::PoolMember::MemberMonitorStatus' )
], 'LocalLB::PoolMember::MemberMonitorStatus[]' )
];
I'm not sure whether those member variables are public - I'm not familiar with the modules used - so this might well violate the encapsulation of the LocalLB::PoolMember::MemberMonitorStatus class. You should check before using.
for my $mms ( #{$VAR1->[0]} ) {
warn $mms->{monitor_status};
}
It would be better to check whether the MemberMonitorStatus class provides an accessor, and possibly an iterator for the member monitor status array.
The above was tested simply by pasting your Dumper output into a Perl script with the code of the for loop implemented based on eyeballing the data structure.
(edit: based on F5 webcentral docs in the Google cache, it may be that MemberMonitorStatus is a simple struct in the underlying code, exposed in the Perl as a class with two member variables - but no behaviour. If so, the above is probably OK.)
Hi everyone,
This is very simple for perl programmers but not beginners like me,
I have one xml file and I processed using XML::Simple like this
my $file="service.xml";
my $xml = new XML::Simple;
my $data = $xml->XMLin("$file", ForceArray => ['Service','SystemReaction',
'Customers', 'Suppliers','SW','HW'],);
Dumping out $data, it looks like this:
$data = {
'Service' => [{
'Suppliers' => [{
'SW' => [
{'Path' => '/work/service.xml', 'Service' => 'b7a'},
{'Path' => '/work/service1.xml', 'Service' => 'b7b'},
{'Path' => '/work/service2.xml', 'Service' => 'b5'}]}
],
'Id' => 'SKRM',
'Customers' =>
[{'SW' => [{'Path' => '/work/service.xml', 'Service' => 'ASOC'}]}],
'Des' => 'Control the current through the pipe',
'Name' => ' Control unit'
},
{
'Suppliers' => [{
'HW' => [{
'Type' => 'W',
'Path' => '/work/hardware.xml',
'Nr' => '18',
'Service' => '1'
},
{
'Type' => 'B',
'Path' => '/work/hardware.xml',
'Nr' => '7',
'Service' => '1'
},
{
'Type' => 'k',
'Path' => '/work/hardware.xml',
'Nr' => '1',
'Service' => '1'
}]}
],
'Id' => 'ADTM',
'Customers' =>
[{'SW' => [{'Path' => '/work/service.xml', 'Service' => 'SDCR'}]}],
'Des' => 'It delivers actual motor speed',
'Name' => ' Motor Drivers and Diognostics'
},
# etc.
],
'Systemreaction' => [
# etc.
],
};
How to access each elements in the service and systemReaction(not provided). because I am using "$data" in further processing. So I need to access each Id,customers, suppliers values in each service. How to get particular value from service to do some process with that value.for example I need to get all Id values form service and create nodes for each id values.
To get Type and Nr value I tried like this
foreach my $service (#{ $data->{Service}[1]{Suppliers}[0]{HW}[0] }) {
say $service->{Nr};
}
foreach my $service (#{ $data->{Service}[1]{Suppliers}[0]{HW}[0] }) {
say $service->{Type};
}
can you help me how to get all Nr and Type values from Supplier->HW.
I suggest reading perldocs Reference Tutorial and References and Nested Data Structures. They contain an introduction and full explanation of how to access data like that.
But, for example, you can access the service ID by doing:
say $data->{Service}[0]{Id} # prints SKRM
You could go through all the services, printing their ID, with a loop:
foreach my $service (#{ $data->{Service} }) {
say $service->{Id};
}
In response to your edit
$data->{Service}[1]{Suppliers}[0]{HW}[0] is an hash reference (you can check this quickly by either using Data::Dumper or Data::Dump on it, or just the ref function). In particular, it is { Nr => 18, Path => "/work/hardware.xml", Service => 1, Type => "W" }
In other words, you've almost got it—you just went one level too deep. It should be:
foreach my $service (#{ $data->{Service}[1]{Suppliers}[0]{HW} }) {
say $service->{Nr};
}
Note the lack of the final [0] that you had.