Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

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
use warnings is complaining
5 direct replies — Read more / Contribute
by Anonymous Monk
on Aug 15, 2018 at 09:34

    Hi, i have written a script that reads some data and reports about it. Only when i use warnings i got the following complaint: 'Argument "" isn't numeric in printf at C:\Strawberry\codes\pack.pl line 11, <DATA> line 3.

    Here's my code:

    use strict; #use warnings; printf("%-11s %-27s %9s %11s","Date","Description","Incoming","Outgoin +g\n"); my $x = 0; my $tot; my $totex; while(<DATA>){ if($x==0){$x++;next;} my($date,$des,$inc,$exp)= unpack("A10 A27 A10 A*",$_); printf("%-10s %-27s %10.2f %10.2f\n",$date,$des,$inc,$exp); $tot += $inc; $totex += $exp; } printf("%38s %10.2f %10.2f","Totals",$tot,$totex); __DATA__ Date Description incoming outgoing 01/24/2001 Zed's Camel Emporium 100.00 1147.99 01/28/2001 Flea spray 24.99 01/29/2001 Camel rides to tourists 235.00 01/31/2001 avage1 125.00 01/20/2001 carpe diem 20.00 23.00

    How can i get rid of this, besides from turning warnings off?

Net::SSH2::Cisco and Nexus switches
2 direct replies — Read more / Contribute
by cnoyes72
on Aug 14, 2018 at 13:44

    I'm trying to use the Net::SSH2::Cisco module against Cisco Nexus switches and keep getting a timeout when executing the command. The logs show the command executed (I recieved the expected response from the switch), but the script timesout. This same script runs perfectly against and IOS switch. I think it may be waiting for the prompt but doesn't recognize it. Has anyone had luck getting this to work with Nexus gear?

    #!/usr/local/bin/perl # # use strict; use warnings; use Net::SSH2::Cisco; my $t = Net::SSH2::Cisco->new( host => 'nexus-switch', Dump_log => '/root/dump.log', Output_log => '/root/output.log', Input_log => '/root/input.log', Waitfor_clear => '0' ); $t->login( Name => 'name', Password => 'passwd' ); # Execute Command my @output = $t->cmd(string => "show clock"); print @output; $t->close;
Spumux loop to add subs..
2 direct replies — Read more / Contribute
by armight29
on Aug 14, 2018 at 12:22

    hello trying to create a loop which will iterate through an array of .xml files to add subtitles to a clip with spumux..but not working. Here is code:

    foreach my $i(0 .. $#subxmlfiles) { chdir('/users/dragonzero29/.wine/drive_c/mvtmp') or die "Could not cha +nge to dir...$!"; my @subxmllist=glob("@subxmlfiles"); system("spumux -s$i -m dvd -P \"/users/dragonzero29/.wine/drive_c/mvtm +p/$subxmllist[$i]\" < \"/users/dragonzero29/.wine/drive_c/mvtmp/$MM.M +PG\" > \"/users/dragonzero29/.wine/drive_c/mvtmp/.\" . ++$MM . \".MPG +\""); }

    The array @subxmlfiles is created earlier in the script and if you're not familiar with spumux it takes an .xml file and uses it with an input clip to create subtitles and then outputs to your designated filename; if you have multiple .xml files (for multiple languages) then

    each invokation of spumux MUST use the previous muxed clip to add to...I hope this makes sense. This is where I run into problems. the first clip (without subs) should be 0.MPG. This will be the input file and it will be used with the first .xml file in the array and the output file should a filename with $MM incremented by 1 which will be 1.MPG; 1.MPG will then be the input for the next iteration of the loop and so on and so forth so your expertise will be greatly appreciated :)

