Beefy Boxes and Bandwidth Generously Provided by pair Networks DiBona
There's more than one way to do things
 
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.

Want Mega XP? Prepare to have your hopes dashed, join in on the: poll ideas quest 2013  (Don't worry; you've got plenty of time.)

New Questions
Scalar followed by parenthetical...
3 direct replies — Read more / Contribute
by Anonymous Monk
on May 18, 2013 at 22:15
    Why doesn't the following produce an error??
    #!/usr/bin/perl use warnings; use strict; use Data::Dumper; my @h = ( { A => 1, B => undef }, { A => 2, B => 2 }, { A => 2, B => undef }, ); $_->{B} ||= $_{A} foreach @h; # Bug! print Dumper(\@h);
operation inside reference
3 direct replies — Read more / Contribute
by hegotf
on May 18, 2013 at 09:30

    Hi Monks

    Can anyone explain what does that do? @{$doc->{links} // []}

    I don't understand the // [] part.

    here is the link to entire code: https://gist.github.com/creaktive/4607326

XPath to XML
2 direct replies — Read more / Contribute
by tosaiju
on May 17, 2013 at 09:23
    Dear Perl Monks,

    Are there any modules or functionality to create a XML file from XPATH and Data?

    Lets take we have a list of XPath and Data associated to this, is that possible to construct a well formed xml from this?

    Thanks a lot for the help and guidence.

    Kind Regards,

how does perl handle variables
4 direct replies — Read more / Contribute
by jabirahmed
on May 17, 2013 at 07:27
    @a=qw( 1 2 3 4 a b c g f); $s=0; foreach $i(@a){ $s+= $i; } print $s; $perl a.pl 10
    > in this case it should ideally throw some error but it just considers > a,b,c... as 0 ? why does it do that
Storable alternatives
5 direct replies — Read more / Contribute
by salva
on May 17, 2013 at 07:16
    Are there any good alternatives to Storable?

    I need a serializer that can be instructed to replace some inner objects on the fly just before serializing then using a callback.

    Storable has the STORABLE_freeze and STORABLE_thaw hooks but they are not per-object but per-class and besides that, they are methods of the class, not something that can be controlled by the code calling the serializer.

    I was thinking of extending Storable, but frankly, its code its one of the worst examples of code rot I have ever seen and so, my question.

    update: an example of what I need to do:

    my $data = serialize $tree, sub { my $obj = shift; if (check_some_condition($obj)) { return replace($obj) } return (); # serialize $obj as is };
Try::Tiny catch block with $_ eq ''
3 direct replies — Read more / Contribute
by dd-b
on May 16, 2013 at 17:12

    I'm having trouble with ending up in my catch block without any indication of why. Code blocks like:

    try { $self->_run($self, $row_id, $subject) } catch { $logger->error("run($row_id): $_\n") }; $self->_clear_subject;

    (that $logger is from Log::Log4perl).

    Something happens down in $self->_run() and I end up in the catch block, but with no indication of why.

    So, what are the things that count as "errors" for Try::Tiny? The docs just say "error", which doesn't mean anything terribly precise. The obvious case I know about is die calls. I don't have any die calls with null strings as their arguments. (What I end up with in the catch block is a string that Data::Dumper prints as ''. I know that's not impossible; the error put in $_ may not be "true". But I don't have a die statement with a null string arg, and I wouldn't expect DBIC or any standard module to have a die call with a null string arg.)

    I'm using DBIC database stuff in there, and I know it throws errors (in fact that's the point of this whole setup, to catch any errors it throws), but I don't expect it to throw null string errors; does it sometimes?

    I may also be slightly wrong about how far my code got; I've run into at least one case (that I can't reproduce in a simple environment) where it seemed that $logger->trace("String with undefined $variable substituted"); would log nothing ($variable is undef; I'd expect it to log "String with undefined substituted" and give a warning on stderr about the undef, and that's what a simple program that does only that produces). Other trace level calls through the same $logger in the same module are logged. As I say, I can't reproduce this in a simple case, it may be another example of my getting confused.

log4Perl dynamic filename
3 direct replies — Read more / Contribute
by fritz1968
on May 16, 2013 at 08:24

    Hello

    I am trying to setup a logging using log4perl where the filename can be set dynamically. Of course I am having troubles. Here is my current code:

    my $logFile="$logDir/$logfileName"; $logConf = q( log4perl.category.Foo.Bar = INFO, Logfile, Screen log4perl.appender.Logfile = log::Log4perl::Appender::File log4perl.appender.Logfile.filename = logs/frank.txt log4perl.appender.Logfile.layout = Log::Log4perl::Layou +t::PatternLayout log4perl.appender.Logfile.layout.ConversionPattern = %d + %-25M %X{logref} %p %m %n log4perl.appender.Screen = Log::Log4perl::Appende +r::Screen log4perl.appender.Screen.stderr = 0 log4perl.appender.Screen.layout = Log::Log4perl::Layout:: +SimpleLayout ); Log::Log4perl->init(\$logConf);

    The problem is that I don't want each file name to be named "logs/frank.txt". I would rather have something like:

    logs/frank.<date>.txt

    Any help would be greatly appreciated.
    Thanks,
    Frank

Force ASCII regexp for all perls 5.8+
3 direct replies — Read more / Contribute
by vsespb
on May 15, 2013 at 19:57
    I am wondering is there an easier way to force perl treat \w and \d metacharacters as ASCII only, even for Unicode strings with UTF-8 bit set? Needs to work on all perls starting from 5.8.x
    perl -e 'print 3+6 if "\x{424}" =~ /\w/' 9
    Or the only possibility is to use [0-9] instead of \d etc.. ?

    My code works with Unicode, but, at the same time, I wan't to be able to do validation/security checks and often (very often!) I need \d to match only 0-9 digits

    /a /aa and use re '/a' modifier produce syntax errors on early versions of Perl

fail: Linux Perl-DBI to SQL Server
2 direct replies — Read more / Contribute
by madmole
on May 15, 2013 at 16:23

    This is a Database question. I am trying to use Perl-DBI from a Linux system to talk to a SQL Server Database, using the new Microsoft SQL Server driver for Linux. After several hours of effort (spread across a week), I am maddeningly close but just short of success. I've established connectivity to the database (verified with Wireshark) using command line utilities, but for some as-yet undetermined reason Perl-DBI fails to connect (or even access the network).

    Much of what I've accomplished thus far required piecing through a large mass of contradictory and obsolete web pages, so once I get over this hurdle I have the making of a good wiki page for setting up from scratch, which I would be happy to submit for the common good out of gratitude. Details follow:

    Basic Setup

    • Ubuntu 13.04 fresh install on VM (uname -a = Linux rr 3.8.0-19-generic #29-Ubuntu SMP Wed Apr 17 18:16:28 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux)
    • perl 5, version 14, subversion 2 (v5.14.2) built for x86_64-linux-gnu-thread-multi
    • perl -MDBI -e 'DBI->installed_versions;
      Perl : 5.014002 (x86_64-linux-gnu-thread-multi) OS : linux (3.2.0-37-generic) DBI : 1.625 DBD::Sponge : 12.010002 DBD::Proxy : install_driver(Proxy) failed: Can't locate RPC/PlC +lient.pm in @INC DBD::ODBC : 1.43 DBD::Gofer : 0.015326 DBD::File : 0.41 DBD::ExampleP : 12.014310 DBD::DBM : 0.08
    • odbcinst -j
      unixODBC 2.3.0 DRIVERS............: /usr/local/etc/odbcinst.ini SYSTEM DATA SOURCES: /usr/local/etc/odbc.ini FILE DATA SOURCES..: /usr/local/etc/ODBCDataSources USER DATA SOURCES..: /home/madmole/.odbc.ini SQLULEN Size.......: 8 SQLLEN Size........: 8 SQLSETPOSIROW Size.: 8
    • odbcinst -q -s
      [tul1system] [ODBC Driver 11 for SQL Server]
    • cat /usr/local/etc/odbcinst.ini
      [ODBC Driver 11 for SQL Server] Description=Microsoft ODBC Driver 11 for SQL Server Driver=/opt/microsoft/msodbcsql/lib64/libmsodbcsql-11.0.so.2270.0 Threading=1 UsageCount=1 [tul1system] Description=Microsoft ODBC Driver 11 for SQL Server Driver=/opt/microsoft/msodbcsql/lib64/libmsodbcsql-11.0.so.2270.0 Threading=1 UsageCount=1
    • cat ~/.odbc.ini
      [tul1system] Description = Test Driver = /opt/microsoft/msodbcsql/lib64/libmsodbcsql-11.0 +.so.2270.0 Trace = Yes TraceFile = /home/madmole/sql.log Database = Mosaic [ODBC Driver 11 for SQL Server] Description = Test Driver = /opt/microsoft/msodbcsql/lib64/libmsodbcsql-11.0 +.so.2270.0 Trace = Yes TraceFile = /home/madmole/sql.log Database = Mosaic
    • Microsoft SQL Server ODBC driver for Linux v.1.0

    Results

    I have verified I can access the database with both the isql and sqlcmd utilities (double-checked with wireshark):

    • isql -v tul1system user334 2BEvPog
      '+---------------------------------------+ | Connected! | | | | sql-statement | | help [tablename] | | quit | | | +---------------------------------------+ SQL> help [...] SQLRowCount returns 0 888 rows fetched
    • sqlcmd -S tul1system -U user334 -P 2BEvPog
      select * from Award go [...]

    However, when I try a simple Perl-DBI program, I get an error.

    % cat simple.pl #!/usr/bin/perl use DBI; my $dsn="dbi:ODBC:driver={ODBC Driver 11 for SQL Server};server=tul1sy +stem;database=Mosaic"; # SOLVED: keyword "DRIVER=" needs to be all ca +ps my $dbh = DBI->connect($dsn, "user334", "2BEvPog"); if (! defined($dbh) ) { print "***Error connecting to DSN\n"; print "***Error was:\n"; print "***$DBI::errstr\n"; # $DBI::errstr is the error } % simple.pl DBI connect('driver={ODBC Driver 11 for SQL Server};server=tul1cspeldb +02;database=Mosaic','user334',...) failed: [unixODBC][Driver Manager] +Data source name not found, and no default driver specified (SQL-IM00 +2) at ./simple.pl line 6 ***Error connecting to DSN ***Error was: ***[unixODBC][Driver Manager]Data source name not found, and no defaul +t driver specified (SQL-IM002)

    I have tried many variants of connection string, including the following (with various case variations):

    my $dsn="dbi:ODBC:driver={tul1system};server=tul1system;database=Mosai +c"; my $dsn="dbi:ODBC:driver={tul1system};server=tul1system;database=Mosai +c;user=user334;pwd=2BEvPog"; my $dsn="dbi:ODBC:driver={tul1system};server=tul1system;database=Mosai +c"; my $dsn="dbi:ODBC:driver={tul1system};server=tul1system;database=Mosai +c;user=user334;pwd=2BEvPog"; my $dsn="dbi:ODBC:driver=tul1system;server=tul1system;database=Mosaic" +; my $dsn="dbi:ODBC:driver=tul1system;server=tul1system;database=Mosaic; +user=user334;pwd=2BEvPog"; my $dsn="dbi:ODBC:driver={ODBC Driver 11 for SQL Server};server=tul1sy +stem;database=Mosaic;user=iser334;pwd=2BEvPog";

    In all cases, not only does the dbi_connect() fail, but Wireshark confirms there is no traffic on the wire. This is clearly something fundamental, but I've run out of ideas. How does one debug a failed DBI connect()?

Compare zone files in directory with what is listed in named.conf
4 direct replies — Read more / Contribute
by brianjb
on May 15, 2013 at 15:11

    I would really appreciate any assistance that I can get here. I am fairly new to perl. I am trying to rewrite my shell scripts to perl. Currently I have a shell script (using sed, awk, grep, etc) that gets a list of all of the zone files in a directory and then looks in named.conf for what is expected to be in there. If there is a file in the directory that isn't listed in named.conf, then it emails and lets me know that there is some stale files for me to look at. In the past, I have used Text::Diff module to do a diff. Is there any way to put these into two arrays and do a diff without that module? Looking for feedback on either using a module, or doing it without a module. There is more than one way, right?

    This is sample named.conf:

    ########################################### # BIND 9 name server configuration file ########################################### controls { inet 127.0.0.1 allow { localhost; } keys { "rndc-key"; }; }; zone "test.com" in { type master; file "db.test.com"; notify yes; }; zone "brian.com" in { type master; file "db.brian.com"; notify yes; }; zone "." in { type hint; file "db.cache"; }; zone "0.0.127.in-addr.arpa" in { type master; file "db.127.0.0"; };

    This is listing of zonefiles in directory:

    computer:zonefiles brian$ ls -al ~/zonefiles total 0 drwxr-xr-x 5 brian brian 170 May 15 14:58 . drwxr-xr-x 11 brian brian 374 May 15 14:59 .. -rw-r--r-- 1 brian brian 0 May 15 14:48 db.127.0.0 -rw-r--r-- 1 brian brian 0 May 15 14:48 db.brian.com -rw-r--r-- 1 brian brian 0 May 15 14:48 db.test.com

    This is the script so far:

    #!/usr/bin/perl # Putting all of the zonefiles lised in named.conf into an array my $srce = "named.conf.brian"; my $string1 = "db."; open(my $FH, $srce) or die "Failed to open file $srce ($!)"; my @buf = <$FH>; close($FH); my @lines = grep (/$string1/, @buf); #grepping for db. map {s/"//g; } @lines; #removing quotation marks map {s/;//g; } @lines; #removing semicolon #map {s/^.*file.*db.//g; } @lines; #removing file db. map {s/^.*file //g; } @lines; #removing the word file print @lines; # Putting listing of all of the files in ~/zonefiles directory into a +n array # Assigning variable to directory my $directory = "~/zonefiles/"; opendir(D, "$directory") || die "Can't opendir $directory: $!\n"; my @list = readdir(D); closedir(D); foreach my $f (@list) { print "$f\n"; }

    This is the output when I run it:

    computer:tmp brian$ ./brian.pl db.test.com db.brian.com db.cache db.127.0.0 . .. 9 db.127.0.0 db.brian.com db.test.com

    How would I ignore things that I don't want to check? For example, ignore db.cache in named.conf. I also want to ignore "." and ".." in the zone file directory.

architecture width ?
6 direct replies — Read more / Contribute
by shapavi1
on May 15, 2013 at 14:14

    I want to know if the Perl distribution I use has to match the architecture width of my hardware. If I have a 64 bit cpu, do I need a 64 bit release of Perl? Are there other factors in play?

    Thanks!
xml::writer and xml::LibXML
1 direct reply — Read more / Contribute
by fionbarr
on May 15, 2013 at 11:24
    I am successfully creating XML files with XML::Writer. As an exercise I tried to parse these files with XML::LibXML.
    It chokes on 'parser error : Extra content at the end of the document' +.
    Turns out there's a <CTRL-Z> at the end of the created XML document. Has anyone encountered this? What might the work around be? I tried to remove it with a REGEX with no success.
New Meditations
What is the impact of 5.18.0?
2 direct replies — Read more / Contribute
by Tux
on May 19, 2013 at 06:53

    I use my laptop as test environment. When all (upgrades) pass on production/utility software, I upgrade my workstation. If that shows no problems after a few weeks, I start upgrading other production servers.

    So I installed 5.18.0 on my laptop as default perl, and then tried to install all CPAN modules I ever used in 5.16.x

    There are just a few things that stand out as a reason for FAILure:

    • Test files that use for $foo qw( … ) { which is now deprecated. The module autor should rewrite that to for $foo (qw( … )) {. I filed RT tickets for all I encountered. Lets hope the authors will fix their code.
    • POD failures. As the POD checker is now way stricter than the old version was, it shows more real trouble. And it should! The problem is that many authors ship the pod tests too, so the test suite will fail if Test::Pod and friends are installed, and thus cpan won't install those modules. That means manual work ignoring the pod errors (and when obvious file RT tickets). Authors: unless you keep in sync with reality and release often, please do not include pod tests in you distribution.
    • Loads of warnings when a module uses given/when as those are now marked experimental. I can ignore the warnings when testing, but will they fill up my logs when run in production code? When the module also uses Test::NoWarnings, one cannot install. A module like MooseX::App now won't install, and it causes many other modules that have this as a prerequisite to fail too.
    • XS authors that never updated ppport.h from Devel::PPPort. Easy to fix.
    • Modules that use Module::Install but ship ancient or incomplete versions of it in inc. The easiest solution for me was to just recursively remove inc/ and all problems vanish. I have no tuits to RT all of them.
    • Several modules fail because they declare optional deps that are actually hard deps, like using JSON::XS, Lexical::Sub or Data::Alias - which won't build under 5.18.0
    • Test failures, or maybe even code failures, due to hash randomization. Unless I am absolutely sure that the test is wrong, I cannot install these modules.
    • Some failures have been reported to the authors but the available fix has not (yet) been applied (like in Template::Toolkit RT#84778) or not yet released (Tk and SQL::Statement). When their repository is open, they build fine from the repo and I am confident the next release will build fine.

    The message is: perl-5.18.0 is awesome, but please please test the module you require before starting the installation on machines other than a test environment.


    Enjoy, Have FUN! H.Merijn
New Cool Uses for Perl
Batch Printing in Linux
1 direct reply — Read more / Contribute
by jmlynesjr
on May 15, 2013 at 15:26

    I had a bunch of music lyrics that I wanted to print and I didn't want to manually drive gedit. I did, however, want to retain the document formatting. I found a hint in Re^2: using Brother QL-570 printer with Perl and also found sample commands in an Ubuntu Community Documentation post for using the Open Office command line interface for batch printing or viewing. The are also posts out there for doing a similar thing with MS-Word.

    The following is what I threw together. It does the job for me, YMMV. I needed to print UTF-8 text files, but Open Office will try to print whatever file you give it using the file extension as a guide to the file format. I have also printed .odt files.

    James

    There's never enough time to do it right, but always enough time to do it over...

New Obfuscated Code
Pumping JAPH
1 direct reply — Read more / Contribute
by choroba
on May 13, 2013 at 11:03
    My first JAPH. Works under strict and warnings, too.
    $|--;for($/=1/10;$/<=1/2;$/+=50e-7){$\=qq/J,\r/; substr$\,$/*length$\,$/-$/,$_,for split/(.{2})/, q/ursetk caanho tlhreerP /;print'';}warn qq/\n/;

    Update: Retitled.

    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
New Monk Discussion
Thank You Monks!
1 direct reply — Read more / Contribute
by walkingthecow
on May 13, 2013 at 08:59
    I just want to say thank you to everyone here for all your help. I remember 15 years ago when I was doing my first install of Slackware Linux, I'd ask questions in IRC channels and be met with replies like, "Stupid question.", "RTFM", and so on. I guess my early Linux experience has always made me a bit apprehensive about asking questions. However, this community truly makes a person feel like there is no such thing as a stupid question. You've always been willing to help, and I've never been met with any snide or rude replies. This community is the #1 reason I love Perl, honestly. It's the reason I stick with it. I'm still learning, and still making a lot of mistakes, even after all these years; but I know that if I am ever stuck, or having a difficult time figuring something out on my own, someone here will kindly help me understand. For that, I truly appreciate all of you, and all of your help.

    Thank you Monks for making the Perl community so awesome. And thank you all for helping me along my way as I have learned.
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 chilling in the Monastery: (9)
As of 2013-05-19 19:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best material for plates (tableware) is:









    Results (397 votes), past polls