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

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
Accessing module variables
5 direct replies — Read more / Contribute
by talexb
on Apr 19, 2018 at 11:04

    I want to cycle through a bunch of modules that have credentials and site URLs .. I have this working OK, but it seems that I'm doing it in a much complicated way than I should. Feedback would be appreciated.

    This script illustrates how I plan to cycle through the credential modules:

    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; { my @variables = qw/User Password Url Prefix/; foreach my $module ( qw/Foo Bar/ ) { eval "use ABCXYZ::$module"; # Foreach loop for illustration only .. could be replaced # with a map .. my %values; foreach my $var ( @variables ) { $values{ $var } = eval '$' . join ( '::', 'ABCXYZ', $module, $var ); } # Call subroutine with values hash here .. } }

    The goal is to be able to call a routine with individual site credentials for order processing. Is there a more Perl-ish way of accomplishing this? Credential modules look like this:

    package ABCXYZ::Foo; use Exporter; our @Export = ( $User $Password $Url $Prefix ); our $User = 'Joe'; our $Password = 'Secret123'; our $Url = 'www.example.com'; our $Prefix = 'EX'; 1;
    The credentials are stored like this so that the processing scripts can be checked into github without credentials leaving the customer site.

    Alex / talexb / Toronto

    Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.

what difference between eval and do ?
2 direct replies — Read more / Contribute
by Anonymous Monk
on Apr 18, 2018 at 23:14
    I thought do BLOCK and eval BLOCK were the same except the last one traps error. But I'm wrong!
    my @dd = (1..10); grep { s/\d+/a/ } do {@dd}; print @dd; # aaaaaaaaa +a my @dd = (1..10); grep { s/\d+/a/ } eval{@dd}; print @dd; # 123456789 +10
    seems 'eval' make a copy but 'do' not, any comment?
Win32::CreateProcess problem
2 direct replies — Read more / Contribute
by mdamazon
on Apr 18, 2018 at 22:32
    I can execute Win32::CreateProcess in a loop succesfully. Now I want to prevent my script from moving forward until all the child processes that were created in the loop have exited. This is causing problems. The code inside the first foreach loop below executes properly. The code below inside the while loop which attempts to detect the status of the child processes is not.

    use Win32::Process qw(CREATE_NEW_CONSOLE STILL_ACTIVE); use Win32; my %procs=(); foreach my $a (@array) { my $cmd01 = 'some command ' . $a . ' other stuff'; my $cmd = "cmd /c echo Performing job. Please wait... +& $cmd01"; my $ProcessObj; Win32::Process::Create($ProcessObj,"C:\\WINDOWS\\syste +m32\\cmd.exe","$cmd01",0,CREATE_NEW_CONSOLE,"."); $procs{$ProcessObj} = $a; } while (%procs) { foreach my $key (keys %procs) { my $exit_code = 1; $key->GetExitCode($exit_code); while($exit_code == Win32::Process::ST +ILL_ACTIVE()) { sleep(1); $key->GetExitCode($exi +t_code); } print "GetExitCode returned $exit_code +, exiting main process for $procs{$key}.\n"; delete $procs{$key}; } }

    This returns:

    Can't locate object method "GetExitCode" via package
    "Win32::Process=SCALAR(0x6e604a4)" (perhaps you forgot to load "Win32::Process=SCALAR(0x6e604a4)"?) at ..\code\script.pl line 1498, <STDIN> line 1 (#1)
    Uncaught exception from user code:
    Can't locate object method "GetExitCode" via package "Win32::Process=SCALAR(0x6e604a4)" (perhaps you forgot to load "Win32::Process=SCALAR(0x6e604a4)"?) at ..\code\script.pl line 1498, <STDIN> line 1.

shebang anomaly
2 direct replies — Read more / Contribute
by perlboy_emeritus
on Apr 18, 2018 at 17:09
    Hello Monks,

    I run Perl natively in macOS (v5.18.2) and in perlbrew (v5.20.3) and in debian in a VirtualBox vm (v5.24.1). The Perl code is common by design, meant to be portable and shared by all of these separate Perl environments, so my shebang line is:

    #!/usr/bin/env perl -w or #!/usr/bin/env perl -wd

    depending on whether I wish to debug. This shebang works correctly in macOS, either the native perl or the perlbrew perl but in debian env does not like either the -w or the -wd arguments. This, despite the (relatively) common man pages for env that assert that the command handed to env can take arguments, as in:

    macOS:

    env [-iv] [-P altpath] [-S string] [-u name] [name=value ...] [utility [argument ...]]

    debian:

    env [OPTION]... [-] [NAME=VALUE]... [COMMAND [ARG]...]

    When I run perl scripts shared from macOS in debian I have to remove -w or -wd, otherwise I get:

    /usr/bin/env: 'perl -w': No such file or directory

    The single quotes in that error message are from bash, not my script. I have tried to quote -w and -wd but explicitly quoting those arguments does not change the outcome. The bash shell or env or whatever are treating -w and -wd as the names of files and failing to find either, barf that error message. It is important that I be able to share Perl scripts between all of these execution environments so the shebang must be the same in all. What is debian env doing that the native macOS env is not? Is this possibly a bug in GNU env (v8.26)?

    I suppose, technically, this is not a Perl question, so perhaps I should post this to stack overflow or some other technical site, but I thought I'd try Monks first.

How to read a file containing filenames and pass as input to perl script?
1 direct reply — Read more / Contribute
by Kal87
on Apr 18, 2018 at 15:53
    Hi there, I'm using the below perl script to process an XML file where the filename is passed as an argument during runtime (Argv[0]) to the script. This works well, however, I would like to take this up a notch. I have another file (let's call it filename_list.txt) that contains a list of filenames (with absolute paths). Say the first two lines in that file read like this:
    /home/kal/assemble.xml /home/tom/Venice.xml
    I expect thousands of lines in filename_list.txt I'm looking to update the script so that it reads each line of filename_list.txt, and executes the twig and CSV bit for each file. Not sure how to get this going, could someone please help? I've attempted to use another bash script with a while, but it would be great if someone could suggest a faster method that involves just this perl script.
    use Text::CSV_XS; use XML::Twig; my $csv = Text::CSV_XS->new({'sep_char' => "|",}, ); my $twig = XML::Twig->new( ); twig_handlers => {'EDI_DC40' => \&process_EDI_DC40,}, ); $twig->parsefile( $ARGV[0] ); sub process_EDI_DC40 { my( $twig, $thingy ) = @_; my @values = map { $thingy->first_child( $_ )->text } qw(DOCNUM MESTYP SNDPRN RCVPOR RCVPRN); $csv->say( *STDOUT, \@values ); + }
Including template in template from other source than .tt file
4 direct replies — Read more / Contribute
by DreamT
on Apr 18, 2018 at 09:43
    Hi,
    Usually, when using a TT file in another TT file, the syntax could look as follows:
    [% PROCESS 'my_included_file.tt' %]

    But, let's say that the included file isn't a physical file, but is stored in a variable:
    [% SET template = fetch_template('my_parameter') %]

    Is it possible to PROCESS that variable and get the same result as if this was a physical file?
    [% PROCESS template???? %]
Binding within a sub
3 direct replies — Read more / Contribute
by jsteng
on Apr 17, 2018 at 09:54
    I have this loop. As it adds labels into a Pane, it binds <1> and <Double-1> to perform specific tasks.
    my @items; for my $text ( qw( one two three four five six seven eight nine ten ) +) { my $label = $pane->Label( -text => $text, -anchor => 'w', -fg => '#000000', -bg => '#C0C0C0', -font => 'courier 20 normal', )->pack(-fill => 'x', -expand => 1); push @items, $label; $label->bind('<Button-1>' => sub { $_->configure( -bg => '#C0C0C0') for @items; #unhighlight e +verything $selectedtext = $text; $label->configure( -bg=>'#CCE8FF'); #highlight this } ); $label->bind('<Double-1>' => sub { if (${$label->cget("-font")} eq 'courier 20 normal') { $label->configure( -font => 'courier 20 bold', -fg +=>'#0000FF'); } else { $label->configure( -font => 'courier 20 normal', - +fg=>"black"); } } ); }
    My problem is that, I want to put that whole loop into a function because it will be performed by different parts of the script.
    my @items; InitializeList(); sub InitializeList { undef @items; for my $text ( qw( one two three four five six seven eight nine te +n ) ) { ... } }
    But doing so, those Bindings dont work anymore.


    Moreso, I also want to isolate the bindings into separate subroutine on their own as well to compartmentalize everything for ease of maintenance:
    sub BindButton1 { } sub BindDouble1 { }
    It appears I am having parameter passing problem.
JSON not loading Cpanel::JSON::XS
2 direct replies — Read more / Contribute
by 1nickt
on Apr 16, 2018 at 11:39

    Hi all,

    My understanding is that using JSON will cause Perl to load JSON::XS or Cpanel::JSON::XS if they are installed. But my testing shows that JSON::PP is used:

    $ perl -MJSON -e 1 $ perl -MJSON::XS -e 1 Can't locate JSON/XS.pm in @INC (you may need to install the JSON::XS +module) [...] BEGIN failed--compilation aborted. $ perl -MCpanel::JSON::XS -e 1 $ perl -MJSON -MData::Dumper -wE 'decode_json(qq{{"foo":"bar"}}); say +Dumper %INC;' | grep JSON $VAR33 = 'JSON/PP.pm'; $VAR34 = '/Users/ntonkin/perl5/perlbrew/perls/perl-5.26.1/lib/5.26.1/J +SON/PP.pm'; $VAR37 = 'JSON.pm'; $VAR38 = '/Users/ntonkin/.perlbrew/libs/perl-5.26.1@dev/lib/perl5/JSON +.pm';

    Anything I have missed?


    The way forward always starts with a minimal test.
The @_ array and the lexical variables
2 direct replies — Read more / Contribute
by G4143
on Apr 16, 2018 at 06:35
    Perl noob here... I thought I understood the @_ array and its purpose with subroutines but then I found these usages that made run here for answers..
    Readonly::Hash my %hash => (data => $data); const my $min => 1; const my $max => 10; open (my $infile, '<', "datafile"); chomp(my $line = <STDIN>);
    Now I read and experimented and arrived at the simple subroutine below which reproduces the above but it makes me raise a simple question - What kind of Perl voodoo is happening when you pass my $num to a subroutine parameter list?
    sub doIt { $_[0] = $_[1]; } doIt my $num => 4143;
    So what is happening with the @_?
Wanted: Opinions on Logging Frameworks
3 direct replies — Read more / Contribute
by learnedbyerror
on Apr 15, 2018 at 14:08

    Oh Wise Monks, I come yet again to seek your assistance. Being a devotee of perl for several decades, I understand and appreciate perl TIMTOWDI. Different is not wrong, but sometimes it is confusing for those of use who are less enlightened, which I definitely am :(

    My question today is: What are your preferences regarding the logging frameworks available in perl (Log::Log4perl, Log::Any, Log::ger, Log::Dispatch, Log::Tiny, ...) and upon what is your preference based?

    The reason that I bring this question to you, in the words of the Great Monk of Music Jimmy Buffett, is because "indecision may or may not be my problem".

    Over the years, I have:

    • used simple print statements
    • created a few simple logging frameworks of my own
    • learned of Log4Perl and became a devotee for a while
    • grew lazy and used Smart::Comments, not a logging framework really at all
    • ...

    I am about to start on a new endeavor and as I often do when starting something new, question my own point of view and seek answers in the temple of CPAN. Most times, this brings me enlightenment and a clear path forward. I see many of the greatest monks in the history of CPAN having authored different frameworks: Log::Any(merlyn, xdg, preaction), Log::Dispatch(autarch) and Log::ger(perlancar) as examples ) I have read their words as recorded in CPAN (some monks do a better job of documentation their works than others :)). This has helped educate me on the pure existence of these frameworks; however, I now am unclear on the appropriateness of application of these frameworks. Given neither a North Star Monk(s) or a clear functional point of view, I lack understanding why one framework may be a better fit for some cases than others.

    As such, I seek to understand your opinions and the basis for them. I realize that their is no right or wrong answer here. But as I have learned over the years, there are many opinions expressed here from which I have learned!

    Thank you in advance for your patience with me and your responses,

    lbe

iterating through array and saving matches to new array
8 direct replies — Read more / Contribute
by jazzfan
on Apr 15, 2018 at 02:27

    I have contrived a problem to help me learn, iterating through an array 1 ..1000, if an item is > 850, save to new array which is visible outside the foreach block. I often find myself wanting to do similar things in while or if blocks but wonder if I am approaching this in the wrong way. So it's more a question of Perl style in handling such scenarios

    our @newa; my @a = 1..1000; foreach my $elem (@a) { if ($elem > 850) { push @newa, $elem; }; say "newa is: @newa\n"; } say "newa is:", @newa;
pre-preprocess Moose args in constructor
1 direct reply — Read more / Contribute
by Anonymous Monk
on Apr 14, 2018 at 02:21
    Esteemed and hallowed Monks: Trying to force upper case on Moose args taken from command line - eg FX currency codes - e.g.
    $lookup usd eur gbp chf
    Yes - I could put it into the calling code - but how would I get Moose to force it itself - I tried this from Moose docs:
    trigger => sub { $_[0]->code( uc( $_[0]->code ) );
    but I get message that I am unable to to modify read-only value despite Moose docs saying that trigger can modify RO values. What is correct way to force upper case (and thus in general) 'pre-preocess' object constructor arguments? Thank-you.
Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2018-04-20 19:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?