Compile perl for performance
2 direct replies — Read more / Contribute
by learnedbyerror
on Aug 14, 2018 at 10:55

    Oh so kind Monks

    Last night, I watched Graham TerMarsch's presentation "Red Wunz Go Fasta" from TPC 2018. It inspired me to go out and build perl 5.28.0 using perlbrew adding -D usemyalloc and -D optimize="-O3" flags. I have run a few benchmarks for one of my applications that munges through a large corpus of data files and builds several LMDB databases containing the parsed/analyzed information. The result is that I have shaved almost 25% of my runtime for this one program from per 5.26.2. This is running in a Debian Jessie LXC on a proxmox physical host running Debian Stretch

    In doing the above, I violated one of the cardinal rules of "Optimization Club" - make one change at a time. I changed both perl versions and two compiler flags. I promise to be more disciplined next time.

    My question to you oh so wise ones is - what options should I consider when trying to optimize against my specific code based?

    Some of those that come to mind are:

    • Compiler and compiler version - gcc/clang
    • glibc version
    • compiler flags
    • perl features - such as no threads

    At this time, my platforms of interest are Linux and Macos. My current plan is to compare 5.26.2 to 5.28.0 only. I have done previous comparisons with earlier versions of perl and have seen significant performance increases from versions prior to 5.26.0. This is primarily due to the changes in hash generation as my applications tends to be hash heavy.

    I kindly request that those of you who may be inclined to inform me using intemperate language that this investigation is folly save your time and not try to convince a fool of his self-recognized folly

    I do greatly appreciate those who do offer up options. I will happily update this thread with my findings.

    Thank you in advance

    lbe

Implementing named infix operators with parser hook?
1 direct reply — Read more / Contribute
by LanX
on Aug 12, 2018 at 09:04
    To those familiar with the Perl guts ...

    The following code demonstrates two approaches to "simulate" a named infix operator in Perl

    use strict; use warnings; package mypkg; use Carp; sub BETWEEN { carp "left = ", shift; carp "right = @_"; }; our $BETWEEN = \&BETWEEN; 2->$BETWEEN(1,3) ; BETWEEN 2 => 1,3; #2 BETWEEN (1,3) ;

    But commenting out the last line naturally leads to compile-time parser error:

    Bareword found where operator expected at d:/Users/lanx/tmp/rewrite_bi +nary.pl line 20, near "2 BETWEEN" (Missing operator before BETWEEN?) syntax error at d:/Users/lanx/tmp/rewrite_binary.pl line 20, near "2 B +ETWEEN " Execution of d:/Users/lanx/tmp/rewrite_binary.pl aborted due to compil +ation errors.

    Question:

    Is there a clean approach to hook into the parse to catch this error event and to rewrite the code?

    Such that

    2 BETWEEN (1,3)

    is translated to

    2->$BETWEEN(1,3)

    if ref $BETWEEN eq 'CODE' and parsing continues?

    No source filters please... :)

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

    update

    I stumbled over perlguts#Custom-Operators but didn't understand the restrictions. (especially how do I create a "custom peephole optimizer"?)

    update

    that's it https://metacpan.org/pod/optimize, so if I understand correctly, I need to register BETWEEN as custom operator and use optimize to provide a callback?

POST fieldnames with underscores
1 direct reply — Read more / Contribute
by BernieC
on Aug 11, 2018 at 13:45
    This is odd: I'm trying to post a form that has fields with underscores in them. It appears that LWP converts the underscores to hyphens. Can that be? {my problem is that the post doesn't work and I am trying to figure out why not} When I GET the page with the form on it and scrape it for INPUT fields, I find
    [...] $VAR53 = 'slagk%40xx.com_nomail'; $VAR54 = 'on'; $VAR55 = 'slagk%40xx.com_nodupes'; $VAR56 = 'on'; [...]
    but when I use Dumper to look at the _request object, it tells me
    [...] 'slagk%40xx.com-plain' => 'on', 'slagk%40xx.com-nomail' => 'on', 'qoz%40puz.org-language' => 'en', [...]
    I looked at the code in HTTP::Request::Common and didn't see anything obvious that might be causing this {if this is actually happening, of course}

    two questions: is LWP really converting underscores to hyphens? And if so, how can I pass an underscore through to the web server.

