Perl memory usage profiling and leak detection?

You could have a circular reference in one of your objects. When the garbage collector comes along to deallocate this object, the circular reference means that everything referred to by that reference will never get freed. You can check for circular references with Devel::Cycle and Test::Memory::Cycle. One thing to try (although it might get expensive … Read more

Can you force flush output in Perl?

By default, STDOUT is line-buffered (flushed by LF) when connected to a terminal, and block-buffered (flushed when buffer becomes full) when connected to something other than a terminal. Furthermore, <STDIN> flushes STDOUT when it’s connected to a terminal. This means STDOUT isn’t connected to a terminal, you aren’t printing to STDOUT, or STDOUT’s been messed … Read more

What’s the difference between iterating over a file with foreach or while in Perl?

For most purposes, you probably won’t notice a difference. However, foreach reads each line into a list (not an array) before going through it line by line, whereas while reads one line at a time. As foreach will use more memory and require processing time upfront, it is generally recommended to use while to iterate … Read more

How to use foreach with a hash reference?

foreach my $key (keys %$ad_grp_ref) { … } Perl::Critic and daxim recommend the style foreach my $key (keys %{ $ad_grp_ref }) { … } out of concerns for readability and maintenance (so that you don’t need to think hard about what to change when you need to use %{ $ad_grp_obj[3]->get_ref() } instead of %{ $ad_grp_ref … Read more

Extracting data from HTML table

A Python solution using BeautifulSoup4 (Edit: with proper skipping. Edit3: Using class=”details” to select the table): from bs4 import BeautifulSoup html = “”” <table class=”details” border=”0″ cellpadding=”5″ cellspacing=”2″ width=”95%”> <tr valign=”top”> <th>Tests</th> <th>Failures</th> <th>Success Rate</th> <th>Average Time</th> <th>Min Time</th> <th>Max Time</th> </tr> <tr valign=”top” class=”Failure”> <td>103</td> <td>24</td> <td>76.70%</td> <td>71 ms</td> <td>0 ms</td> <td>829 ms</td> </tr> … Read more

Copying a hashref in Perl [duplicate]

When you copy a hashref into another scalar, you are copying a reference to the same hash. This is similar to copying one pointer into another, but not changing any of the pointed-to memory. You can create a shallow copy of a hash easily: my $copy = { %$source }; The %$source bit in list … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)