Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
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
Redefined import method and EXPORT not working
1 direct reply — Read more / Contribute
by Anonymous Monk
on Dec 22, 2014 at 01:13

    I redefined the import method of a module

    package M1; use parent qw(Exporter); our @EXPORT; sub func{print "Hello"} sub import{print "Imported";@EXPORT=qw(func)} 1;

    And EXPORT is not working

    perl -I. -le 'use M1;func()' Imported Undefined subroutine &main::func called at -e line 1.

    But when I don't redefine import function, EXPORT works fine

    package M1; use parent qw(Exporter); our @EXPORT; sub func{print "Hello"} #sub import{print "Imported";@EXPORT=qw(func)} 1;
    perl -I. -le 'use M1;func()' Hello
    How can I get both redefining import and EXPORT to work?
Deleting records from an array
2 direct replies — Read more / Contribute
by viffer
on Dec 22, 2014 at 00:34
    Morning all

    I know this isn't something that is normally done, but I seriously can't think of any other way to process the file I'm looking at

    I have a hash containing a number of keys, and an array that contains a number of records.

    For each hash key I'm processing all the records in the array, and printing off the records in the array that have a field that matches the hash key.
    The problem I have, is that for each hash key I process, I'm reading the entire array EVERY time. Once I've printed the records in the array I want to delete them so they they don't get processed every single time.

    For 40,000 records the process is taking in excess of 3 hours.

    I think I can delete the record from the array if I know what the index of the record is, but I'm not sure how to work that out

    for my $key (keys %coll_key_hash) { foreach my $rec (@recs_read) { my @fields = split(/\|/, $rec); if ($fields[0] eq '010') { if ($fields[3] =~ $key) { $print_record = 1; if ($key ne $prev_key) { $prev_key = $key; print_record ("CUST-BEG|$seq\n",' '); } print_record ("$rec\n",'c'); } else { $print_record = 0; } } else { if ($print_record) { print_record ("$rec\n",'c'); } } } }
    What I'm trying to do is to find a way to delete the record from the array when I've processed it, so that I'm not processing a record I've already processed once before.

    Any suggestions gratefully received. Thanks - and Merry Xmas to all

comma(or fat comma) in 'use' directive
3 direct replies — Read more / Contribute
by Anonymous Monk
on Dec 21, 2014 at 23:20

    Like this sentence:

    use Test::Simple tests => 1;

    Sometimes I see there is a fat comma on use directive Of course I would think they are functions if it looked like:

    use Foo::Bar qw(func1 func2);

    But in the first case, '1' cannot be a function name. So what is that '1'? Is it a parameter to the function 'tests' or a parameter to the module Test::Simple or what???

Need help !
1 direct reply — Read more / Contribute
by lonewolf28
on Dec 21, 2014 at 19:19

    Hi I have a piece of code, when a no is passed twice it will just print out saying the no is called twice. But unfortunately it's not working as intended. Any thoughts ? Thanks

    { my $last = 5; sub do_stuff { my $arg = @_; if ( $arg == $last ) { print "You called me twice in a row with $arg\n"; } $last = $arg; } } do_stuff($_) for 0..5;
Help inserting Kanji in MSSQL DB
2 direct replies — Read more / Contribute
by michaeldhenry
on Dec 21, 2014 at 11:12

    Hi Everyone,

    I am hoping someone can help me out here. I am having a problem getting some text into a MSSQL DB correctly using the following CGI script (i tried to trim it down to just the essentials).

    use CGI; use DBI; my $test = 'testこんにちはtest'; print "Content-type: text/plain; charset=utf8\n\n"; my $dbproc = DBI->connect("DBI:ODBC:*********","*******", "*****", + { PrintError => 0, RaiseError => 1, ChopBlanks => 1 } ) || print "un +able to insert record $DBI::errstr"; my $insQry = "exec ins_result N'".$test;."'"; print "insQry: $insQry\n\n"; my $sth = $dbproc->prepare($insQry); $sth->execute; $dbproc->disconnect || print "unable to insert record $DBI::errstr +";
    I see the correct data on the web page as follows: insQry: exec ins_result N'testこんにちはtest'

    but when i check the DB i see the following was inserted into the table:

    mikeSkaotest

    Environment details are:

    OS: Red Hat Enterprise Linux Client release 5.9 (Tikanga)

    DB Microsoft SQL Server 2005

    Apache: Apache/2.0.50

    Perl: 5.10.0

    Any help would be greatly appreciated.

    Thanks,

    Mike

