Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

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
Bookmarking PDF by string
2 direct replies — Read more / Contribute
by ReverendDovie
on Jan 16, 2019 at 15:05
    Hello, first time poster. Hope I get it right.

    I'm trying to do the following:

    1) Convert an HTML page to a PDF
    2) Add the appropriate bookmarks in that PDF
    3) Join it with a pre-fab "title page" PDF

    I have found the answers (I'm pretty sure, haven't fully tested yet) to 1 and 3. Number 2 is getting me a bit. I found the bookmarking ability of PDF::Reuse to be close, but I want to bookmark to a specific string, not a page number since I won't necessarily know the right page number since the PDF was just generated back in step one.

    Is there a way to do the bookmarking thing but to a specific string (which I can preset when building the HTML)?

    Thank you
Reading a hash structure stored in a file
5 direct replies — Read more / Contribute
by sam1990
on Jan 15, 2019 at 15:08

    Hello, I have a file that has a hash stored in it. I am trying to read that hash as it is using eval but I am getting following error: Global symbol "%hash1" requires explicit package name (did you forget to declare "my %hash1"?) at line (print Dumper(\%hash1);) 14. Execution of aborted due to compilation errors. Please help me understand the issue here, thank you : my %hash1 = (hello => 1, hi =>2 ); #!/home/utils/perl5/perlbrew/perls/5.24.2-021/bin/perl use strict; use warnings; use Path::Tiny qw( path ); use Data::Dumper; my $file = ''; open(my $fh, '<', $file) or die "Could not open file $file"; eval($fh); close $fh; print Dumper(\%hash1);
Invoke the Perl string interpolation engine on a string contained in a scalar variable.
1 direct reply — Read more / Contribute
by ibm1620
on Jan 15, 2019 at 12:49
    I want to be able to take arbitrary lines containing variables that are defined in the program, and interpolate them.
    #!/usr/bin/env perl use 5.010; use warnings; use strict; my $var1 = "abel"; my $var2 = "baker"; my $var3 = "charlie"; while (my $line = <DATA>) { chomp $line; say "Before interpolation: $line"; say "After interpolation: " . perform_interpolation($line); say ''; } sub perform_interpolation { my $text = shift; # now what? } __DATA__ I'd like to see this one: $var1. How about \$var3? You shouldn't interpolate \$var3 (but it would be ni +ce if you'd remove the backslash) Try a concatenation: $var1$var2
    I've seen String::Interpolate mentioned in my searches, but unless I'm misunderstanding something, I'd have to know in advance what variables I'd be interpolating

    What I'm trying to do is create a program template where a comment block right after the shebang line (possibly containing scalar variables like $program or other constants or environment variables) can be rendered to produce a usage statement.

    The perform_interpolation() subroutine would be part of the template and wouldn't know specifically what variables the programmer might want to interpolate for the usage statement.

    Can String::Interpolate do this? Or is there a simpler way?

Win32::GUI and threads issue
2 direct replies — Read more / Contribute
by Garden Dwarf
on Jan 15, 2019 at 05:20

    Hello Monks!

    I am trying to create a Win32 application (with Strawberry Perl (v5.14.4) on Win10) displaying graphic computations. In order to optimize the process, I want to divide the management of my virtual buffer into small pieces computed by individual threads, then compile the results and copy the virtual buffer on the screen.

    My problem is the combination of Win32::GUI and threads (I have also tried forkmanager without success). Threads without Win32 is ok, Win32 without threads is ok, but using both is not. Here is a simple sample code to illustrate the issue (you can enable/disable the use of Win32 with the variable $use_win or change the amount of threads with the variable $t_amount):

    #!/bin/perl use Win32::GUI(); use threads; use strict; use warnings; use Data::Dumper; my $use_win=1; # Create Win32 GUI interface (1) or not (0) my $t_amount=4; # Amount of threads to create my $textbox; my $win; my $draw; if($use_win){ # Initialize window $win=new Win32::GUI::Window( -left => 0, -top => 0, -width => 300, -height => 300, -name => "Window", -text => "Test", ); $win->InvalidateRect(1); $textbox=$win->AddTextfield( -name => "Output", -left => 5, -top => 5, -width => 275, -height => 255, -text => ""); # Start application $draw=$win->AddTimer('draw',1000); $win->Show(); Win32::GUI::Dialog(); }else{ draw_Timer(); } sub Window_Terminate{-1} sub draw_Timer{ my @threads; my @ret; my $c; my $d; # Assign range of computation to fork processes foreach $c(1..$t_amount){ $d=$c-1; push(@threads,threads->new(\&draw,($d*10),($d*10+10))); } foreach my $thread(@threads){ @ret=$thread->join; foreach my $data(@ret){ $use_win?$textbox->Append("|".$data):print"|".$data; } $use_win?$textbox->Append("\n"):print"\n"; } } sub draw{ my $begin=shift; my $end=shift; my @tbl; my $cpt; for($cpt=$begin;$cpt<$end;$cpt++){ push(@tbl,$cpt); } return(@tbl); }

    Any help would be welcome. I have searched for previous posts without finding a solution. I also googled with no luck. Thanks in advance!

