Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

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
Best practices for warnings about wrong context
1 direct reply — 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!

XML::SAX parameters
1 direct reply — Read more / Contribute
by Artimus
on Oct 02, 2015 at 08:53

    Hi everyone!

    I wonder if someone could explain me the way I can pass parameters to my SAX handler or, maybe receive smth back after $parser->parse_file($fh) worked.

Perl Modules Scope ?
4 direct replies — Read more / Contribute
by Anonymous Monk
on Oct 02, 2015 at 02:01
    Hi, I am writing a login screen and and have following files: lib.cgi and login.cgi in i have following code: package config; our $abcd = 1; 1; in lib.cgi i have following code: use config; 1; in login.cgi i have following code: use strict; required 'lib.cgi'; print $config::abcd; on compiling this I get an error. The error goes away if i "use config" in login.cgi. I thought since I am including lib.cgi inside login.cgi I would not have to use config inside login.cgi Please help me understand this better
Use LWP get no URL response
3 direct replies — Read more / Contribute
by hoanglearning
on Oct 01, 2015 at 20:32

    My program don't get URL response in search for capacitor part from DIGI-key. I received good response when URL is typed in directly on Google Chrome browser. When running my program, it shows the HTTP status of 302 without URL redirect and no response is received. Please help to point out what wrong with my codes. Thanks so much in advance

    use strict; use warnings; use LWP; my $browser = LWP::UserAgent->new; $browser->env_proxy; my $url = ' +74K4PACTU&WT.z_header=search_go'; my $response = $browser->get( $url ); print "HTTP status: ", $response->code( ), "\n"; unless ($response->is_success) { print "no response\n"; } my $response_content = $response->content; print "response_content= $response_content\n";

    The modified codes from Sathishkumar resolved this problem. Sathishkumar, thanks verymuch. I got your codes and it's working. I still need to learn the line "$cookie->extract_cookies($res);" to see what it exactly doing. I also like to thanks Perlmonk and MidLifeXis. I can't not get this problem solved without your help.

Handling SIG INT multiple times
1 direct reply — Read more / Contribute
by delight
on Oct 01, 2015 at 10:15
    Hello guys I'm using sig int to communicate between my class and the script who use it. In detail i'm using signals to change the flow of the script in certain situation. This works okay but the problem is that my sig int handler looks something like:
    sub handler { # call some log stuff here a_function() if($myobject->{something} eq 'lorem'); b_function() if($myobject->{something} eq 'ipsum); #etc }
    my class send a signal to change the flow of the script in some scenarios, so its just a: kill 'INT', $$; Thing is that after the first okay the handler runs a_function() ( which is a long running function who dont return) if something happens and the class sends another kill, handler() will not be called because, I guess, for the OS or Perl we are still handling the first signal. So my question is there's a solution?
switch statement
4 direct replies — Read more / Contribute
by fionbarr
on Oct 01, 2015 at 09:18
    when I try to install 'Switch' in ActiveState PPM it says 'do not use if you can use given/when'. When I use 'given/when' I get the message 'given is experimental' and the program will not compile. I do not want a pile of 'if' statements if possible. What to do?
OAuth 2.0 - Starting Lynx from within a Perl script
2 direct replies — Read more / Contribute
by jprimes
on Oct 01, 2015 at 06:11

    Dear Monks,

    I would like to achieve the following:

    An HTTP client implemented in Perl should redirect the user to an OAuth 2.0 authentication server. The user should authenticate exclusively through a web browser (e.g. Lynx) launched by the Perl script, since the HTTP client must not know the user's credentials.

    Once the user has been successfully authenticated by the OAuth 2.0 authentication server through the browser, the HTTP response contains a redirect URI (typically in the Location header).

    Question: is there a way to come back from the Lynx process to the Perl script, capturing the HTTP response received by Lynx in a Perl variable?

    Please note that using a third-party user-agent is a requirement (so nothing for LWP).

    Thank you in advance.

Combine files with same extension in new file
6 direct replies — Read more / Contribute
by AhmedABdo
on Oct 01, 2015 at 04:47
    Hi guys, I have many txt files in one directory, and want combine them all in one new file but with excluding the header of each file. I know how to do it using linux command, but I want to do it in Perl. Thanks and I appreciate any help
How to read from text previously copied into memory buffer into perl script?
2 direct replies — Read more / Contribute
by daggett
on Sep 30, 2015 at 19:45

    I have searched the Internet (1) and am unable to find how to read from the memory buffer from which I have copied text (using Ctrl-C, Select->Copy, ...) into a perl script. (2)

    An example would be, from, the story "Bank dividends may not be sacrosanct after all" at (3) .

    I would like to transform the above title into :


    ... and ultimately into :


    ... which would be the name of the file to which I would save the HTML.


    (1) I have used the search terms "perl how-to read memory buffer" and "perl how-to read memory buffer -file".

    (2) For now, I am trying to write a perl script to run from the command line on an xterm window. I would prefer to create a small rudimentary X11 input form. Is that possible with perl?

    (3) Where the URL itself describes the page content, as in the given example, I use text fromm that URL, but many web pages don't have such descriptive URLs. I those cases, I copy the title of the page and begin to laboriously transform the title as I have shown.

xs wrapper for a threaded library
2 direct replies — Read more / Contribute
by fng
on Sep 30, 2015 at 16:32

    I am writing an XS wrapper to a library that makes its own threads and also expects C style callbacks. The intent is to use this library in a perl application, not in a C application with embedded perl. My design hinges on being able to call a perl sub within the C-style callback, allowing this library to throw events at me and for me to handle these events in perl. The problem is that the library in question is multi-threaded, and I never know what thread it will be executing in when it calls the C-style callback. From there I cannot call any perl API functions because I have no interpreter context. I realize that this is because perl is meant to start the threads so it can clone the interpreter at that point; because the library manages its own threads I end up unable to look up an interpreter.

    So, my question is, what should I do? I can try to start a perl interpreter within the callback, but I have none to clone and I am nervous about managing the collection of cloned interpreters myself. I could try to handle the callback by passing info about the events back to one of the perl-capable threads in some pure-C based manner (zmq, is my first guess). I'm not sure if there is a better option, any suggestions or advice would be 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 surveying the Monastery: (4)
As of 2015-10-05 02:47 GMT
Find Nodes?
    Voting Booth?

    Does Humor Belong in Programming?

    Results (106 votes), past polls