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
Checking Variable for Not Null
2 direct replies — Read more / Contribute
by dirtdog
on May 27, 2015 at 10:49

    Hi Monks, Can anyone tell me if the following 2 statements would be equivalent as far as checking that a variable is not null.

    next unless ! $ex_date; next unless !defined($ex_date);

    Basically, i would like to go to the next record if the variable $ex_date is null. any help is much appreciated. thanks Tony

how to let sub return hash of 2 sub?
2 direct replies — Read more / Contribute
by toohoo
on May 27, 2015 at 10:18

    Hello wise monks

    it comes to me by circumstances, that I have to create a construct in a sub, which gives me back a hash (or object) of 2 subs which each return a value.

    This is the goal. The circumstances are too complicated to describe them here.

    I know that what I want seems to be done much easier by referencing the hash itself. But please, would you mind thinking of how it proably might be done to use the wanted sub just right as I would try to?

    The goal of all is this one line of code

    print $_->name, ': ', $_->val, "\n" for $objA->parameter( 'a.x' );

    I have tried some notations which all do not work because I am writing it false.

    Would be great if someone has an idea.

    best regards, Thomas

Reuse Tk:Frame -solved
No replies — Read more | Post response
by glenn
on May 27, 2015 at 10:01

    Hi Monks, back again with another Tk question. I want to reuse a frame in main window to show different information based on what the user selects. I am trying to avoid having to destroy and recreate all the widgets as a user goes into a sub section and back out. This is a very simplified working example code of what I want to do (except it does not load the widgets and will eventually run out of mem, hence my Q). Basically there are multiple systems monitored and buttons for each system to show sub information on that system. Thanks.

    Only Unmap/Map a FRAME not the widgets within


Data Move
1 direct reply — Read more / Contribute
by PilotinControl
on May 27, 2015 at 09:36

    Good Morning Monks!
    The sub routine code posted below does what I want except it copies all lines over to a new file instead of one line based on its unique key. What the code does is remove a line from one file however it copies All lines instead of the updated line to the new file. What am I missing? The update code works as it updates a record. What my focus is on is moving that updated line from the original cardata.txt to the updatedcardata.txt. Thanks in advance!

    use strict; use warnings; use File::Copy; sub update_record_now { my ($rcid,$car,$owner) = @_; open my $infile, '<','cardata.txt' or die $!; open my $outfile, '>', 'cartemp.txt' or die $!; while (<$infile>) { s/^$rcid\:.*/$rcid\:$car\:$owner/g; print $outfile $_; } my $pinfile = "cardata.txt"; my @data; { open(my $data_file_handle, '<', "$pinfile") or die("Can't open file: $ +pinfile"); @data = <$data_file_handle>; close($data_file_handle); close $pinfile; } chomp(@data); open(my $out_file_handle,'>', $pinfile) or die("Can't open file: $pinf +ile"); foreach my $line_from_file ( @data ) { my @field = split( /\:/, $line_from_file ); next if( $field[0] == $rcid ); print $out_file_handle $line_from_file, "\n"; } close $pinfile; close $out_file_handle; close $infile; close $outfile; move 'cartemp.txt', 'updatedcardata.txt'; } __DATA__ 1:Chevy:Bob 2:Ford:Tom 3:Fiat:Dave
How smart is 'seek $fh, $pos, 0'?
2 direct replies — Read more / Contribute
by Monk::Thomas
on May 27, 2015 at 08:36

    If the filehandle is at position X and I want to go to position Y: Does 'seek $fh, $pos, 0' rewind to the begin and then skip ahead to $pos or does it optimize automatically and just moves from the current position to the intended position?

    In other words:

    a) Does it actually make a difference if I calculate the difference and use 'seek $fh, $delta, 1' or if I simply use 'seek $fh, $abs, 0'?

    b) Is there a difference between going back (Y<X) and skipping ahead (Y>X)?