Failure with IO::AIO
No replies — Read more | Post response
by vr
on Aug 11, 2018 at 07:10

    Looks like async file operations are popular topic; I also was trying, recently, to replace what I'm using with something more elegant. However, async file copy (aio_copy) fails, at the step (see description) of changing file atime and mtime. So it can be reduced to failure of aio_utime.

    use strict; use warnings; use feature 'say'; use AnyEvent; use AnyEvent::IO; my $c = AnyEvent-> condvar; aio_utime( 'x', 0, 0, sub { $c-> send( @_ )}); say $c-> recv;

    File named 'x' is in current directory, IO::AIO and AnyEvent::AIO should be installed. The above example works in Linux, but not in Windows (where actual script is to be used). Perl dies silently in Win10, or with usual OS messagebox "Application needs to be closed blah-blah" in e.g. Win2008.

    I found that if this line is replaced with

    int i; printf( "calling\n" ); i = utime (filename, &buf); printf( "called\n" ); return i;

    then I only see "calling", and process crashes. A call to "utime" looks quite innocent, of course it works from either simple C program or wrapped into Perl and Inline::C. Apart from reporting a bug, perhaps there is some simple and quick fix?

WebService::YQL "Couldn't find a decoder method"
1 direct reply — Read more / Contribute
by chuntuk
on Aug 11, 2018 at 07:05
    I have some established perl code that uses WebService::YQL to access web services, it's been working fine for years. But for the last few days any script using that module has crashed out with this error:
    Couldn't find a decoder method. at /home/redacted/perl/usr/share/perl5/WebService/YQL.pm line 9.
    BEGIN failed--compilation aborted at /home/redacted/perl/usr/share/perl5/WebService/YQL.pm line 9.
    
    I get the error even with a test script like this:
    use CGI::Carp qw(fatalsToBrowser);
    
    use WebService::YQL;
    
    print "Content-type: text/plain\n\n";
    
    print "Hello World\n";
    
    Line 9 of YQL.pm is "use JSON::Any;", which is deprecated, but I don't see what I can do about that. Could anyone help me figure out what the problem is and how to fix it? Thanks.
Libxml parser cosuming 100% cpu
5 direct replies — Read more / Contribute
by geek2882
on Aug 11, 2018 at 02:50
    Hi Monk how can i optimize the parser. its take 2.5 min to finish the job but cpu usage is 100% untill the job finish script. i have post a design and a small view of code
    xml File open store on array of @lines(file contain record block) foreach line(@lines) #10Lakhs Line { $stringparse .=line # stringparse get the record (maybe of 100 lin +es which go to if block for parsing) IF(Endtags of block)# Execute 10000 times for each record($str +ingparse 100 lines xml) { $XML::LibXML::skipXMLDeclaration = 1; our $dom = XML::LibXML->load_xml(string => $stringparse); our $xml = $dom->documentElement; . . . #Some of use Api of my Code $method = $xml->getChildrenByTagName('Method')->to_literal; $Value = $xml->getChildrenByTagName("$Check")->to_literal; $bune = $xml->getChildrenByTagName('Number')->to_literal; if ($xml->findnodes('./Indi/Lost/true') ) { } if ( $xml->findnodes('./Indi/Lost/true') || $xml->findnodes('. +/Indi/Losgshshsht/false') ) { } if(($xml->findnodes('./nike'))[0]->firstChild){ ($xml->findnodes('./nike'))[0]->firstChild->setData($localS); } my $Tag =$dom->createElement('Nike_identifier') + $Tag->appendText($localS); $xml->addChild($Tag); $org= $xml->findnodes('./NikkooIdentifiers/abc')->to_literal; } }
Show all errors on compile time
5 direct replies — Read more / Contribute
by ifazlives
on Aug 08, 2018 at 15:12
    I recently upgraded Perl from 5.18 to 5.22 and I noticed a difference at the compile time [ perl -c <script> ] experience.

    On 5.18, I used to get all the syntax errors at compile time in a single compilation.

    But on 5.22, the compile process exists as soon as it hits the first syntax error. I will have to fix this error and re-compile it to check for further syntax errors.

    Is there a way to get all the syntax errors at compile time in a single compilation.

    Regards, IE
