perl

How can I extract just the scheme, host, and port from a URL in Perl?

I need to be able to extract just the scheme, host, and port from a URL. So if my url in the browser is: http://www.example.com:80/something.pl I need to be able to get the: http://www.example.com:80 ...

How can I find out what was replaced in a Perl substitution?

Is there any way to find out what was substituted for (the "old" text) after applying the s/// operator? I tried doing: if (s/(\w+)/new/) { my $oldTxt = $1; # ... } But that doesn't work. $1 is undefined. ...

Why can't I use a Perl variable's value to access a lexical variable name?

Why does this print 42: $answer = 42; $variable = "answer"; print ${$variable} . "\n"; but this doesn't: my $answer = 42; my $variable = "answer"; print ${$variable} . "\n"; ...

How can I get $cgi->state to return meaningful information under HTTP::Server::Simple?

First, here is the code I am using (you'll need version 0.42 of HTTP::Server::Simple to run it): #!/usr/bin/perl package My::HTTP::Server; use strict; use warnings; use parent 'HTTP::Server::Simple::CGI'; sub handle_request { my $server = shift; my ($cgi) = @_; print $cgi->header('text/plain'), $cgi->state, "\n"; } packa...

How do I save a file as UTF-8 from Perl?

I'm trying to create/save HTML files in Perl in UTF-8, but nothing I have done so far works. A previous answer here on SO said to use binmode, so I tried that. Here is my code: open (OUT, ">$sectionfilename"); binmode(OUT, ":utf8"); print OUT $section; close OUT; When I open these files in a text editor like Notepad they are still in ...

How do I elegantly split pairs of words out of a string in Perl?

In perl, I have a string that roughly looks like my $str = "one 10 two 20 three 30"; Now, I'd like to split that string into word-number pairs, but have no success. I thought I could do a my @pairs = split /([a-z]+[^a-z]+)/, $str; and would then have $pairs[0] eq 'one 10 ' $pairs[1] eq 'two 20 ' $pairs[2] eq 'three ...

How can I extract fields from a CSV file in Perl?

I want to extract a particular fields from a csv file (830k records) and store into hash. Is there any fast and easy way to do in Perl with out using any external methods? How can I achieve that? ...

Does grouping work inside a Perl regex character class?

consider the following code: perl -wne 'chomp;print if m/[^(?:test)]/' I was surprised to see that grouping inside a character class works, How does this differ from (?!pattern)? ...

Why does Perl's LWP gives me a different encoding than the original website?

Hey, Lets say i have this code: use strict; use LWP qw ( get ); my $content = get ( "http://www.msn.co.il" ); print STDERR $content; The error log shows something like "\xd7\x9c\xd7\x94\xd7\x93\xd7\xa4\xd7\xa1\xd7\x94" which i'm guessing it's utf-16 ? The website's encoding is with <META HTTP-EQUIV="Content-Type" CONTENT="text/h...

How can I resolve this case of `Useless use of a variable in a void context`?

How can I resolve this case of Useless use of a variable in a void context? For example: my $err = $soap_response->code, " ", $soap_response->string, "\n"; return $err; I get warnings like Useless use of a variable in a void context? Why? How can I resolve it ? ...

How does opendir work in Perl 6?

Hello! Can someone tell me, why the "opendir" doesn't work? #!/usr/bin/env perl6 use v6; my $file = 'Dokumente/test_file'; if ( my $fh = open $file, :r ) { for $fh.lines -> $line { say $line; } } else { say "Could not open '$file'"; } my $dir = 'Dokumente'; my $dh = opendir $dir err die "Could not open $dir: $!"; ...

Why doesn't my parent process see the child's output until it exits?

Consider the following script: use IO::File; $| = 1; my ($handle, $pid) = myPipe(); if ($pid == 0) { print "$$"; sleep 5; exit; } print "child: ".<$handle>."\n"; sub myPipe { my $handle = new IO::File(); my $pid = open($handle, "-|"); return ($handle, $pid); } In this case, the "child:" message doesn't appear for 5 secon...

automatically expanding array in .NET ?

Does .NET have anything similar to Perl arrays, which are indexed numerically but automatically expand as needed? It would work like this: var x = new DreamArray<string>(); x[6] = "foo"; // x automatically has 7 elements x[10] = "bar"; // now it has 11 ...

How can I mix command line arguments and filenames for <> in Perl?

Consider the following silly Perl program: $firstarg = $ARGV[0]; print $firstarg; $input = <>; print $input; I run it from a terminal like: perl myprog.pl sample_argument And get this error: Can't open sample_argument: No such file or directory at myprog.pl line 5. Any ideas why this is? When it gets to the <> is it trying t...

How do I get killed using Perl FCGI?

I'm having a little problem with nginx and the Perl FCGI module. I have a long operation in my FCGI program that may outlive the server (or the user on the server) on the other end of the Unix socket I'm using to communicate FCGI. I need the FCGI accept() loop in my program to break if the FCGI request is closed. I tried installing INT, ...

Why does Gtk2::Builder mix up my signals?

I'm having this perl code: #!/usr/bin/perl use warnings; use strict; use Data::Dumper; use Gtk2 '-init'; my $data; my $builder_file = "lists.glade"; my $builder = Gtk2::Builder->new(); $builder->add_from_file( $builder_file ) or die "Couldn't read $builder_file"; $builder->connect_signals( undef ); my $window = $builder->get_...

How can I script vim to run perltidy on a buffer?

At my current job, we have coding-style standards that are different from the ones I normally follow. Fortunately, we have a canned RC file for perltidy that I can apply to reformat files before I submit them to our review process. I have code for emacs that I use to run a command over a buffer and replace the buffer with the output, wh...

How would I remove all <script> tags (and everything in between) from multiple files using UNIX?

I have a folder with multiple files, and I'd like to remove all <script> tags and everything in between, e.g.: This: <script type="text/javascript">function(foo);</script> As well as this: <script type="text/javascript" src="scripts.js"></script> I think in PHP it would be something like this: <?php $string = preg_replace('#(\n?<...

How can I get rid of the "Can't locate object method "warn" via package "sssself" error in IE::Mechanize?

I'm playing around with the Win32::IE::Mechanize. I'm trying a script to automatically access six of my web-based email accounts. The script basically works but perl throws a kind of cryptic "Can't locate object method "warn" via package "sssself" (perhaps you forgot to load " sssself)" error. Despite the error, the script can still get ...

I can I separate multiple logical pages in a text file I create in Perl?

So far, I've been successful with generating output to individual files by opening a file for output as part of outer loop and closing it after all output is written. I had used a counting variable ($x) and appended .txt onto it to create a filename, and had written it to the same directory as my perl script. I want to step the code up a...