perl

Comparing records in file and reporting stats - Scenario 1

The requirements are : Fact 1 : We have some data files produced by a legacy system Fact 2 : We have some data files produced by a new system that should eventually replace the legacy one Fact 3 : Both the files are text/ASCII files, with records being composed of multiple lines. Each line, within a record, consists of a fieldname a...

Should I use \d or [0-9] to match digits in a Perl regex?

Having read a number of questions/answers over the past few weeks, I have seen the use of \d in perl regular expressions commented on as incorrect. As in the later versions of perl \d is not the same as [0-9], as \d will represent any Unicode character that has the digit attribute, and that [0-9] represents the characters '0', '1', '2', ...

Perl piped log script only running every 2 hits

I am using this script for a piped log setup in Apache 2: #!/usr/local/bin/perl $|=1; # Use unbuffered output while (<STDIN>) { if (<STDIN> =~ m/(.php|.html|.htm|.dhtml|.cpp|.h|.c|.txt|.pdf|.pl)$/) {system("beep");} } I am sending in the directive %f to give it the filename. As you can tell, it checks to see if the requested...

How can I resize an image to fit area with Image::Magick?

From command line with imagemagick, you can use: convert dragon.gif -resize 64x64^ -gravity center -extent 64x64 fill_crop_dragon.gif to resize and then crop an image so that it fills the area as much as possible. How do I do this from Perl's Image::Magick? ...

Can this Perl behaviour be emulated with a switch/case or given/when?

I was wondering if anyone had any suggestions on improving the following code (if possible) so that it didn't need the repeated (my @a = $time =~ ...), possibly using case/switch or given/when or some other idea that i'm missing? my $time = '12:59pm'; if( my @a = $time =~ m/^(\d\d?)(am|pm)$/ ) { tell_time( $a[0], 0, $a[1] ) } if...

How should I handle digits from different sets of UNICODE digits in the same string?

I am writing a function that transliterates UNICODE digits into ASCII digits, and I am a bit stumped on what to do if the string contains digits from different sets of UNICODE digits. So for example, if I have the string "\x{2463}\x{24F6}" ("④⓶"). Should my function return 42? croak that the string contains mixed sets? carp that the...

Perl: Looping over input lines with an index-based approach

This is a beginner-best-practice question in perl. I'm new to this language. The question is: If I want to process the output lines from a program, how can I format THE FIRST LINE in a special way? I think of two possibilities: 1) A flag variable, once the loop is executed first time is set. But it will be evaluated for each cycle. BA...

How can I map a local unix socket to an inet socket?

I'm curious if it is possible to map a UNIX socket on to an INET socket. The situation is simply that I'd like to connect to a MySQL server. Unfortunately it has INET sockets disabled and therefore I can only connect with UNIX sockets. The tools I'm using/writing have to connect on an INET socket, so I'm trying to see if I can map one on...

How can I include Win32 modules only when I'm running my Perl script on Windows?

Hello, I have a problem that I cannot seem to find an answer to. With Perl I need to use a script across Windows and unix platforms. Te problem is that on Windows we use Win32-pecific modules like Win32::Process, and those modules do not exist on unix. I need a way to include those Win32 modules only on Windows. if($^O =~ /win/i) { ...

How can I keep WWW::Mechanize from following redirects?

I have a Perl script that uses WWW::Mechanize to read from a file and perform some automated tasks on a website. However, the website uses a 302 redirect after every time I request a certain page. I don't want to be redirected (the page that it redirects to takes too long to respond); I just want to loop through the file and call the f...

Unable to replace the word in a given folder's contents by Sed/Python/Perl

I have a project where I have folders, subfolders, and files. I need to replace the word Masi by the word Bond in each files. I run the following Sed script called replace unsuccessfully s/Masi/Bond/ in Zsh by sed -f PATH/replace PATH2/project/** It gives me all files, also the ones which do not have Masi, as an output. Sed is n...

Can I have an application scope variable in Perl?

Hello everybody, I'm somewhat new to Perl/CGI, and I'm coming from a Java/JSP background. I'm writing a small prototype and need to load some "heavy" data (~200MB) into a data structure. Now, I'd obviously like to avoid loading the data with every request. So far I managed to use a "static" variable (one enclosed in a {} block), but t...

What does -G do in Perl?

I have searched around the Internet and the Perl doc and can't seem to find the answer. Help will be appreciated. EDIT:: He asked me about -G, wrote it down on a piece of paper and when i looked stumped asked me to go read up on the basics. ...

Is it better to use the Perl DBI module or to set up a system DSN and use ODBC?

I am using Perl to collect data from several logfiles and store it into an Oracle database on the same Windows 2003 host I am running my script on. Is it better to use the Perl DBI module or to set up a system DSN and use ODBC? Thank you ahead of time! Michael ...

How do I sleep for a millisecond in Perl?

How do I sleep for shorter than a second in Perl? ...

Connect to SQL Server 2005 from Perl and do a SELECT

How do I do a SELECT on a SQL Server 2005 from a Perl script? ...

How do I build Perl regular expressions dynamically?

I have a Perl script that traverses a directory hierarchy using File::Next::files. It will only return to the script files that end in ".avi", ".flv", ".mp3", ".mp4", and ".wmv." Also it will skip the following sub directories: ".svn" and any sub directory that ends in ".frames." This is specified in the file_filter and descend_filte...

How do I tell cpan to install all dependencies?

How do I tell cpan to install all dependencies? Edit: After following Sinans link. I tried setting these in cpan: cpan> o conf prerequisites_policy follow cpan> o conf commit I still had to answer y a couple of times. (but fewer than before it feels like) Any way to get it to always go ahead and install? I want to make it unattended...

How does Perl know a file is binary?

I know you can use the file test operator -B to test if a file is binary, but how does Perl implement this internally? ...

How can I recursively visit links without revisiting links?

I want to check a site for links, and then recursively check those sites for links. But I don't want to fetch the same page twice. I'm having trouble with the logic. This is Perl code: my %urls_to_check = (); my %checked_urls = (); &fetch_and_parse($starting_url); use Data::Dumper; die Dumper(\%checked_urls, \%urls_to_check); sub ...