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
Filter by
Sorted by
Tagged with
836 votes
48 answers
823k views

How to fix a locale setting warning from Perl

When I run perl, I get the warning: perl: warning: Setting locale failed. perl: warning: Please check that your locale settings: LANGUAGE = (unset), LC_ALL = (unset), LANG = "en_US.UTF-8" ...
xain's user avatar
  • 13.4k
594 votes
7 answers
113k views

Why does modern Perl avoid UTF-8 by default?

I wonder why most modern solutions built using Perl don't enable UTF-8 by default. I understand there are many legacy problems for core Perl scripts, where it may break things. But, from my point of ...
w.k's user avatar
  • 8,296
526 votes
6 answers
28k views

Why is this program valid? I was trying to create a syntax error

I'm running ActiveState's 32 bit ActivePerl 5.14.2 on Windows 7. I wanted to mess around with a Git pre-commit hook to detect programs being checked in with syntax errors. (Somehow I just managed to ...
Bill Ruppert's user avatar
  • 8,986
410 votes
8 answers
224k views

Match whitespace but not newlines

I sometimes want to match whitespace but not newline. So far I've been resorting to [ \t]. Is there a less awkward way?
JoelFan's user avatar
  • 38k
353 votes
12 answers
68k views

Why are scripting languages (e.g. Perl, Python, and Ruby) not suitable as shell languages? [closed]

What are the differences between shell languages like Bash (bash), Z shell (zsh), Fish (fish) and the scripting languages above that makes them more suitable for the shell? When using the command line,...
Muhammad Alkarouri's user avatar
347 votes
8 answers
420k views

Regex to match any character including new lines

Is there a regex to match "all characters including newlines"? For example, in the regex below, there is no output from $2 because (.+?) doesn't include new lines when matching. $string = "START ...
kurotsuki's user avatar
  • 4,547
326 votes
5 answers
617k views

How do I break out of a loop in Perl?

I'm trying to use a break statement in a for loop, but since I'm also using strict subs in my Perl code, I'm getting an error saying: Bareword "break" not allowed while "strict subs" in use at ./...
Zain Rizvi's user avatar
  • 24.1k
315 votes
24 answers
158k views

How can I start an interactive console for Perl?

How can I start an interactive console for Perl, similar to the irb command for Ruby or python for Python?
ibz's user avatar
  • 45.4k
290 votes
5 answers
92k views

What are the differences between Perl, Python, AWK and sed? [closed]

What are the main differences among them? And in which typical scenarios is it better to use each language?
Khaled Al Hourani's user avatar
284 votes
13 answers
524k views

How can I check if a Perl array contains a particular value?

I am trying to figure out a way of checking for the existence of a value in an array without iterating through the array. I am reading a file for a parameter. I have a long list of parameters I do ...
Mel's user avatar
  • 3,915
281 votes
12 answers
646k views

Find size of an array in Perl

I seem to have come across several different ways to find the size of an array. What is the difference between these three methods? my @arr = (2); print scalar @arr; # First way to print array size ...
David's user avatar
  • 2,865
268 votes
6 answers
251k views

What's the difference between Perl's backticks, system, and exec?

Can someone please help me? In Perl, what is the difference between: exec "command"; and system("command"); and print `command`; Are there other ways to run shell commands too?
rlbond's user avatar
  • 66.6k
255 votes
34 answers
221k views

How can I quickly sum all numbers in a file?

I have a file which contains several thousand numbers, each on its own line: 34 42 11 6 2 99 ... I'm looking to write a script which will print the sum of all numbers in the file. I've got a solution,...
Mark Roberts's user avatar
  • 3,269
244 votes
10 answers
273k views

How do I use boolean variables in Perl?

I have tried: $var = false; $var = FALSE; $var = False; None of these work. I get the error message Bareword "false" not allowed while "strict subs" is in use.
Chad DeShon's user avatar
  • 4,742
242 votes
24 answers
376k views

What's the easiest way to install a missing Perl module?

I get this error: Can't locate Foo.pm in @INC Is there an easier way to install it than downloading, untarring, making, etc?
217 votes
3 answers
162k views

How is Perl's @INC constructed?

What are all the ways of affecting where Perl modules are searched for? or, How is Perl's @INC constructed? As we know, Perl uses @INC array containing directory names to determine where to search for ...
DVK's user avatar
  • 128k
212 votes
8 answers
248k views

How do I perform a Perl substitution on a string while keeping the original?

In Perl, what is a good way to perform a replacement on a string using a regular expression and store the value in a different variable, without changing the original? I usually just copy the string ...
kaybenleroll's user avatar
  • 16.9k
211 votes
7 answers
482k views

How do I compare two strings in Perl?

How do I compare two strings in Perl? I am learning Perl, I had this basic question looked it up here on StackOverflow and found no good answer so I thought I would ask.
PJT's user avatar
  • 3,519
205 votes
12 answers
131k views

What is the difference between 'my' and 'our' in Perl?

I know what my is in Perl. It defines a variable that exists only in the scope of the block in which it is defined. What does our do? How does our differ from my?
Nathan Fellman's user avatar
196 votes
14 answers
387k views

How do you round a floating point number in Perl?

How can I round a decimal number (floating point) to the nearest integer? e.g. 1.2 = 1 1.7 = 2
Ranguard's user avatar
  • 2,776
189 votes
11 answers
395k views

How can I print the contents of a hash in Perl?

I keep printing my hash as # of buckets / # allocated. How do I print the contents of my hash? Without using a while loop would be most preferable (for example, a one-liner would be best).
Kys's user avatar
  • 2,595
186 votes
23 answers
208k views

How do I get the full path to a Perl script that is executing?

I have Perl script and need to determine the full path and filename of the script during execution. I discovered that depending on how you call the script $0 varies and sometimes contains the fullpath+...
Chris Madden's user avatar
  • 2,550
181 votes
23 answers
117k views

How can I delete a newline if it is the last character in a file?

I have some files that I'd like to delete the last newline if it is the last character in a file. od -c shows me that the command I run does write the file with a trailing new line: 0013600 n t ...
Todd Partridge 'Gen2ly''s user avatar
175 votes
11 answers
243k views

How do I remove duplicate items from an array in Perl?

I have an array in Perl: my @my_array = ("one","two","three","two","three"); How do I remove the duplicates from the array?
David's user avatar
  • 14.1k
172 votes
20 answers
66k views

while (1) Vs. for (;;) Is there a speed difference?

Long version... A co-worker asserted today after seeing my use of while (1) in a Perl script that for (;;) is faster. I argued that they should be the same hoping that the interpreter would optimize ...
Copas's user avatar
  • 5,931
165 votes
8 answers
123k views

Best way to give a variable a default value (simulate Perl ||, ||= )

I love doing this sort of thing in Perl: $foo = $bar || $baz to assign $baz to $foo if $bar is empty or undefined. You also have $foo ||= $bletch which will only assign $bletch to $foo if $foo is not ...
Tom Auger's user avatar
  • 19.7k
164 votes
6 answers
301k views

How to match any non white space character except a particular one?

In Perl \S matches any non-whitespace character. How can I match any non-whitespace character except a backslash \?
Lazer's user avatar
  • 92.4k
156 votes
9 answers
119k views

What exactly does Perl's "bless" do?

I understand one uses the "bless" keyword in Perl inside a class's "new" method: sub new { my $self = bless { }; return $self; } But what exactly is "bless" doing to that hash reference ?...
user47145's user avatar
  • 1,795
152 votes
13 answers
79k views

grep -P no longer works. How can I rewrite my searches?

It looks like the new version of OS X no longer supports grep -P and as such has made some of my scripts stop working, for example: var1=`grep -o -P '(?<=<st:italic>).*(?=</italic>)' ...
kugyousha's user avatar
  • 2,561
149 votes
5 answers
130k views

How do I update all my CPAN modules to their latest versions?

How do I update all my CPAN modules to their latest versions?
David B's user avatar
  • 29.6k
143 votes
78 answers
62k views

Hidden features of Perl?

What are some really useful but esoteric language features in Perl that you've actually been able to employ to do useful work? Guidelines: Try to limit answers to the Perl core and not CPAN Please ...
142 votes
9 answers
373k views

How can I pass command-line arguments to a Perl program?

I'm working on a Perl script. How can I pass command line parameters to it? Example: script.pl "string1" "string2"
lamcro's user avatar
  • 6,139
139 votes
6 answers
269k views

Escaping a forward slash in a regular expression

My question is a simple one, and it is about regular expression escaping. Do you have to escape a forward slash / in a regular expression? And how would you go about doing it?
Zerobu's user avatar
  • 2,611
135 votes
6 answers
140k views

How can I output UTF-8 from Perl?

I am trying to write a Perl script using the utf8 pragma, and I'm getting unexpected results. I'm using Mac OS X 10.5 (Leopard), and I'm editing with TextMate. All of my settings for both my editor ...
user avatar
135 votes
2 answers
331k views

How do I enter a multi-line comment in Perl? [duplicate]

Possible Duplicate: What are the common workarounds for multi-line comments in Perl? How do I add a multi-line comment to Perl source code?
134 votes
3 answers
393k views

Check whether a string contains a substring

How can I check whether a given string contains a certain substring, using Perl? More specifically, I want to see whether s1.domain.example is present in the given string variable.
Belgin Fish's user avatar
  • 19.6k
133 votes
10 answers
256k views

How to match a substring in a string, ignoring case

I'm looking for ignore case string comparison in Python. I tried with: if line.find('mandy') >= 0: but no success for ignore case. I need to find a set of words in a given text file. I am ...
Mandar Pande's user avatar
  • 12.6k
133 votes
4 answers
83k views

Perl flags -pe, -pi, -p, -w, -d, -i, -t?

I have seen lots of ways of running Perl code or scripts, with different flags. However, when I try to google for what each flag means, I mainly get results to generic Perl sites and no specific ...
Tudor Constantin's user avatar
131 votes
18 answers
198k views

In Perl, how can I read an entire file into a string?

I'm trying to open an .html file as one big long string. This is what I've got: open(FILE, 'index.html') or die "Can't read file 'filename' [$!]\n"; $document = <FILE>; close (FILE); ...
125 votes
8 answers
314k views

How can I check if a file exists in Perl?

I have a relative path $base_path = "input/myMock.TGZ"; myMock.TGZ is the file name located in input folder. The filename can change. But the path is always stored in $base_path. I need to ...
superstar's user avatar
  • 1,832
124 votes
10 answers
107k views

How do I tell CPAN to install all dependencies?

How do I tell CPAN to install all dependencies? 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 ...
Nifle's user avatar
  • 11.8k
123 votes
9 answers
141k views

What's the safest way to iterate through the keys of a Perl hash?

If I have a Perl hash with a bunch of (key, value) pairs, what is the preferred method of iterating through all the keys? I have heard that using each may in some way have unintended side effects. ...
Rudd Zwolinski's user avatar
123 votes
4 answers
21k views

Why are Perl 5's function prototypes bad?

In another Stack Overflow question Leon Timmermans asserted: I would advice you not to use prototypes. They have their uses, but not for most cases and definitely not in this one. Why might ...
Alnitak's user avatar
  • 337k
122 votes
7 answers
126k views

Using output from a previous job in a new one in a GitHub Action

For (mainly) pedagogical reasons, I'm trying to run this workflow in GitHub actions: name: "We 🎔 Perl" on: issues: types: [opened, edited, milestoned] jobs: seasonal_greetings: runs-on: ...
jjmerelo's user avatar
  • 23k
122 votes
10 answers
110k views

Should I choose ActivePerl or Strawberry Perl for Windows? [duplicate]

I'm totally new to Perl, but I'd like to try it out. I read about two rival distributions available for Windows platform (I guess there's just Perl on other OSes :). Wikipedia says that Strawberry ...
Andrei Андрей Листочкин's user avatar
121 votes
8 answers
310k views

How to extract string following a pattern with grep, regex or perl [duplicate]

I have a file that looks something like this: <table name="content_analyzer" primary-key="id"> <type="global" /> </table> <table name="content_analyzer2" ...
wrangler's user avatar
  • 2,025
114 votes
7 answers
267k views

What is the proper way to check if a string is empty in Perl?

I have been using this code to check if a string is empty: if ($str == "") { // ... } And also the opposite with the not equals operator... if ($str != "") { // ... } This ...
Nick Bolton's user avatar
  • 38.9k
114 votes
4 answers
73k views

How are nested capturing groups numbered in regular expressions?

Is there a defined behavior for how regular expressions should handle the capturing behavior of nested parentheses? More specifically, can you reasonably expect that different engines will capture ...
Alana Storm's user avatar
111 votes
9 answers
161k views

How can Perl's print add a newline by default?

In Perl most of my print statements take the form print "hello." . "\n"; Is there a nice way to avoid keeping all the pesky "\n"s lying around? I know I could make a new function such as myprint ...
Mike's user avatar
  • 59.8k
111 votes
8 answers
70k views

Why use strict and warnings?

It seems to me that many of the questions in the Perl tag could be solved if people would use: use strict; use warnings; I think some people consider these to be akin to training wheels, or ...
TLP's user avatar
  • 67k

1
2 3 4 5
1363