Inserting new column
3 direct replies — Read more / Contribute
by Avi_Perl_Beginner
on May 27, 2015 at 06:30
    My requirement is to read all.csv files in the format groupid_groupname.csv in a folder. Secondly after reading create two new columns with groupid & groupname and print the constant value fetched above. I have written the code but while using just print it works perfect and shows exact result but when writing to csv goes for a toss. Please advice
    use strict; use warnings; use Text::CSV; use Text::CSV_XS; use autodie; print_Filenames_directory(); sub print_Filenames_directory { my @files = <*.csv>; foreach my $file (@files) { open my $in, "+<:encoding(utf8)", $file or die $!; my $csv = Text::CSV_XS->new ({ binary => 1, auto_diag => 1 }); my @name = split _,$file; my $grpid = $name[0]; my $grpname = substr($name[1], 0,-4); my @headers; while (my $row = $csv->getline($in)) { if($.==1) { # Add value to @columns array splice @$row, 0, 0, "Group ID"; splice @$row, 1, 0, "Group Name"; @headers = "@$row\n"; print "@headers"; #$csv->print(@headers); } else { splice @$row, 0, 0, $grpid; splice @$row, 1, 1, $grpname; print "@$row\n"; #$csv->print($in, @$row); + } } # check for errors $csv->eof or $csv->error_diag(); close $in; } }
CGI Script connection to SQL Server.
2 direct replies — Read more / Contribute
by solegaonkar
on May 27, 2015 at 05:41
    Hello Monks, Anyone seen this issue.. I have a script that connects to SQLServer. It works in standalone.. but in CGI, gives error not found.. Tried updating the LD_LIBRARY_PATH. Set the permissions on the library file as well as the entire path to 777. But no luck... Can you please suggest what could be wrong!
how to read words in a line into separate variables
1 direct reply — Read more / Contribute
by jag194u
on May 27, 2015 at 05:35

    Hi Monks, lets say i have a line


    how do i read the parts of above line into different perl variables

    $a = "thepath"; $b = 8'h44; $c = status; $d = "info" and $e = write
Lightweight alternative to DateTime::Duration?
No replies — Read more | Post response
by perlancar
on May 27, 2015 at 05:19

    Hi guys,

    I'm looking for a lightweight alternative to DateTime::Duration, like Time::Moment is a lightweight alternative for DateTime. 100% (or even "50%") API compatibility is not a must. Any pointers?

Mojo Dom extract
1 direct reply — Read more / Contribute
by ribo75017
on May 27, 2015 at 04:55


    I wanna extract : - text from date, title, cat and lieu classes - anchors with Mojo::Dom but I am not able to do it :

    Is it possible to get data like this : for each anchor into "ret" div, get (anchors, date elements, title, cat and lieu)

    Thanks a lot

    use Mojo::DOM; my $dom = Mojo::DOM->new(<<'HTML'); <div class="ret"> <a href="" title="Text 1"> <div class="rtm"> <div class="date"> <div>22</div> <div>mai</div> <div>19:52</div> </div> <div class="image"> <div class="imageclass-and-nb"><im +g src="" alt="Text 1"></div> </div> <div class="all"> <h2 class="title">Text 1 Title</h2> <div class="cat">Blue</div> <div class="lieu">Dourdan</div> </div> </div> </a> <a href="" title="Text 2"> <div class="rtm"> <div class="date"> <div>22</div> <div>mai</div> <div>11:55</div> </div> <div class="image"> <div class="imageclass"><img src="" alt="Text 2"></div> </div> <div class="detail"> <h2 class="title">Text 2 title</h2> <div class="cat">Blue</div> <div class="lieu">Champigny-sur-Marne< +/div> </div> </div> </a> </div> HTML print $dom->find('')->map(sub{$_->children->each})->map(sub{$_ +->text})->each;

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!
  • 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?

    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 examining the Monastery: (22)
    As of 2015-05-27 15:03 GMT
    Find Nodes?
      Voting Booth?

      In my home, the TV remote control is ...

      Results (539 votes), past polls