Questions tagged [perl]

Perl is a procedural, high-level, general-purpose, dynamic programming language, known for its native support of regular expressions and string parsing capabilities. Please use this tag for questions about Perl in general. For things related to the new (but related) language Raku (formerly "Perl 6"), please use the raku tag. For Perl-style regular expressions in other languages, use the regex tag, or, if they are based on the PCRE library, the pcre tag.

Perl is a highly capable, feature-rich programming language with over 25 years of development. Perl runs on over 100 platforms from portables to mainframes and is suitable for both rapid prototyping and large-scale development projects.

Perl can also refer to a family of high-level, general-purpose, interpreted, dynamic programming languages. The languages in this family include Perl and Raku (formerly known as Perl 6). For Raku-related questions, please use the tag . For "Perl-style" regular expressions in other languages, often based on the PCRE library, do not use tag , but tag or a more specific tag such as tag .

Related Resources

Perl-related Blogs and Bloggers

Free Perl Programming Books

Perl Programming Books

Related tags

Date & Time

Libraries for working with dates and times

Devices

Libraries to talk to physical devices

DevOps Tools

Libraries that help when you want to deploy software across networks on several hosts that are working across computer networks

  • Rex - Remote Execution

FAQs

Frequently ask questions

More information

67820 questions
103
votes
3 answers

How should I use the "my" keyword in Perl?

I keep seeing the my keyword in front of variable names in example Perl scripts online, but I have no idea what it means. I tried reading the manual pages and other sites online, but I'm having difficulty discerning what it is for, given the…
FistOfFury
  • 6,735
  • 7
  • 49
  • 57
103
votes
8 answers

Turning multiple lines into one comma separated line

I have the following data in multiple lines: foo bar qux zuu sdf sdfasdf What I want to do is to convert them to one comma separated line: foo,bar,qux,zuu,sdf,sdfasdf What's the best unix one-liner to do that?
neversaint
  • 60,904
  • 137
  • 310
  • 477
100
votes
2 answers

In Perl, what is the difference between a .pm (Perl module) and .pl (Perl script) file?

What is the Difference between .pm (Perl module) and .pl (Perl script) file? Please also tell me why we return 1 from file. If return 2 or anything else, it's not generating any error, so why do we return 1 from Perl module?
user380979
  • 1,387
  • 5
  • 16
  • 20
99
votes
9 answers

How can I convert a string to a number in Perl?

I have a string which holds a decimal value in it and I need to convert that string into a floating point variable. So an example of the string I have is "5.45" and I want a floating point equivalent so I can add .1 to it. I have searched around the…
Anton
  • 12,285
  • 20
  • 64
  • 84
99
votes
14 answers

How do I tell if a variable has a numeric value in Perl?

Is there a simple way in Perl that will allow me to determine if a given variable is numeric? Something along the lines of: if (is_number($x)) { ... } would be ideal. A technique that won't throw warnings when the -w switch is being used is…
Derek Park
  • 45,824
  • 15
  • 58
  • 76
98
votes
8 answers

What are the Python equivalents to Ruby's bundler / Perl's carton?

I know about virtualenv and pip. But these are a bit different from bundler/carton. For instance: pip writes the absolute path to shebang or activate script pip doesn't have the exec sub command (bundle exec bar) virtualenv copies the Python…
riywo
  • 1,378
  • 1
  • 11
  • 14
97
votes
13 answers

How do I daemonize an arbitrary script in unix?

I'd like a daemonizer that can turn an arbitrary, generic script or command into a daemon. There are two common cases I'd like to deal with: I have a script that should run forever. If it ever dies (or on reboot), restart it. Don't let there ever…
dreeves
  • 26,430
  • 45
  • 154
  • 229
95
votes
9 answers

How to print lines between two patterns, inclusive or exclusive (in sed, AWK or Perl)?

I have a file like the following and I would like to print the lines between two given patterns PAT1 and PAT2. 1 2 PAT1 3 - first block 4 PAT2 5 6 PAT1 7 - second block PAT2 8 9 PAT1 10 - third block I have read How to select lines between…
fedorqui
  • 275,237
  • 103
  • 548
  • 598
94
votes
9 answers

In Perl, how can I concisely check if a $variable is defined and contains a non zero length string?

I currently use the following Perl to check if a variable is defined and contains text. I have to check defined first to avoid an 'uninitialized value' warning: if (defined $name && length $name > 0) { # do something with $name } Is there a…
Jessica
  • 1,421
  • 5
  • 15
  • 10
93
votes
6 answers

Use of 'use utf8;' gives me 'Wide character in print'

If I run the following Perl program: perl -e 'use utf8; print "鸡\n";' I get this warning: Wide character in print at -e line 1. If I run this Perl program: perl -e 'print "鸡\n";' I do not get a warning. I thought use utf8 was required to use…
Eric Johnson
  • 17,502
  • 10
  • 52
  • 59
89
votes
5 answers

Perl build, unit testing, code coverage: A complete working example

Most Stackoverflow answers that I have found in regards to the Perl build process and unit testing and code coverage simply point me to CPAN for the documentation there. There's absolutely nothing wrong with pointing to CPAN modules because that's…
89
votes
10 answers

Perl: function to trim string leading and trailing whitespace

Is there a built-in function to trim leading and trailing whitespace such that trim(" hello world ") eq "hello world"?
Landon Kuhn
  • 76,451
  • 45
  • 104
  • 130
88
votes
6 answers

How can I de-install a Perl module installed via `cpan`?

I am using Perl running in user space (not installed via root) and installing modules via the command-line cpan. I would like to know if there is a simple way to remove a module without having to do a lot of work deleting individual files. I…
user181548
88
votes
15 answers

What is the best way to delete a value from an array in Perl?

The array has lots of data and I need to delete two elements. Below is the code snippet I am using, my @array = (1,2,3,4,5,5,6,5,4,9); my $element_omitted = 5; @array = grep { $_ != $element_omitted } @array;
user21246
  • 1,774
  • 3
  • 15
  • 15
87
votes
4 answers

How to replace a string in an existing file in Perl

I want to replace the word "blue" with "red" in all text files named as 1_classification.dat, 2_classification.dat and so on. I want to edit the same file so I tried the following code, but it does not work. Where am I going wrong? @files =…
user831579
  • 981
  • 1
  • 8
  • 8