Perl SAQ

A SAQ is a "Self Answered Question".

This collection of Perl SAQ's are taken mostly from comp.lang.perl.misc. References removed to protect the innocent (and perhaps, somewhat naive). Newest entries are at the top.

Added 30 May 2006

how do i chomp the \n of a string? 

If the question was chomped back any further, only the answer would be left.

Added 8 Apr 2006

Hi all 

I have a string like so 


I need to split and add it to an array 

my method of 

($abc) = $test =~ /\./g; 

does not seem to provide me with a satisfying result 
and I get back is the period. 

If anyone could assit, it would be most helpful 

Perhaps if he could split two brain cells, the answer might be obvious.

Added 15 Mar 2006

Can someone let me know how to get target name of a symbolic link and
store it into a variable? For example

    system( "readlink $link" );

only prints the target name. I would like to store the target name into
a variable, say $target, without printing it.

Ultimately I would like to have a procedure that copies a regular file
and one or more of its symbolic links, into another file and another
set of symbolic links pointing to the copy of the original file. In
other words a set of files should be copied so that their link
structure is preserved. Is there any high level function that can do
the job? 

Hopefully the OP can readlinks to the documentation.

Added 4 Feb 2006

If I use the following to load a file into an array, is there a simple
way of reversing the arrays contents?


Maybe if I was able to reverse the question, I'd come up with the answer.

I use 
while(<INFILE>) { 
  $line = $_; 


to get one line from the file, 

The line may like: sum  11  32 
How to split lines into array like @myarray = {"sum","11","32"}; which 
knowing how many space between the words? 

Any suggestions will be appreciated! 

Perhaps if we split the question into single words, the answer would come to us in a split second.

How to let program sleep several seconds in perl?

Has the poster's brain gone to sleep?.

OK... This may seem like a dumb question, but I haven't figured out a way to 
do this yet...

Given a directory, I want to be able to distinguish which entries are files 
and which are directories... Actually I only want to list the directories in 
my output.

In a shell script I can easily accomplish this:


cd /home/rbarnes

ls > /tmp/workfile

cat /tmp/workfile | \
while read VAR

if [ -d $VAR ]
echo $VAR

rm /tmp/workfile

How can I accomplish the same task in PERL... basically, if I had the TEST 
command equiv in PERL I believe I could do this.

Always good to test a dumb question isn't a self answering question with a -d.

THis has got to be documented somewhere, but where?  Couldn't find it
in learning perl - where they document what happens, but not how to get
around it, nor in perlreftut or perldsc.  It's an obvious problem so
please point me where to look.

I have a hash, $hash.

If may or may not have an entry

that entry if it exists, may or may not have the value 0.

How do I determine if it exists without warnings if it doesn't?  

I wonder if such a function exists.

I need all files created to be -rw-rw-rw , on unix I would put at the top of 
the script:

umask 0

Can I use umask on perl ?

#!/usr/bin/perl -w
umask 0

If only I could umask my aversion to reading the documentation.

Hello ... if I had a hash say %hash ... how can I get the 
value of $hash{$variableKey}

There must be such little value in hashing this question over and over.

is there such a thing as a perl / cgi
script that will do a CHMOD on a
specified file - in a specified directory.....

Perhaps someone should chmod their brain to look up the obvious function.

I want my perl script to be able to kill a process given its pid. I 
could invoque the UNIX kill -9, but I would prefer something provided by 
the perl language.
Is there something in perl for this?

I could just kill myself when I find the answer.

how to rename a file in a certain directory ?

let s say i want to change  ../bad.html 

to  ../good.html 

Which lines do I need for this ?

Obviously never heard of the rename function.

Hi perl gurus,

If I run something like

@arr = `cat /etc/passwd 2>&1 `;

How do I check whether the cat command succeeded or failed?

In other words is there something like "$?"

In other words, it is exactly like $?

In Unix, we have fork() to spawn a process & Exec().
Is there something in Perl like that? 

Surely Perl wouldn't have a fork or exec function.

Dear Perl Gurus,
I have a question on how to get the length of string in Perl. Is there any
command in Perl to do that or I have to do some trick in order to solve this
? Pls advise.

For example :

$a = "eat,run,sleep";

I want to know the length of $a ( how many chars ).  Should be 13 in this
case. Any idea ?

Thanks a lot for your help

The lengths some people go to asking a SAQ.

Is it possible to position the perl print command like in unix ksh
print "\033[1;37HHello"

How can i do this in perl

I think I saw somewhere that Perl had a print function.

how do i delete a particular key from a hash

Did this person delete part of their brain?

I do not have cron access to the server where the
script is hosted, so I was thinking of using
something like a sleep or suspend in my script,
but I do not know the right function to call.

Looks like their logic has certainly gone to sleep.

Subject: Is there a kind of sleep or wait function? 

I would like to wait a while inside a script. Is this possible?
If yes - how?

Zzzzzzzzzz...... See above

foreach $a(@b)
	if ... { ... }; # next loop no further execution of this foreach


Is there a command to realize this?

The next time someone asks this question, maybe they should look at the docs.

If I have a variable, $whatever, storing a string like "I will burn
you", then how can I find the length (in characters, and probably
spaces) of that string?

It might help setting $whatever to "I will check the perl docs for a length function before posting".

I know this is a simple question with an even simpler answer but:
How can I sort an array of file names so then i can figure out which one has
the alphabetically last name?

This person needs to sort out how to read the perl docs.


is there a way to split a string by another string?



Perhaps if we split their brain in half, we can find a way.

I need to find a way to truncate the file.

The full question has been truncated for full effect.

I need to build a regular expression that matches the following

This is a sentence with some words
This is a sentence containing some words

I want to be able to tell the regexp that you can choose the word
"with" or "containing" in the middle of the sentence.

I know I can use the OR operator(|) and put it between two regexp

/^This is a sentence with some words/|/^This is a sentence containing
some words/

but is there a way to shorten the regex? Something like 
/^This is a sentence (containing|with) some words/ ?

A great way to shorten the time it takes to answer the question is to try it before posting it to a newsgroup.

how do I get the string length of a string?

The length of this question is in direct proportion to the OP's ability to use the documentation.

Sorry, but how does one move up a directory ?

I'm in a directory from which I called a script. Part of the script
functionality is checking the permissions of the directory I am in. I may
need to change the directory permissions depending on what they are. Hence,
I need to be able to move up [and possibly down] one level of the directory

chdir? Something like cd .. would be nice

Something like chdir would be nice.

Is there a way to know if a command executed via backticks was
successful or not?
my $res = `shell_command`;

What would I have to check to obtain this information? Would it be one of
$? variables? Any pointers to perldocs will be helpful.

In fact, it might just be the $? variable.

Email Peter Sundstrom if you have some more examples.
Please include URL or message ID.