Seeking regexp @ARGV arrays Wisdom
4 direct replies — Read more / Contribute
by Anonymous Monk
on Dec 21, 2014 at 10:24

    Thank you in advance. I am writing a short program, a game, that tests your knowledge of GNU/Linux commands and programs (with a whatis entry). I want it to put like flash cards: present the name of the command, wait for the user, then print the description, wait for the user, ad infinitum. The code below shows the first attempt (SLATHERED WITH COMMENTS):

    #!/usr/bin/perl #bashflash #tests your memory of bash/gnu-linux commands and cli programs #USAGE: #cd to the directory containing the binarys of the programs #you want to test your knowledge of (DO NOT NEED TO BE ROOT) #pass perl bashflash -`whatis --wildcard *` # use strict; use warnings; #The following loop utilizes a shift-and-push loop #to shift the zeroth element of @ARGV out of the array, into a scalar, #then push the scalar back into the array as the final element. #effectively, this process lets one cycle through the output of #`whatis --wildcard *` indefinitely, *while separating the #command names from their descriptions, like flashcards.* #*In it's current implementation, @ARGV stores the output of `whatis` #word by word: such that elements are delineated by spaces. my $currentcard; #this variable holds onto strings shifted off of @ARG +V #, prints them, then pushes it's contents #back onto the end of ARGV while (1) { #infinite loop START $currentcard = shift; #shift first element #of @ARGV into $currentcard print $currentcard; push (@ARGV, $currentcard); #push $currentcard #(previously element 0) back onto #@ARGV as the last element <STDIN>; #Wait for user } #infinite loop END exit; #the script never gets here, #but I always explicitly exit on the end of my Perl. #and then an empty comment line #

    This program puts this to STDOUT:

    Note, each line after "-bash" had to be summoned by the return key, meaning that (as explained in the comments) @ARGV has stored the command line argument into new elements by word. (This is my current understanding, please correct me.)

    #1, the negative testing while

    #2, the while controlled by internal positive testing if

    Above: an attempt, two of many, to get the "flash card" output I desire. My plan is to instead push the shifted elements of @ARGV onto an Array in a regexp-conditional loop, to attempt to match the "- " in the last element of Array, if it does match "- ", then the code should exit the loop and print everything it collected up to and including the "- ", then pushing it's content back onto the end of @ARGV, and waiting for the return key press. Or the logical equivalent, test positive for iteration with the lack of "- ", and keep adding to Array until shift adds a "- ". As above.

    I have tried dozens of variations with positive and negative regular expressions, I even tried /.*/, but every time I try to group the output such that iterations of print are delimited by "- ", just I get blank lines.

    Am I using push and shift incorrectly?

    Prithee, great archons of perl, behelpeth me.

