Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

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
Perl to Oracle Connection Error: ERROR OCINlsEnvironmentVariableGet(OCI_NLS_CHARSET_ID)
5 direct replies — Read more / Contribute
by fattahsafa
on Dec 16, 2014 at 03:21
    I'm trying to connect to a remote oracle database in my perl app. When running the app, the following error appears:  "Connection Error: ERROR OCINlsEnvironmentVariableGet(OCI_NLS_CHARSET_ID)" I got Oracle Instant Client and placed it locally then set the Oracle Home env to it, as I don't have Oracle client installed there. My code is below:
    BEGIN { $ENV{NLS_LANG} = "AMERICAN_AMERICA.UTF8"; $ENV{OCI_NLS_CHARSET_ID} = "AMERICAN_AMERICA.UTF8"; $ENV{ORACLE_HOME} = #The path to the instant Oracle client"; } #Connect to database my $DBHandle = DBI->connect( "dbi:Oraclehost=$host;sid=$sid;port=$port", $userName, $passWord ) or die "Connection Error: $DBI::errstr\n";
    I'm using ActiveStat perl on Windows Server 2008, ORACLE 10g
Code Coverage on perl based web Application
1 direct reply — Read more / Contribute
by bimleshsharma
on Dec 16, 2014 at 00:31
    I have a web application running over apache. it reads .xsp files to invoke page on browser. The sample of xsp file is like this..
    <?xml version="1.0"?> <xsp:page xmlns:xsp="http://apache.org/xsp/core11/w1" > <xsp:logic> use Data::Dumper; </xsp:logic> <test> <xsp:logic> my $u= $r->notes('tester'); die "must be admin to view this page" unless $r->notes('q')->{ +pp}; </xsp:logic> </test> </xsp:page>
    and like this lots of other files are being used in that application. So, i want code coverage of this application. As we know (from above xsp file content) there is perl code and for that i was trying to use Devel::Cover module to get coverage of my web application. If we have single perl file then we can do like..
    perl -MDevel::Cover asc.pl
    and i am getting result of this but if we want to get coverage of a web application(having multiple files) then I am getting stuck that where and how to do that. Any help is much appreciated.
Join multiple lines in a string based on regex.
5 direct replies — Read more / Contribute
by pr33
on Dec 15, 2014 at 22:36
    Hi, I have data in a string with multiple lines opened for reading in a command line file handle . I want to join lines starting with a pattern until it hits the same pattern at the End . Below is my data . I want to combine the lines starting with array in to a single line until it hits the pattern starting with array and then when it finds the next array , It should combine all the lines and so on until End of String . The New O/P should look like this ...
    array A physicaldrive 2C:1:1 port 2C:box 1:bay 1 SAS 1 TB OK physicald +rive 2C:1:2 port 2C:box 1:bay 2 SAS 1 TB OK array B physicaldrive 2C:1:3 port 2C:box 1:bay 3 SAS 1 TB OK physicald +rive 2C:1:4 port 2C:box 1:bay 4 SAS 1 TB OK

    data array A physicaldrive 2C:1:1 port 2C:box 1:bay 1 SAS 1 TB OK physicaldrive 2C:1:2 port 2C:box 1:bay 2 SAS 1 TB OK array B physicaldrive 2C:1:3 port 2C:box 1:bay 3 SAS 1 TB OK physicaldrive 2C:1:4 port 2C:box 1:bay 4 SAS 1 TB OK array C physicaldrive 3C:1:5 port 3C:box 1:bay 5 SAS 1 TB OK physicaldrive 3C:1:6 port 3C:box 1:bay 6 SAS 1 TB OK array D physicaldrive 3C:1:7 port 3C:box 1:bay 7 SAS 1 TB OK physicaldrive 3C:1:8 port 3C:box 1:bay 8 SAS 1 TB OK array E physicaldrive 4C:2:1 port 4C:box 2:bay 1 SAS 1 TB OK physicaldrive 4C:2:2 port 4C:box 2:bay 2 SAS 1 TB OK array F physicaldrive 4C:2:3 port 4C:box 2:bay 3 SAS 1 TB OK physicaldrive 4C:2:4 port 4C:box 2:bay 4 SAS 1 TB OK array G physicaldrive 5C:2:5 port 5C:box 2:bay 5 SAS 1 TB OK physicaldrive 5C:2:6 port 5C:box 2:bay 6 SAS 1 TB OK array H physicaldrive 5C:2:7 port 5C:box 2:bay 7 SAS 1 TB OK physicaldrive 5C:2:8 port 5C:box 2:bay 8 SAS 1 TB OK array I physicaldrive 6C:3:1 port 6C:box 3:bay 1 SAS 1 TB OK physicaldrive 6C:3:2 port 6C:box 3:bay 2 SAS 1 TB OK array J physicaldrive 6C:3:3 port 6C:box 3:bay 3 SAS 1 TB OK physicaldrive 6C:3:4 port 6C:box 3:bay 4 SAS 1 TB OK array K physicaldrive 7C:3:5 port 7C:box 3:bay 5 SAS 1 TB OK physicaldrive 7C:3:6 port 7C:box 3:bay 6 SAS 1 TB OK unassigned physicaldrive 7C:3:7 port 7C:box 3:bay 7 SAS 1 TB OK physicaldrive 7C:3:8 port 7C:box 3:bay 8 SAS 1 TB OK
    The Code that I tried to implement didn't work.
    while (my $row =~ <$fd>) { $row =~ /^$/ and next; $row =~ s/[,|)|(]//g; chomp $row; next if $row =~ /^Smart/g; $row =~ s/^\s+//; my @temp; my $counter; if($row =~ /^array/) { push @temp,$_; next unless ($row =~ /^array/); } ## Aim is to have the resulting O/P Captured in $row .
Search and delete
6 direct replies — Read more / Contribute
by pr33
on Dec 15, 2014 at 18:11
    Hi, I want to delete a line containing a pattern and 2 lines after that pattern as well . Ex : The pattern should match unassigned and delete the line containing unassigned next 2 lines after it . Please help me with this . unassigned physicaldrive 7C:3:7 port 7C:box 3:bay 7 SAS 1 TB OK physicaldrive 7C:3:8 port 7C:box 3:bay 8 SAS 1 TB OK Thanks
Encryption, Decryption & Password Protection
6 direct replies — Read more / Contribute
by Bugz
on Dec 15, 2014 at 17:27
    I have three part problem: 1. I'd like to encrypt a file and store it and decrypt it when I need to use it. How can i do that? 2. I need to password protect access my perl script which does the encryption & decryption so that no one can open it and read what's in without a password 3. Unless the right password it provided no one can execute my perl script. Can you please provide examples of how I can do this?
Perl script run foreach loop and sort without having to save and reopen the filehandle each time.
3 direct replies — Read more / Contribute
by john.tm
on Dec 15, 2014 at 15:54

    I have a file which i run a foreach loop on, then save it. reopen it and remove some dupliacte lines based on certaian fields. how can i run this without the saving and reopening of the file in the middle section each time.

    #!/usr/bin/perl use strict; use warnings; #use diagnostics; my $file = "c:\\tmp.txt"; open( my $fh, "<", $file ) or die $!; my $OUTNET = "c:\\NETtmp.txt"; open( OUTPUT, ">", "$OUTNET" ) or die $!; my @array; foreach (<$fh>) { chomp ; if ( $_ =~ m/^\s+\d/ ) { $_ =~ s/^\s+//g; $_ =~ s/\s+$//g; $_ =~ s/\s+/,/g; push @array, "s_"; print " $_ \n"; printf OUTPUT "$_ \n"; } } close OUTPUT; my $file2 = "c:\\NETtmp.txt"; my $OUTNET2 = "c:\\final.txt"; open my $in, '<', $file2 or die $!; open my $out, '>', $OUTNET2 or die $!; seek $in, 0, 0; my %hash; while (<$in>) { my $key = join ',', ( split /,/ )[ 1, 2, 3, 4 ]; printf $out $_ unless $hash{$key}++; } close $out; close $in;
Crypt::Eksblowfish::Bcrypt doesnt support 2y?
2 direct replies — Read more / Contribute
by dallase
on Dec 15, 2014 at 14:35
    The laravel framework (php) creates user passwords in the following bcrypt format

    $2y$10$(salt)(hash)

    I'm trying to write a program in perl that can validate the laravel password that is stored in the database, but it doesnt appear Crypt::Eksblowfish::Bcrypt supports the '2y' bcrypt format. While '2a' works just fine.

    $ perl bcrypt_2a Plain password is bcrypt as $2a$10$q4VIJI0lTJBh4O6Kfo/f/uwvN4CQWPbFutc +8hO8bKmn3Rz6qV4xcS Valid Password password
    $ perl bcrypt_2y bad bcrypt settings at bcrypt_2y line 22
    The files bcrypt_2a and bcrypt_2y are below in case anyone has any great idea here!
    ===========================
    #!/usr/bin/perl # File: bcrypt_2a use Crypt::Eksblowfish::Bcrypt; use Crypt::Random; $password = 'password'; $encrypted = encrypt_password($password); print "Plain $password is bcrypt as $encrypted\n"; if (check_password($password, $encrypted)) { print "Valid Password $password\n" } sub encrypt_password { my $password = shift; my $salt = shift || salt(); my $settings = '$2a$10$'.$salt; return Crypt::Eksblowfish::Bcrypt::bcrypt($password, $settings); } sub check_password { my ($plain_password, $hashed_password) = @_; # Regex to extract the salt if ($hashed_password =~ m!^\$2a\$10\$([A-Za-z0-9+\\\.\/]{22})!) { # Use a letter by letter match rather than a complete string match + to avoid timing attacks my $match = encrypt_password($plain_password, $1); my $bad = 0; for (my $n=0; $n < length $match; $n++) { $bad++ if substr($match, $n, 1) ne substr($hashed_password, $n, +1); } return $bad == 0; } else { return 0; } } # Return a random salt sub salt { return Crypt::Eksblowfish::Bcrypt::en_base64(Crypt::Random::makerand +om_octet(Length=>16)); }

    ===========================
    #!/usr/bin/perl # File: bcrypt_2y use Crypt::Eksblowfish::Bcrypt; use Crypt::Random; $password = 'password'; $encrypted = encrypt_password($password); # from another program, a bcrypt 2y of 'password' = '$2y$10$iG2fZoSKzW +UVn65cMDGL0uG8sWvy0G0G2Z/1Fll7zcBvEIOvn8qLG'; print "Plain $password is bcrypt as $encrypted\n"; if (check_password($password, $encrypted)) { print "Valid Password $password\n" } sub encrypt_password { my $password = shift; my $salt = shift || salt(); my $settings = '$2y$10$'.$salt; return Crypt::Eksblowfish::Bcrypt::bcrypt($password, $settings); } sub check_password { my ($plain_password, $hashed_password) = @_; # Regex to extract the salt if ($hashed_password =~ m!^\$2y\$10\$([A-Za-z0-9+\\\.\/]{22})!) { # Use a letter by letter match rather than a complete string match + to avoid timing attacks my $match = encrypt_password($plain_password, $1); my $bad = 0; for (my $n=0; $n < length $match; $n++) { $bad++ if substr($match, $n, 1) ne substr($hashed_password, $n, +1); } return $bad == 0; } else { return 0; } } # Return a random salt sub salt { return Crypt::Eksblowfish::Bcrypt::en_base64(Crypt::Random::makerand +om_octet(Length=>16)); }
printing all values in a hash of hashes of arrays
3 direct replies — Read more / Contribute
by estreb
on Dec 15, 2014 at 14:13
    Hello again, Monks. With the following code:
    my %hash_of_hash_of_arrays = ( 'key' => { 'innerkey2' => [ 'here' ], 'innerkey3' => [ 'there' ], 'innerkey1' => [ 'hello', 'world' ] } );
    I am trying to print each value in the hash ('here', 'there', 'hello', and 'world'). I tried the following, but it is giving me a syntax error in the second foreach loop:
    foreach my $outerkey (keys %hash_of_hash_of_arrays) { print "'$outerkey'"; foreach my $innerkey (keys %hash_of_hash_of_arrays{$outerkey}) { print "\t'$hash_of_hash_of_arrays{$outerkey}{$innerkey}'"; foreach my $array_item (@{ $hash_of_hash_of_arrays{$outerkey}{$inn +erkey} }) { print "array item: $array_item"; } } }
    The second foreach loop is incorrect. I have no idea if the third is even in the right ballpark. How do I fix this? Thanks in advance.
Creating a hash of hashes of arrays
4 direct replies — Read more / Contribute
by estreb
on Dec 15, 2014 at 11:52
    Hi Monks. I am a very new to hashes in Perl. In the following code, I am creating a hash of hashes of arrays.
    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; my @array1 = ( "hello", "world" ); my @array2 = ( "here" ); my @array3 = ( "there" ); my %inner_hash = ( innerkey1 => \@array1, innerkey2 => \@array2, innerkey3 => \@array3 ); my %hash_of_hash_of_arrays = ( key => \%inner_hash ); print Dumper(\%hash_of_hash_of_arrays); exit(0);
    This gives the output:
    $VAR1 = { 'key' => { 'innerkey2' => [ 'here' ], 'innerkey3' => [ 'there' ], 'innerkey1' => [ 'hello', 'world' ] } };
    Is there any way I can get this same output by defining only one variable? Something like:
    my %hash_of_hash_of_arrays = { 'key' => { 'innerkey2' => [ 'here' ], 'innerkey3' => [ 'there' ], 'innerkey1' => [ 'hello', 'world' ] } };
    ...but that gives an error about "Reference found where even-sized list expected", and the output looks like:
    $VAR1 = { 'HASH(0x57e1d78)' => undef };
    which obviously is not what I want at all. Please help.
Net::OpenSSH memory allocation error with seemingly low memory code
1 direct reply — Read more / Contribute
by DanBev
on Dec 15, 2014 at 10:52

    Hello Monks, I appeal to your wisdom for an advice.

    I have an "unable to fork ssh master: can not allocate memory" error when using Net :: OpenSSH-> new.
    I know that this kind of error is spy for unoptimized code, but unfortunately I have a code of 9000 lines that I can't for obvious reasons post (and I know that's very blamable). I am also aware that with a code so long the chance to have bug in the code is virtually 1.

    The only thing I ask is: is it possible to have a "can not allocate memory" if checking the program execution with "top" I see that the memory allocation is practically negligible? How can it be?

    Thank you all.

Add your question
Title:
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!
  • 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
  • Outside of code tags, you may need to use entities for some characters:
            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?
    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 exploiting the Monastery: (8)
    As of 2014-12-19 04:03 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (70 votes), past polls