Case where '( shift @_ )[ 0, 0 ]' returns only one value?
5 direct replies — Read more / Contribute
by rsFalse
on Jan 14, 2019 at 15:58
Dbic and inflating Oracle DATE columns - solved
1 direct reply — Read more / Contribute
by Ea
on Jan 14, 2019 at 05:27
    Newbie trying to get DBIx::Class to work with Oracle DATE columns. Managed to get my error to go away, so I'm putting this out there for expert comment and for those newbies searching on the same problem.

    I used dbicdump with components=["InflateColumn::DateTime"] on an Oracle schema which produced a Result class which has

    __PACKAGE__->load_components("InflateColumn::DateTime"); ... __PACKAGE__->add_columns( "start_time", { data_type => "datetime", is_nullable => 1, original => { data_type => "date" }, }, );
    but after searching with dbic and getting a resultset
    $resultset = $schema->resultset('Ical') ->search({ uid => $id });
    calling $resultset->start_time would give me DBIx::Class::InflateColumn::DateTime::catch {...} (): Error while inflating '24-APR-18' for start_time on Timetable::Schema::Result::Ical: Invalid date format: 24-APR-18. There are lots of hints in the documentation, but nothing explicit on how to avoid this error.

    I found that the error went away when I added the on_connect_call option to the connect method.

    my $schema = Timetable::Schema->connect("dbi:Oracle:$schema_name", $db_username, $db_password, {on_connect_call => 'datetime_setup'} );

    Just thought I'd get it down while it was fresh in my mind and I'll update the post when I know more about what I've done.



    The more documentation I read, the more I think I did the Right Thing.
  • How to connect
  • How to customize InflateColumn (which I didn't need)


    Sometimes I can think of 6 impossible LDAP attributes before breakfast.

    Mojoconf was great!

Broken cpan shell
1 direct reply — Read more / Contribute
by stangoesagain
on Jan 13, 2019 at 23:53
    Respectful obeisances, My Perl installation got updated to 5.28.1 by my OS (openSUSE) and ever since then some of my scripts complain about missing some of my modules and cpan interactive shell quits with complaints about lock files and segmentation faults. I can usually install missing modules using cpanminus and proceed doing whatever I was doing but I still need to fix cpan. As of now, I get this (edit: corrected misspelled command in original message):
    stan@linux-pwfe:~> sudo perl -d -MCPAN -e shell [sudo] password for root: Loading DB routines from version 1.53 Editor support available. Enter h or 'h h' for help, or 'man perldebug' for more help. main::(-e:1): shell *** buffer overflow detected ***: perl terminated Aborted stan@linux-pwfe:~>
    Cpan itself responds to "sudo cpan -h" and "sudo cpan -v" and it even updated cpan itself via "sudo cpan -i CPAN". It can't find and install missing "" this way, though. Edit - another example:
    stan@linux-pwfe:~> sudo cpan [sudo] password for root: Loading internal logger. Log::Log4perl recommended for better logging There seems to be running another CPAN process (pid 8012). Contacting +... Other job not responding. Shall I overwrite the lockfile '/root/.cpan/ +.lock'? (Y/n) [y] y cpan shell -- CPAN exploration and modules installation (v2.22) Enter 'h' for help. Segmentation fault stan@linux-pwfe:~> sudo rm /root/.cpan/.lock stan@linux-pwfe:~> sudo cpan Loading internal logger. Log::Log4perl recommended for better logging cpan shell -- CPAN exploration and modules installation (v2.22) Enter 'h' for help. *** buffer overflow detected ***: /usr/bin/perl terminated Aborted stan@linux-pwfe:~>
Perl ARRAY() result
7 direct replies — Read more / Contribute
by pvfki
on Jan 13, 2019 at 16:26


    I am sort of a beginner to Perl here and I am having problems converting ARRAY() output to string. The code below is a program which computes All subsets (of a set) with a given sum. The only issue is it prints the results in ARRAY() form instead of strings. Does anyone know how to convert ARRAY() --> string?

    sub Solve { my ($goal, $elements) = @_; # For extra speed, you can remove this next line my (@results, $RecursiveSolve, $nextValue); $RecursiveSolve = sub { my ($currentGoal, $included, $index) = @_; for ( ; $index < @$elements; ++$index) { $nextValue = $elements->[$index]; # Since elements are sorted, there's no point in trying a # non-final element unless it's less than goal/2: if ($currentGoal > 2 * $nextValue) { $RecursiveSolve->($currentGoal - $nextValue, [ @$included, $nextValue ], $index + 1); } else { print "@$_\n", [ @$included, $nextValue ] if $currentGoal == $nextValue; return if $nextValue >= $currentGoal; } } # end for }; # end $RecursiveSolve $RecursiveSolve->($goal, [], 0); undef $RecursiveSolve; # Avoid memory leak from circular reference return @results; } # end Solve my @results = Solve(869, [15, 43, 51, 56, 60, 67, 122, 152, 193, 204, +229, 271, 293, 301] );

    The code prints:

    ARRAY(0x2829430) ARRAY(0x2829148) ARRAY(0x28291c0) ARRAY(0x28291a8) ARRAY(0x2829418) ARRAY(0x2829190) ARRAY(0x2828c68) ARRAY(0x2828c50) ARRAY(0x28291c0) ARRAY(0x28291a8) ARRAY(0x2828e78) ARRAY(0x2828bf0) ARRAY(0x2828e60) ARRAY(0x2828bf0)

    which is not understandable. Thanks for help!

Comparing each line of a file to itself
3 direct replies — Read more / Contribute
by Manju Moorthy
on Jan 13, 2019 at 07:42

    I have a file with each line having dna sequences. I want to know how to compare each line of the file with the entire file, to get the number of times each line in the file has been repeated, using PERL?

    each line of the file will contain one dna sequence per line

Autodetect Proxy on Windows
2 direct replies — Read more / Contribute
by Takamoto
on Jan 11, 2019 at 07:12


    I need to run scripts through a proxy on Windows machines. I usually do this:

    use strict; use warnings; use LWP::UserAgent; use HTTP::Request; print "Connecting...\n"; my $ua = LWP::UserAgent->new( ); $ua->proxy(['http', 'https'], ''); my $url=""; my $req = HTTP::Request->new(GET => $url); my $response = $ua->request($req); if ($response->is_success) { print "OK CONNECTION!\n"; } else{ print "NO CONNECTION!\n"; }

    This works fine. However, I would like to autodetect the proxy. The only thing I found so far is LWP::UserAgent::ProxyAny;. Changing the script to:

    use strict; use warnings; use LWP::UserAgent::ProxyAny; use HTTP::Request; print "Connecting...\n"; my $ua = LWP::UserAgent::ProxyAny->new; $ua->env_proxy; my $url=""; my $req = HTTP::Request->new(GET => $url); my $response = $ua->request($req); if ($response->is_success) { print "OK CONNECTION!\n"; } else{ print "NO CONNECTION!\n"; }

    I go through the proxy. If I look inside $ua I found my proxy. HOWEVER, the module is 15 years old, and normally I have lots of doubts in using modules that never get updated. So, before using it in production I would like to have some feedbacks from people using it and know possible problems I could face (and of course I am interested in alternatives, if any). Thank you.

Display text and image on same page
3 direct replies — Read more / Contribute
by bachoA4o
on Jan 11, 2019 at 06:57
    Hello , i have a simple script that displays information in table format , and above the the table i want to display graphic (in .png) format. I think my problem is with the header but could not figure how to do it . Here is part of the script :
    print "Content-type: text/html\n\n "; print "<html> \n"; print "<head> Network </head>\n"; print "<body>\n"; print "<img src=/var/www/grafika/graphHour.png width=400 height=200>\n +"; print "<table style=width:70%>\n"; print "<tr>\n"; print "<th>Interface</th>\n"; . . .
Regex not matching closing newline?
1 direct reply — Read more / Contribute
by rverscho
on Jan 11, 2019 at 04:33

    I cannot figure out the following seemingly trivial regex, so I hope some wisdom can be dispensed...

    I have a 4-char string, consisting of 'abc' plus a closing newline.
    When matching until the end of the string, I get a different result when using '.*$' vs '.*?$': in the first case the closing newline is included, in the second, it is not. Note that /s is being used.
    I am mystified how the end of the string can be interpreted different in these regexes, but apparently it is.
    What am I missing here?
    When the closing char is not a newline, the results are identical, as expected.
    This in in Perl v5.14.4 (no options to use a different version).

    Thanks much in advance for guidance!

    $s = "abc\n"; if ($s =~ /(ab.*?)$/s) { $p = substr($s, @-[1], (@+[1] - @-[1])); print("match A=[$p] length=".length($p)."\n"); } if ($s =~ /(ab.*)$/s) { $q = substr($s, @-[1], (@+[1] - @-[1])); print("match B=[$q] length=".length($q)."\n"); } result: match A=[abc] length=3 match B=[abc ] length=4
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 pondering the Monastery: (4)
As of 2019-01-17 02:18 GMT
Find Nodes?
    Voting Booth?
    After Perl5, I'm mostly interested in:

    Results (300 votes). Check out past polls.