New Cool Uses for Perl
Exploring Type::Tiny Part 4: Using Types::Standard as a Ref::Util-Like Library
No replies — Read more | Post response
by tobyink
on Aug 12, 2018 at 11:50

    Type::Tiny is probably best known as a way of having Moose-like type constraints in Moo, but it can be used for so much more. This is the third in a series of posts showing other things you can use Type::Tiny for. This article along with part 1, part 2, and part 3 can be found on my blog and in the Cool Uses for Perl section of PerlMonks.

    Even if you read the documentation of Types::Standard pretty thoroughly, you'd probably miss that you can do things like this:

    use Types::Standard qw(is_ArrayRef is_HashRef); if (is_ArrayRef($var)) { ...; } elsif (is_HashRef($var)) { ...; }

    It is documented that Types::Standard exports functions called ArrayRef and HashRef, which are constant-like functions returning Moose/Moo-compatible type constraint objects, but where did these is_ArrayRef and is_HashRef functions come from?

    Well, their existence is documented in Type::Library, the type library base class used by Types::Standard. Any type library built with it will offer is_* variants of type constraints. These functions check their argument and return a boolean indicating whether it passes the type constraint.

    The object-oriented way of writing these checks is like this:

    use Types::Standard qw(ArrayRef HashRef); if (ArrayRef->check($var)) { ...; } elsif (HashRef->check($var)) { ...; }

    Though the object-oriented way is a little slower because it will result in at least three sub calls (including a method call).

    The is_* functions should be pretty darn fast, especially if Type::Tiny::XS is installed. Ref::Util::XS is faster, and Params::Util is sometimes faster, but using Type::Library-based type libraries (such as Types::Standard, Types::Common::Numeric, Types::Common::String, Types::Path::Tiny, Types::XSD, etc) will give you a richer selection of types that you can check.

    Assertions

    A common use for type checking functions is to do something like:

       is_ArrayRef($var) or die(...);

    Type::Library-based type libraries offer a shortcut for this:

       assert_ArrayRef($var);

    The return value of the assert_* functions (if they don't die) is the parameter you passed to them, which makes it convenient to do things like:

    use Types::Standard qw(assert_Object assert_ArrayRef); sub process_data { my $self = assert_Object( $_[0] ); my $data = assert_ArrayRef( $_[1] ); ...; }

    The object-oriented equivalent of assert_Object($thing) is Object->assert_return($thing). Due to overloading Object->($thing) will also work.

    Coercions

    If a type constraint has coercions (like Path from Types::Path::Tiny), there's also a to_* function:

    use Types::Path::Tiny qw( to_Path ); my $path = to_Path($thing);

    Note that if a coercion fails, there is no exception thrown, and the original value is passed through unaltered. If you want to make sure coercion succeeded:

    use Types::Path::Tiny qw( assert_Path to_Path ); my $path = assert_Path( to_Path($thing) );

    The object-oriented equivalent of to_Path($thing) is Path->coerce($thing). The object-oriented equivalent of assert_Path(to_Path($thing)) is Path->assert_coerce($thing).

    Parameterized Types

    It would be pretty cool if you could do:

    if (is_ArrayRef[Int]($var)) { ...; }

    But that wouldn't be syntactically valid Perl.

    You can do this though:

    use Types::Standard qw(ArrayRef Int); BEGIN { my $type = ArrayRef->of(Int); *is_ArrayRef_of_Int = $type->compiled_check; *assert_ArrayRef_of_Int = \&{ $type }; *to_ArrayRef_of_Int = sub { $type->coerce(@_) }; } if (is_ArrayRef_of_Int($var)) { ...; }

    Exporting Tricks

    To export just Object:

       use Types::Standard qw(Object);

    To export just is_Object:

       use Types::Standard qw(is_Object);

    To export Object and is_Object:

       use Types::Standard qw(Object is_Object);

    To export Object and all related functions (is_Object, assert_Object, and to_Object):

       use Types::Standard qw(+Object);

    To export Object, ArrayRef, and all the other types:

       use Types::Standard qw(:types);

    To export Object, ArrayRef, all the other types, and the related is_* functions:

       use Types::Standard qw(:types :is);

    To export Object, ArrayRef, all the other types, and the related is_*, assert_*, and to_* functions:

       use Types::Standard qw(:types :is :assert :to);
Exploring Type::Tiny Part 3: Using Type::Tie
No replies — Read more | Post response
by tobyink
on Aug 08, 2018 at 13:02

    Type::Tiny is probably best known as a way of having Moose-like type constraints in Moo, but it can be used for so much more. This is the third in a series of posts showing other things you can use Type::Tiny for. This article along with part 1 and part 2 can be found on my blog and in the Cool Uses for Perl section of PerlMonks.

    This works:

    use Types::Standard qw(Int); tie(my @numbers, Int); push @numbers, 1, 2, 3; # ok push @numbers, "four"; # dies

    Well, if you try it, you may find it complains about not being able to load Type::Tie.

    Type::Tie is an add-on for Type::Tiny distributed separately. It's an optional dependency, so if you want to use this feature, you'll need to make sure it's installed.

    Coercions

    This tie feature automatically supports coercions.

    use Types::Standard qw(Int Num); my $RoundedInt = Int->plus_coercions( Num, 'int $_' ); tie(my @numbers, $RoundedInt); push @numbers, 1, 2, 3; # ok push @numbers, 4.2; # rounded to 4 push @numbers, "five"; # dies

    More about Type::Tie

    Type::Tie is designed to be pretty independent of Type::Tiny. You can use it with MooseX::Types, Mouse::Types, and Specio, and it also bundles its own nanoscale type constraint library Type::Nano.

    use Type::Tie qw(); use MooseX::Types::Moose qw(Int); tie(my @numbers, "Type::Tie::ARRAY", Int);

    To save yourself typing "Type::Tie::ARRAY", "Type::Tie::HASH", and "Type::Tie::SCALAR" all the time, Type::Tie offers a convenience function ttie:

    use Type::Tie qw(ttie); use MooseX::Types::Moose qw(Int); ttie(my @numbers, Int);

    Use in Attributes

    Perl has a type checking hole thanks to references:

    use v5.16; package Foo { use Moo; use Types::Standard qw(ArrayRef Int); has numbers => ( required => 1, is => 'ro', isa => ArrayRef[Int], ); } my $foo = Foo->new( numbers => [1, 2, 3] ); push @{ $foo->numbers }, "hi"; # this is allowed

    The type constraint is only checked in the constructor and in writers/accessors.

    Tying the array allows you to perform type checks and coercions on any new elements added to the array. It's a use for trigger that doesn't suck!

    use v5.16; package Foo { use Moo; use Types::Standard qw(ArrayRef Int); has numbers => ( required => 1, is => 'ro', isa => ArrayRef[Int], trigger => sub { tie @{$_[1]}, Int }, ); } my $foo = Foo->new( numbers => [1, 2, 3] ); push @{ $foo->numbers }, "hi"; # dies

    With a little bit of work (okay, a lot!) it should be possible to even check deeply nested structures.

    Performance

    While effort has been made to optimize Type::Tie, tied variables are necessarily slower than untied ones.

    If you have an array you want to make sure only contains integers, but you don't want to compromise on performance, you could enable the tie only when you run your test suite, and trust that your test suite will be enough to trigger any potential errors.

    use Types::Standard qw(Int); use Devel::StrictMode qw(STRICT); my @array_of_ints; tie @array_of_ints, Int if STRICT; ...; # do stuff here

    Devel::StrictMode is a module which exports a constant called STRICT which will be true if the PERL_STRICT, EXTENDED_TESTING, RELEASE_TESTING, or AUTHOR_TESTING environment variables is true, and false otherwise.

Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2018-08-15 16:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Asked to put a square peg in a round hole, I would:









    Results (161 votes). Check out past polls.

    Notices?