Attempt to embed Perl in C on Windows
2 direct replies — Read more / Contribute
by Anonymous Monk
on Dec 21, 2014 at 09:02

    Ok, this is a cross-posting from StackOverflow http://stackoverflow.com/questions/27058788/attempt-to-embed-perl-in-c-on-windows . I posted it there first because I thought it is more C than Perl question. However I did not get any answer and so I'd like to try it again.

    I am trying to run the following example from http://perl-node-interface.blogspot.de/2011/03/deploy-perl-application-on-windows.html (is not accepted as link) with a simple "Hello.pl" (just prints "Hello" to STDOUT).

    It fails. The .exe file is created but does not produce any output.

    Probably this is my basic misunderstanding. Could you please point me in the right direction? Btw. the "lib folder containing all dependencies" in the project folder is empty since there are no modules in the "hello.pl". Is this a correct assumption?

    Thank you very much!

    The hello.c file:

    #include <EXTERN.h> #include <perl.h> EXTERN_C void xs_init (pTHX); EXTERN_C void boot_DynaLoader (pTHX_ CV* cv); EXTERN_C void boot_Win32CORE (pTHX_ CV* cv); EXTERN_C void xs_init(pTHX) { char *file = __FILE__; dXSUB_SYS; /* DynaLoader is a special case */ newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file); newXS("Win32CORE::bootstrap", boot_Win32CORE, file); } static PerlInterpreter *my_perl; /*** The Perl interpreter ***/ int main(int argc, char **argv, char **env) { argv[1] = "-Ilib"; argv[2] = "hello.pl"; PERL_SYS_INIT3(&argc,&argv,&env); my_perl = perl_alloc(); perl_construct(my_perl); PL_exit_flags |= PERL_EXIT_DESTRUCT_END; perl_parse(my_perl, NULL, argc, argv, (char **)NULL); perl_run(my_perl); perl_destruct(my_perl); perl_free(my_perl); PERL_SYS_TERM(); }

    The perl file to build the compiler command:

    #!/perl use strict; use warnings FATAL => qw(all); use ExtUtils::Embed; print "\nBuilding Hello\n"; my $gcc_cmd = join( ' ' , 'C:\Perl_516_portable\c\bin\gcc -Wall -mwind +ows -o K:\Scripts\Embed\Hello_3\hello K:\Scripts\Embed\Hello_3\hello. +c', &ccopts, &ldopts ); print STDOUT $gcc_cmd , "\n"; system( $gcc_cmd );

    The output:

    ---------------------------------------------- Perl executable: C:\Perl_516_portable\perl\bin\perl.exe Perl version : 5.16.3 / MSWin32-x86-multi-thread C:\Perl_516_portable>perl K:\Scripts\Embed\Hello_3\building_3.pl Building Hello C:\Perl_516_portable\c\bin\gcc -Wall -mwindows -o K:\Scripts\Embed\He +llo_3\hello K:\Scripts\Embed\Hello_3\hello.c -s -O2 -DWIN32 -DPERL +_TEXTMODE_SCRIPTS -DPERL_IMPLICIT_CONTEXT -DPERL_IMPLICIT_SYS -fno-st +rict-aliasing -mms-bitfields -I"C:\Perl_516_portable\perl\lib\CORE" + -s -L"C:\Perl_516_portable\perl\lib\CORE" -L"C:\Perl_516_portable\ +c\lib" C:\Perl_516_portable\perl\lib\CORE\libperl516.a C:\Perl_516_p +ortable\c\i686-w64-mingw32\lib\libmoldname.a C:Perl_516_portable\c\i6 +86-w64-mingw32\lib\libkernel32.a C:\Perl_516_portable\c\i686-w64-ming +w32\lib\libuser32.a C:\Perl_516_portable\c\i686-w64-mingw32\lib\libgd +i32.a C:\Perl_516_portable\c\i686-w64-mingw32\lib\libwinspool.a C:\Pe +rl_516_portable\c\i686-w64-mingw32\lib\libcomdlg32.a C:\Perl_516_port +able\c\i686-w64-mingw32\lib\libadvapi32.a C:\Perl_516_portable\c\i686 +-w64-mingw32\lib\libshell32.a C:\Perl_516_portable\c\i686-w64-mingw32 +\lib\libole32.a C:\Perl_516_portable\c\i686-w64-mingw32\lib\liboleaut +32.a C:\Perl_516_portable\c\i686-w64-mingw32\lib\libnetapi32.a C:\Per +l_516_portable\c\i686-w64-mingw32\lib\libuuid.a C:\Perl_516_portable\ +c\i686-w64-mingw32\lib\libws2_32.a C:Perl_516_portable\c\i686-w64-min +gw32\lib\libmpr.a C:\Perl_516_portable\c\i686-w64-mingw32\lib\libwinm +m.a C:\Perl_516_portable\c\i686-w64-mingw32\lib\libversion.a C:\Perl_ +516_portable\c\i686-w64-mingw32\lib\libodbc32.a C:\Perl_516_portable\ +c\i686-w64-mingw32\lib\libodbccp32.a C:\Perl_516_portable\c\i686-w64- +mingw32\lib\libcomctl32.a In file included from C:\Perl_516_portable\perl\lib\CORE/sys/socket.h: +180:0, from C:\Perl_516_portable\perl\lib\CORE/win32.h:356, from C:\Perl_516_portable\perl\lib\CORE/win32thread.h:4, from C:\Perl_516_portable\perl\lib\CORE/perl.h:2834, from K:\Scripts\Embed\Hello_3\hello.c:2: C:\Perl_516_portable\perl\lib\CORE/win32.h:361:26: warning: "/*" withi +n comment [-Wcomment] C:\Perl_516_portable\perl\lib\CORE/win32.h:362:33: warning: "/*" withi +n comment [-Wcomment] In file included from C:\Perl_516_portable\perl\lib\CORE/win32thread.h +:4:0, from C:\Perl_516_portable\perl\lib\CORE/perl.h:2834, from K:\Scripts\Embed\Hello_3\hello.c:2: C:\Perl_516_portable\perl\lib\CORE/win32.h:361:26: warning: "/*" withi +n comment [-Wcomment] C:\Perl_516_portable\perl\lib\CORE/win32.h:362:33: warning: "/*" withi +n comment [-Wcomment] K:\Scripts\Embed\Hello_3\hello.c: In function 'main': K:\Scripts\Embed\Hello_3\hello.c:37:1: warning: control reaches end of + non-void function [-Wreturn-type]
