perl

Why is my hash not undef?

I have the following (idealised from a bug) short script in Perl: my %metadata = undef; if (defined %metadata) { print "defined"; } For some reason the output of the program is always "defined". So setting the hash to be "undefined" somehow makes it defined. Is it defined as being "undefined"? EDIT: This is an idealised case, ...

How can I set a default value for a Perl variable?

I am completely new to Perl. I needed to use an external module HTTP::BrowserDetect. I was testing some code and tried to get the name of the OS from os_string method. So, I simply initialized the object and created a variable to store the value returned. my $ua = HTTP::BrowserDetect->new($user_agent); my $os_name = $ua->os_string(); ...

How should I use Perl's File::Temp?

I would like to create a temp file, write to the file-handle then call an external program with the filename. The problem is I would normally want to close the file after writing to it and before calling the external program, but if I understand correctly close-ing a tempfile() causes it to be removed. So what is the solution here? ...

How can I filter an array without using a loop in Perl?

Here I am trying to filter only the elements that do not have a substring world and store the results back to the same array. What is the correct way to do this in Perl? $ cat test.pl use strict; use warnings; my @arr = ('hello 1', 'hello 2', 'hello 3', 'world1', 'hello 4', 'world2'); print "@arr\n"; @arr =~ v/world/; print "@arr\n"; ...

How can I ping a host with a Perl one-liner with Net::Ping?

Trying to integrate the following Perl one-liner into a shell script. This code works within a Perl script but not as a one-liner executed from a shell script. I've tried replacing $host with a real hostname with no luck. #!/bin/ksh hosts="host1 host2 host3" PERL=/usr/bin/perl # Check to see if hosts are accessible. for host in $hos...

How can I parse just part of a file with Perl?

I'm a total newbie to Perl, but I've heard that it's great for parsing files, so I've thought of giving it a spin. I have a text file that has the following sample info: High school is used in some parts of the world, particularly in Scotland, North America and Oceania to describe an institution that provides all or part of secondary e...

How do you convert the following encryption code from Perl (or from PHP) to VB.NET?

I have some encryption code that has been written in Perl (also a code snippet in PHP) - but I just can't get a version written in VB.NET to work with the third party. Example in Perl package Sitemaker::API::Crypt; use warnings; use strict; use base qw( Exporter ); use Crypt::CBC; use MIME::Base64; our @E...

How can I return context sensitive return values in Perl 6?

In the summary of differences between Perl 5 and Perl 6, it is noted that the wantarray function is gone: wantarray() is gone wantarray is gone. In Perl 6, context flows outwards, which means that a routine does not know which context it is in. Instead you should return objects that do the right thing in every conte...

Parsing a file into an array

I'm really new to Perl, but I want to basically read in a file to get some data out of it. I want to parse this data into an array. Why an array? Because, I want to use the data to generate an graph (bar or pie of the data). Here's my code for Perl so far: #!/usr/bin/perl -w use warnings; #Creating an array my @cpu_util; #Creating a...

How can I test if a filename matching a pattern exists in Perl?

Can I do something like this in Perl? Meaning pattern match on a file name and check whether it exists. if(-e "*.file") { <Do something> } I know the longer solution of asking system to list the files present; read it as a file and then infer whether file exists or not. ...

How can I easily test if XML file is well-formed in Perl?

I know there are quite some threads talking about validating XML file against its XML schema, such as : validate-xml-using-libxml and xml-schema-validation-with-relaxng So if there is a simple Perl module on CPAN that can test this with minimal code, then that would be very fantastic to know. ...

How can I add an externally managed Perl installs to Perlbrew?

I would like to use perlbrew to manage multiple Perl installations on my system. However, in addition to the clean installs from CPAN and the system install, I have a couple of other Perl installs that are tied to specific projects. I would like to be able to switch to these perls as well. For example, if I have /opt/SomeApp/perl/bin/p...

PHP resources for the Perl Programmer

I used to write PHP 10 years ago. For the last few years I've been much more into the C variant languages and Perl. Any recommendations for books or resources that seem particularly well geared towards the transition from Perl to PHP? EDIT: It turns out this has already been answered here, I just didn't see it in my search. ...

Regex for finding number of times substring "hello hello" occurs in string "hello hello hello"

I want to get the count of occurrence of a substring within a string. My string is "hello hello hello". I want to get the number of times "hello hello" occurs in it, which in the above case is 2. Can someone please help me find a regex for it? ...

Is Perl's -w switch for warnings deprecated after 5.6.x?

I read in Simon Cozens' book "Beginning Perl" that -w switch for warnings would be deprecated going forward. Is this true or is it still ok to continue using -w rather than "use warnings". ...

How can I ensure only one copy of a Perl script is running at a time?

I need to ensure that only one copy of my Perl script is running at a time. According to the suggestions here I wrote a sub to do the check: sub check_instances { open my $fh, '<', $0 or die $!; unless (flock($fh, LOCK_EX|LOCK_NB)) { print "$0 is already running. Exiting.\n"; exit 1; } } But it doesn't ...

Why does WWW::mechanize complain about 'Unknown submit_form parameter "form_id"'?

I'm using the submit_form function in WWW::Mechanize as follows: eval{ my $me = $mechanize->submit_form( form_id => 'signin', fields => { login => 'abc', password => 'def'} ); $me->is_success or die $me->status_line; }; Although this throws the error of Unknown submit_form parameter "form_id" it still re...

Add the content-length field from Apache log to AWStats

I want to enhance my AWStats installation by adding the Content-Length field from a POST request/response to my AWStats reports. How do I accomplish this? What do I need to add to awstats.pl? I'm totally new to Perl programming. The LogFormat in Apache is: LogFormat "%h %l %u %t \"%r\" %>s %b %{Content-Length}i" enhcommon Many tha...

How do I create a link on an image in a CGI script?

I can successfully show image in my CGI scripts via CGI.pm, using this code: #!/usr/bin/perl -w use CGI::Carp qw(fatalsToBrowser); use CGI qw/:standard/; print img {src => "../images/myimage.png", align=>"CENTER"}; However when I want to do is to include URL in that image, wo that whenever people click on that image it will point to...

Why does Perl makes the system very slow when I made more than 4,000 database connections?

I was writing a code to find the speed of my database using a Perl script. My intention was to make a 4,000 database connection after each fork (which would act as a 4,000 different clients) and sleep, and I issue the update command when I get the signal but the system itself becomes very slow and almost hangs for making the connection...