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

Seekers of Perl Wisdom

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

If you have a question on how to do something in Perl, or you need a Perl solution to an actual real-life problem, or you're unsure why something you've tried just isn't working... then this section is the place to ask. Post a new question!

However, you might consider asking in the chatterbox first (if you're a registered user). The response time tends to be quicker, and if it turns out that the problem/solutions are too much for the cb to handle, the kind monks will be sure to direct you here.

User Questions
DBI Adding Null Bytes to String
2 direct replies — Read more / Contribute
by Stringer
on May 25, 2016 at 16:57

    I've run across a strange bit of behavior within DBI. When I set the 'LongTruncOk' parameter to true for a database handle and retrieve a long string it seems to insert null bytes between each character. For example, a string of '600' comes back as (in hex):

    36 00 30 00 30

    This pattern persists throughout the string. This does not occur if LongTruncOk is set to false.

    Is this expected behavior that I'm simply ignorant of? I want it to automatically truncate strings that are too long, but not if my string gets trashed in the process.

    The following code reproduces the problem. I'm working with an MS SQL Server if it matters.

    #!/usr/bin/perl use strict; use warnings; use DBI qw(:utils); my $data; my $host = 'somehost'; my $query = <<'END'; select someColumn from someDb.someSchema.someTable where id = someValue END $data = queryDb( 'host' => $host, 'query' => $query, 'longTruncOk' => 1, ); print $data->[0][0], "\n"; #Output as ASCII $data->[0][0] =~ s/(.)/sprintf("%x ",ord($1))/eg; print $data->[0][0], "\n"; #Output as hex sub queryDb { my %args = @_; my $connStr = 'DBI:ODBC:' . $args{host}; my $dbh = DBI->connect($connStr) or die "Couldn't connect to database: " . DBI->errstr . "\n"; my $table = []; $dbh->{LongReadLen} = defined $args{longReadLen} ? $args{longReadLen +} : 80; $dbh->{LongTruncOk} = defined $args{longTruncOk} ? $args{longTruncOk +} : 0; #Note - this seems to change strings to UTF-16? my $sth = $dbh->prepare($args{query}) or die "Couldn't prepare statement: " . $dbh->errstr . "\n"; $sth->execute( @{ $args{params} } ); while (my @row = $sth->fetchrow_array()) { push @$table, \@row; } return $table; }
Dealing with rectangular images
3 direct replies — Read more / Contribute
by htmanning
on May 25, 2016 at 15:43
    Monks, I have a site that allows clients to upload logos. Using css, we fit the logo into a square container no matter what the size. Anyone with a logo that is square, or slightly rectangular, fits nicely. However, some logos are extremely wide. By that I mean the width far exceeds the height. The database contains the width and height of the pics. I'm trying to detect pics that are extremely wide, and if so, display them above the title instead of next to the title. So I guess I'm looking for feedback as to the best percentage of width to height and how to calculate it?
Regex substitute matches second match first?
4 direct replies — Read more / Contribute
by Linicks
on May 25, 2016 at 13:04

    Hi All,

    Been using Perl for a long time, but tonight something has me beat.

    I am trying to capture certain IP's from mail log lines, and this all worked well until I changed the code a bit to capture a new match. For some reason, this code here:

    #!/usr/bin/perl -w my $line = "May 25 10:44:06 postfix/smtpd[6992]: NOQUEUE: reject: RCPT + from[]: 454 4.7.1 <eax_64@yah>: Relay access denied; from=<> to=<> + proto=ESMTP helo=<>"; if ($line =~ /Relay access denied/) { $line =~ s/.*\]: //; #print $line; $line =~ s/.*\[//; $line =~ s/\].*//; chomp($line); print $line; }

    matches the second ']:' first, and fubars the whole logic.

    I have tried researching this, but got nowhere due to the noise.

    Any ideas what is going on?



    P.S. I fixed it up another way, but I am intrigued.

In Perl, How to pass the File Transfermode in SFTP
3 direct replies — Read more / Contribute
by Yedu
on May 25, 2016 at 12:17
    Hi, While sftp'ing the files, how can we set the transfer mode (ASCII/Binary) of the file? I am sftp'ing the file with out using any modules(Net::SFTP). $sftp_cmd = `sftp $usr\@$address <<EOF \n cd $remote_dir \nbye \nEOF`; Can someone please help on how can I set the transfer mode ? Thanks in Advance!!!
Code coverage of forked code
1 direct reply — Read more / Contribute
by britterm
on May 25, 2016 at 11:46
    Hi monks,

    I have a module which forks several times to set up a few workers. I'm using Test::More and Devel::Cover to do unit testing of my code and generate code coverage. I have unit tests that stress the workers, but the coverage report shows that the forked code didn't execute even though it did.

    I considered putting the child code into separate functions that can be tested independently of forking, but the code relies on piped handles to communicate with the parent, which AFAIK do not function without forking (can't write/read to a pipe in the same process). I would have to do a significant amount of coding in my unit tests to attempt to work around this, and I'm looking for a more straightforward solution if there is one.

    Does anyone know how to configure Devel::Cover to detect coverage in the child process when the program forks? Is this even possible? (perhaps with a different coverage tool?)

    Thanks in advance,
Finding file level lexical variables
1 direct reply — Read more / Contribute
by johndeighan
on May 25, 2016 at 11:35
    Our mod_perl application is sometimes running out of memory. So, I want to create a function to track down all variables outside of functions and check their memory usage using Devel::Size. I'm using Package::Stash to find all package variables (declared using 'our'), but now I want to also find all lexical variables (declared with 'my') which are at the file, i.e. top, level in all packages. I know about PadWalker but don't know how to use it - or anything else - to find and get the memory used by these top level lexical variables.
In Perl, how to check for the remote folder existence using sftp command? Cant use Net:SFTP
3 direct replies — Read more / Contribute
by Yedu
on May 25, 2016 at 10:31


    As part of SFTP process, I need to validate the remote folder before sftp'ing the files. We cannot have the Net::SFTP module installed on our servers.

    I have written the following code to accomplish the same. *

    #!/usr/bin/perl -i my $sftp_cmd = "sftp $usr\@$address <<EOF \n cd $remote_dir \nbye \nEO +F"; my $sftp_log = `$sftp_cmd`; if ($? == 0) { print "SFTP Completed Successfully"; } else { print "SFTP Failed"; }

    * When I pass some wrong directory to $remote_dir, it is showing the information "Couldn't canonicalise: No such file or directory" in the command line. But finally, throwing the message "SFTP Completed Successfully".

    Can some one please help how to handle this?

    Is there any other variable that I should use instead of '$?' to check for the status?.

    Thanks in Advance.


Compare method with Selenium
2 direct replies — Read more / Contribute
by Chaoui05
on May 25, 2016 at 03:28
    Hi every Monks ! I am appealing to your wisdom yet again. I have an issue. Iam using Selenium::Screenshot. I get screenshots i'd like to compare. I did this following code:
    $driver->set_window_size(320, 480); $elem = $driver->find_element('applgs','id' ); my $white = Selenium::Screenshot->new( png => $driver->screenshot, exclude => [{ size => { width => 10, height => 10 }, location => { x => 5, y => 5 }, }] )->save( file => "snapScreenshot" ); $driver->execute_script('document.getElementsByTagName("body")[0]. +style.backgroundColor = "red"'); my $blue = Selenium::Screenshot->new(png => $driver->screenshot)-> +save( file => "snapScreenshot1" ); unless ($white->compare($blue)) { my $diff_file = $white->difference($blue); print 'The images differ; see ' . $diff_file . ' for details'; }
    But i get after this ouput in my Shell:
    not ok 22 - A_test died (Can't locate object method "compare" via pack +age "D:/t/gui-tests/tests/screenshots/snapScreenshot-firefox.png " (perhaps you forgot to load "D:/t/gui-tests/tests/screenshots/snapSc +reenshot-firefox.png"?) at line 260.)
    I have my 2 screenshots and one of them have got effectively its body in red. Somenone can help ? Many Thanks !!
Check website has update file using www::mechanize
2 direct replies — Read more / Contribute
by perlmad
on May 25, 2016 at 01:50

    Hi Monks

    I am using www::mechanize module to download a set of zip files from webpage and I have a small problem that's I need to download only updated file only

    Any Idea Filter out recurring NULL Bytes
4 direct replies — Read more / Contribute
by $h4X4_&#124;=73}{
on May 24, 2016 at 22:50

    Would it be a good idea to filter out recurring(bypassing filter) NULL Bytes from CGI?

    I started playing around with a filter that can do it. But I'm not sure where would be the best place to implement the filter. Should it do all the names and values of the param's or just as it calls them by name and only filter the values of the names?

Add your question
Your question:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • 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 meditating upon the Monastery: (3)
    As of 2016-05-26 01:40 GMT
    Find Nodes?
      Voting Booth?