CGI on OpenBSD 5.6 httpd
3 direct replies — Read more / Contribute
by slamet
on Dec 21, 2014 at 06:08
    I cannot find any documentation on how to get CGI going on OpenBSD 5.6 httpd. (Starting with 5.6, OpenBSD ships its own http daemon.) And I am a newbie with CGI. I have something like the following: hello
    !#/usr/bin/perl print "Content type: text/html"; print "hello world";
    I have done "chmod 755 hello", but when I call the address
    curl localhost/hello
    I got the whole file content, instead of just "hello world". I have poured over manpages for httpd and httpd.conf, but still cannot fix this. Many thanks for your help.
Why Makefile.PL uses uppercase extension?
2 direct replies — Read more / Contribute
by Anonymous Monk
on Dec 21, 2014 at 04:44

    I wonder why Makefile.PL uses uppercase extention 'PL' instead of lowercase 'pl'. Is there any reason to do this?

closures: anonymous subs vs function templates?
5 direct replies — Read more / Contribute
by 5haun
on Dec 21, 2014 at 02:51

    I was reading through perlsub and perlref again on metacpan and it occurred to me creating a closure using function templates is a lot like using an anonymous subroutine, only the syntax is a little different (using local *name = sub {}; name('arg'); versus my $name = sub {}; $name->('arg');).

    Is this true or am I miss understanding something? What about compile time vs runtime?

    Thanks,
    -Shaun

    use warnings; use v5.14; my $y = 2014; sub outer1 { my $x = $_[0] + 35; local *inner = sub { my $a = shift; local *__ANON__ = ( caller( 1 ) )[3] . "->inner"; say( ( caller( 0 ) )[3] ); return $y - $x * 19 + $a; }; return $x + inner( $_[0] / 2 ); } sub outer2 { my $x = $_[0] + 35; my $inner = sub { my $a = shift; local *__ANON__ = ( caller( 1 ) )[3] . "->inner"; say( ( caller( 0 ) )[3] ); return $y - $x * 19 + $a; }; return $x + $inner->( $_[0] / 2 ); } say outer1( 8 ); say outer2( 8 ); __END__ $ perl subtest.pl main::outer1->inner 1244 main::outer2->inner 1244

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 scrutinizing the Monastery: (9)
    As of 2014-12-22 12:46 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

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





      Results (116 votes), past polls