Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

The Monastery Gates

( #131=superdoc: print w/ replies, xml ) Need Help??

Donations gladly accepted

If you're new here please read PerlMonks FAQ
and Create a new user.

New Questions
[Moose] extends(...) throws "Subroutine redefined" warnings
1 direct reply — Read more / Contribute
by muba
on Oct 07, 2015 at 08:16

    I have a superclass (MyClass), which creates instances of a subclass on demand. MyClass implements a custom method "get_subclass_instance" for this. The subclass (SubClass) extends 'MyClass';, however, this generates a "Subroutine get_subclass_instance redefined" warning.

    Minimal example:

    # package MyClass; use SubClass; use Moose; sub get_subclass_object { return SubClass->new; } 1;
    # package SubClass; use Moose; extends 'MyClass'; 1;
    $ perl -MMyClass -e 0 Subroutine get_subclass_object redefined at line 5. $

    I understand that extends(...) tries to load the class(es) passed to it. Consequently, if I remove the Extends 'MyClass' line from, the warning disappears.
    But I don't want to remove that line, because SubClass does extend MyClass!

    Of course, I could simply ignore the warning, but that doesn't feel clean to me. So what am I doing wrong, and how can I have MyClass load and instantiate SubClass, while SubClass explicitly extends MyClass?

Dynamically create the hash reference
2 direct replies — Read more / Contribute
by k_manimuthu
on Oct 07, 2015 at 06:16

    Hi All,

    I am trying to create dynamic references in a recursion function. Below I place code which I try, and it return '{}' only. How to get the initial level reference in else part?

    use Data::Dumper; sub insert { my ($ref, $head, @tail) = @_; return unless ( @tail ); if ( @tail and $head ne 'break') { insert( \%{$ref->{$head}}, @tail +) } else { # Here $ref gives '{}' value only. # How to get values of previous head values (ie. f1,f2,f3) print "\n==>", Dumper $ref; # More commands . . . #insert( \%{$ref->{f1}}, @tail ) } } my %hash; while (<DATA>) { chomp and insert \%hash, split( '/', $_ ) ; } print "\nDump\n", Dumper %hash; __DATA__ f1/f2/f3/f4/break/f1/f2/f5/break/f1/f2/f6
XS: exposing C++ library constant as package variable
1 direct reply — Read more / Contribute
by wisnij
on Oct 06, 2015 at 17:38

    I'm working on a module which is a Perl interface to an existing C++ library at my work. In particular, there's a constant in the C++ header that I want to expose in Perl, rather than duplicating the definition and risking them drift out of sync if someone forgets to update the code in one place or the other. My XS experience to date is limited, but after looking through perlguts, perlapi and some Googling, here's what I've come up with so far:

    package MyModule; use vars qw(@ISA $VERSION $CONSTANT_NAME); BEGIN { @ISA = qw(DynaLoader); $VERSION = '0.01'; $CONSTANT_NAME = -1; } bootstrap MyModule $VERSION;


    #include "my_constants.h" MODULE = MyModule PACKAGE = MyModule BOOT: { SV* const_sv = get_sv( "MyModule::CONSTANT_NAME", 0 ); SvIV_set( const_sv, CONSTANT_VALUE_FROM_C_LIBRARY ); SvIOK_on( const_sv ); SvREADONLY_on( const_sv ); }

    This works in the sense that it compiles and provides the expected value in my simple test one-liner (basically just "use MyModule; print $MyModule::CONSTANT_NAME"). However, I'm not sure whether this is actually the best way to go about it -- in particular, setting $CONSTANT_NAME in the BEGIN block and then changing it again in the XS seems clunky. It's also possible that this is subtly broken in some way that I haven't discerned yet (e.g. threads or something).

    My question, then, is (a) whether this as written is incorrect in any specific way, and (b) whether there's a better approach that I ought to be using instead. What say ye, monks?

Data::Dumper / write output into Mysql
2 direct replies — Read more / Contribute
by steph007
on Oct 06, 2015 at 14:48
    Hi There, I hope everyone is doing fine. First off , my perl is really really rusty so please don't laugh :)

    Anyway , I've been tinkering on a tool to grab mac addresses with SNMP from Wireless CPE equipment on our network which I then need to write into a Mysql database. Now grabbing the data and writing it back is no issue ... well , that's if I can past understanding on how to read the information Data dumper is returning.

    The original code came from a script called which I downloaded from Cpan and then modified it to my needs. Right now it's just passing IP addresses from an array , does an snmpwalk based on a specific IOD and then passes the #hash to Datadumper for processing. Well , that's what I think happens at least :) Now this is where I get stuck , I have no idea how to properly read the processed information so I can write it back line for line into a Mysql DB.

    Here's the code.


    #!/usr/bin/perl use Getopt::Std; use DBI; use Net::SNMP::Util qw(:para); use Net::SNMP::Util::OID qw(*); my @SMhosts = ('', '', '', '' +); # ---------- Data::Dumper setting ---------- # put output into hands of Data::Dumper :-) use Data::Dumper; use Storable qw(lock_nstore); foreach $unitIP (@SMhosts) { local($a,$b); $Data::Dumper::Indent = 1; $Data::Dumper::Terse = 1; $Data::Dumper::Purity = 1; $Data::Dumper::Sortkeys = sub { [ sort { # if keys are numbers, then do numerical sort return ($a =~ /^\d+$/)? ($a <=> $b): ($a cmp $b); } keys %{$_[0]} ] }; my %snmp = (); $snmp{"-version"} = "2c"; $snmp{"-community"} = "g_wireless"; $snmp{"-timeout"} = "2"; # ---------- kick function ---------- my $func = $opt{P}? 'snmpparawalk': 'snmpwalk'; my ( $result, $error ); { no strict; ( $result, $error ) = &$func( hosts => "$unitIP", oids => '', snmp => \%snmp ); die "[ERROR] $error\n" unless defined $result; } # ---------- output result ---------- print Dumper($result); }



    So basically the first value is the IP address of the CPE equipment followed by the Mac addresses contained in the units bridge table. This value varies on the amount of network devices connected to the LAN port of the CPE equipment. Normally , this will only be three if the customer plugged the CPE equipment directly into their routers but more values van appear if the equipment is plugged into a switch for instance. So basically what I want to do is to write each value as a separate row into Mysql but how do I extract each Mac address from the hash ?
    [root@cms3 scripts]# ./ { '' => { #IP Address from CPE equipment. '1' => '0x0a003edd8706', #Bridge table entry #1 '2' => '0x1a003edd8706', #Bridge table entry #2 '3' => '0xc03f0eddfb3b' #Bridge table entry #3 } } { '' => { '1' => '0x0a003ea2643e', '2' => '0x1a003ea2643e', '3' => '0x000c428b8e11' } } { '' => { '1' => '0x0a003e67a706', '2' => '0x1a003e67a706', '3' => '0x200cc8136ef9' } } { '' => { '1' => '0x0a003eb1b2fc', '2' => '0x1a003eb1b2fc', '3' => '0xd850e6bd6330', '4' => '0x023d9d690d52', '5' => '0xec2280985442', '6' => '0x406186c1ef32', '7' => '0x0024219b0d6b', '8' => '0x00237d7cd808', '9' => '0x00272201b896', '10' => '0xf8e903e44b30', '11' => '0x52f5febdca85', '12' => '0xbe95360a4d5c', '13' => '0xac9e17b6007c', '14' => '0x000b7483457d', '15' => '0xaa069206530b', '16' => '0x000c2955956e', '17' => '0x54bef72d8cc2', '18' => '0x0000859f7d3f', '19' => '0x406186c2e658', '20' => '0x02759adcc739' } }
Activestate Perl 5.20, Change make/compiler
2 direct replies — Read more / Contribute
on Oct 06, 2015 at 09:32
    I have installed Activestate Perl 5.20 on a Windows 2012 Server. The default settings for make/Compiler are 'dmake' and 'mingw'. So i installed Visual Studio Express and the MS SDK. I put the dirs in the PATH, executed 'vcvars*', but when trying to install a certain module, i always get the error that the MS Compiler is needed but only 'mingw' is available. I need to install Win32::GUI::Grid and it only Compiles with MSVC... I can install Win32::GUI without any error show, but in fact win32::GUI::Grid and at least win32::GUI::axWindow are mnot installed. I also cannot switch to linux or another Perl Distribution, since I have to 'compile' with PerlApp. An suggestions where or how to change the settings?
Text filer and assign
2 direct replies — Read more / Contribute
by intoperl
on Oct 06, 2015 at 04:13

    Hello Monks, I want to filer some text from a linux output, and store it in variable for report purposes. Below is the text i am processing

    PV /dev/sda2 VG VGExaDb lvm2 [557.62 GB / 20.50 GB free] PV /dev/sda3 VG VGExaDb lvm2 [278.88 GB / 0 free]

    So, from above text, i want to extract (/dev/sda2) (/dev/sda3) and 557.62 GB / 20.50 GB free. I did write a regex code which is not working for me. Please assist. Thanks

What does '::' mean in constructs '$::foo' and '@::foo'?
3 direct replies — Read more / Contribute
by jmeek
on Oct 06, 2015 at 02:50

    In Damian Conway's "Abbot and Costello Who's on First'" Parse::RecDescent example code in "Computer Science & Perl Programming" he starts the code with the line

    use vars qw( %base %man @try_again);

    and then in the action blocks in the grammars uses those variables with the syntax, e.g., $::man{$item[4]} and @::try_again.

    As I thought the point of the 'use vars' pragma was to allow the variables to be used globally without writing the fully qualified name, I'm puzzled as to why the double colons are used.

Perl OPC
2 direct replies — Read more / Contribute
by TFarmer
on Oct 05, 2015 at 14:58
    Im looking for code to use WIN32::OLE::OPC to read and write to a single tag on a channel/device I just need to read and write the value of the tag
call external sub ref from method in single line
4 direct replies — Read more / Contribute
by previous
on Oct 05, 2015 at 09:09
    Can I call a reference to an external sub (supplied via the constructor) from inside an object method using a single line instead of the two I've used.
    sub new { my $class = shift; my $self = {}; bless( $self, $class ); $self->{num} = shift; $self->{sub_ref} = shift; # JUST PASSING IN....REFERENCE TO EXTERN +AL SUB...\&ord_sub return $self; } sub doit { my $self = shift; #QUESTION...CAN I REDUCE THE NEXT TWO LINES TO ONE my $pSub=$self->{sub_ref}; $pSub->(); }
    Thank you in anticipation
Best practices for warnings about wrong context
5 direct replies — Read more / Contribute
by vsespb
on Oct 04, 2015 at 11:38

    Let's suppose I've created a library with the following API:

    sub myfunc { my ($ref) = @_; # modify $ref somehow warn "Should be called in void context" if defined wantarray; }

    The function does something, but does not returns anything useful. It returns nothing. I can add "return ;" but this does not change things - function returns nothing and I don't want it to be used in non-void context. Purpose of the last warning in function is simple: the function returns nothing, and if someone called it in scalar/list context, he's doing something wrong. So this warning adds more strictness to user's code:

    examples of user's code:

    example 1:

    func1(); myfunc($data); func2();

    everything ok - no warning

    example 2:


    there will be the warning! user obvious did something wrong here!

    example 3:

    sub anotherfunc { # .. code here myfunc($data) } func1(anotherfunc())

    there will be the warning! and indeed this code isn't sane.

    So far everything going well, and the warning seems useful.

    However, let's look on Catalyst now:

    sub xxx : Local Args(0) { my ($self, $c, $s, $r, $p) = @_; $c->res->body( wantarray ); }

    It's the Catalyst action, which return value is ignored, but it's called in list context.

    Sometimes action return value is important (see "Any data returned from the action forwarded to, will be returned by the call to forward." in manual), sometimes no. But catalyst always call actions in list context.

    Now the quesions:
    (a) Is that good practice to create library, which warns for "defined wantarray" for functions, returning nothing?
    (b) Are libraries like catalyst obligated to never call user's callback in non-void context if it's value is ignored?
    I assume if (b)=no, then (a)=no.
    Is there a convention for such kind of things? (or let me rephrase: if I am getting this warning when using my library with Catalyst, who's fault is that? the library's or Catalyst's)

removing leading 0's
4 direct replies — Read more / Contribute
by CSharma
on Oct 03, 2015 at 11:48
    Hi PMs, The output should be "10 & 10 are equal", but this gives output "10 & 8 aren't equal". I don't understand this! perhaps because of leading 0's perl converted 0010 to octel i.e. 8
    I tried removing leading 0's, still the same output "10 & 8 aren't equal".

    Please help to overcome this.
    Actually I'm reading values from a file in which lots of values have leading 0's & I have to compare this value to some other from different file which doesn't have leading 0's.
    #!/usr/bin/perl $x = 10; $y = 0010; $y =~ s/^0+//; ##OR $y += 0; if($x == $y) { print $x . " and " . $y . " are equal\n"; } else { print $x . " and " . $y . " aren't equal\n"; }
Handling japanese file & comparison
2 direct replies — Read more / Contribute
by CSharma
on Oct 02, 2015 at 14:16
    Hi PerlMonks,

    1. A Japanese compressed file(tab separated), Say A having products information (iphone etc), columns: name, description, image, uniqueid, rid1, rid2, url
    2. Another file, say B having mid, rid, ntid (column separated).
    3. rid1, rid2 => A & rid => B are numerical values.

    What I want:
    If rid from B matches rid1(from A), get that line form A into third file, Say C. In case, rid is null then rid(from B) to be matched with rid2 (from A). If rid1 & rid2 don't have values, leave that line.
    Also I've to add some values to the url (from A) & then it's to be pushed to 3rd file C after rid comparison.

    The main problem I've are:
    1. How to handle the japanese? Do I have to use any modules? as saving the japanese character to a perl variable turn to junk.
    2. There are 30 files like file A, each having 1 million of lines in them. What would be the efficient way to do this in perl?

    Help is much appreciated!

New Cool Uses for Perl
Edit files automatically cross-platform, line-endings unknown
No replies — Read more | Post response
by stevieb
on Sep 30, 2015 at 16:56

    I had a need for a piece of my software to be platform independent when reading/writing files with any type of line endings. So, I'm sure there are better/more efficient ways to do this, but I couldn't find one and really wanted to better understand record separators, so I wrote File::Edit::Portable.

    This module will read in a file on any platform, save the existing record separators (line endings), and modify them to the local platforms endings for use. It'll return either a file handle or an array of the file contents.

    You can optionally then push back an array of contents, and the module will rewrite the file (or optionally a new one), using the line endings that were found while reading (or optionally a user supplied one). This means it'll open Unix files on Windows and rewrite the file with Unix endings, and vice-versa on Windows and Mac (the three I've tested).

    use warnings; use strict; use File::Edit::Portable; my $rw = File::Edit::Portable->new; my $recsep; $recsep = $rw->recsep('unix.txt'); print "unix file before write: $recsep\n"; $recsep = $rw->recsep('win.txt'); print "win file before write: $recsep\n"; my $ufh = $rw->read(file => 'unix.txt'); my @unix_contents = <$ufh>; close $ufh; $rw->write(contents => \@unix_contents); $recsep = $rw->recsep('unix.txt'); print "unix file after rewrite: $recsep\n"; my $wfh = $rw->read(file => 'win.txt'); my @win_contents = <$wfh>; close $wfh; $rw->write(contents => \@win_contents); $recsep = $rw->recsep('win.txt'); print "win file after rewrite: $recsep\n";


    # unix $ ./ unix file before write: \0a win file before write: \0d\0a unix file after rewrite: \0a win file after rewrite: \0d\0a # windows E:\test>perl unix file before write: \0a win file before write: \0d\0a unix file after rewrite: \0a win file after rewrite: \0d\0a

    We've also got a non-OO interface:

    use File::Edit::Portable qw(pread pwrite); my $fh = pread('file.txt'); # or even my @contents = pread('file.txt'); # then, later pwrite('file.txt', \@contents);

    I definitely know it's quite inefficient, but it's my first working prototype. There are a lot of things missing (unicode and open filters for instance). Criticisms or existing ways to do this are very welcome.


New Monk Discussion
How can one find his previous preview page (uncreated)?
2 direct replies — Read more / Contribute
by u65
on Oct 03, 2015 at 11:48

    Is there a procedure for finding an uncreated preview node?

    A couple of times now I've started a post but got interrupted before I pushed "create" and so far have found no reliable way to easily find what I started. If I don't go too far astray, I can keep pushing the back arrow in my browser and find it, but if I log off or otherwise close the page I can't always find it again. I have not been able to find much searching about preview here except the threads on wanting a preview capability for updates.

    Note that when working on longer posts I've kept text in an offline file to edit and copy from, but that seems to be a pain for relatively small posts, but I guess I could make that a habit.


Log In?

What's my password?
Create A New User
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (15)
As of 2015-10-07 12:46 GMT
Find Nodes?
    Voting Booth?

    Does Humor Belong in Programming?

    Results (184 votes), past polls