Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
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
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 = \≬ 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
Code Loop?
4 direct replies — Read more / Contribute
by bitman
on Aug 08, 2018 at 09:17
    How does this create a loop? I was experimenting.
    use 5.010; my @x = ("x" => 1); my $y = \@x; say $#@x[1];
String comparison in an array
3 direct replies — Read more / Contribute
by newperlbie
on Aug 08, 2018 at 08:37
    Hi, I have the following code:
    .... .... for (....) { push @keys,$key; } my (@elements) = split ';',$line; if ((grep { $elements[0] eq '$_' }@keys)) { print "found"; }
    $elements[0] is present in the array @keys, but this code is not matching them, what am I missing? is it something to do with string comparison with quotes and no quotes?
Reading progress of "copy" executed asynchronously
11 direct replies — Read more / Contribute
by neWerminder
on Aug 08, 2018 at 05:51
    Hello, i have quite a problem to manage and i cant really manage to do so myself so im seeking for help here. What i need to do is to get % of file copied somehow displayed (or captured to variable even better). So on windows im using cmd's "copy" command. copy s.txt b.txt /z Displays nice percentages of progress. Now.. i could add >output.txt at the end to get that percentages to file.. but thats quite a spam of them and thats not what i want to achieve. Generally i tried with backticks and with open command and i cant really get it to work. In best case i received 100% at the end of copy but i dont know how to execute copy and read how much % there is. Any help with making it happening would be greatly appreciated.
Net:SSH2 channels
3 direct replies — Read more / Contribute
by BernieC
on Aug 07, 2018 at 16:10

    I'm trying to get a Net::SSH2 program working and I'm running into a snag. The code seems to be straight from the manual and is dead simple;

    $ssh2->connect(HOST) or $ssh2->die_with_error ; $ssh2->auth_password($login{user}, $login{password}) ; $ssh2->auth_ok() ; print "Logged in\n" ; my $chan = $ssh2->channel() or ssh2->die_with_error ; print "never get here\n" ;

    It zips through connecting and authenticating and then just hangs there -- the channel() never returns. I haven't a clue what I've done wrong...

Prototype Mismatch Errors
5 direct replies — Read more / Contribute
by mavericknik
on Aug 07, 2018 at 15:07
    Hi, I'm changing a large file coded by someone else and am getting prototype errors. Snippet:
    use Getopt::Long; use Data::Dumper; use List::Util qw[min max]; use POSIX; use Cwd 'abs_path'; use Date::Parse; use Date::Format; print "Hello World\n";
    Gives:
    Prototype mismatch: sub main::strftime: none vs ($\@;$) at /pkgs/perl/ +5.14.1/lib64/5.14.1/Exporter.pm line 67. at ./trial.pl line 9 Prototype mismatch: sub main::ctime: none vs ($;$) at /pkgs/perl/5.14. +1/lib64/5.14.1/Exporter.pm line 67. at ./trial.pl line 9 Prototype mismatch: sub main::asctime: none vs (\@;$) at /pkgs/perl/5. +14.1/lib64/5.14.1/Exporter.pm line 67. at ./trial.pl line 9 Hello World
    I understand this is because these functions are begin declared multiple times? What is the best way to fix these warnings without having to go through the whole code? Is it even possible? Thanks!
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 the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2018-08-14 17:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Asked to put a square peg in a round hole, I would:









    Results (153 votes). Check out past polls.

    Notices?