# Perl scripting



## matthisd (Oct 21, 2008)

Hi, I am totally new at perl, and everyone here has never used perl on Mac OS X. I am running Leopard 10.5.5

I have a perl script that is supposed to read from a text file with a list of words, input each word into a phrase and write that new phrase into a new file. I have the input and output files in the same folder as the .pl script. I run the script using " perl path/to/file " in the terminal. it does not seem to access the files in the same folder as the script, and creates the output file in /users/home (this really confuses me). 

Here is the script:
---------------------------------------------

use strict;
use warnings;

my @inputfiles;
my $j;
my @column;
my $line;

@inputfiles=glob 'audio_file_list.txt';					

open OUT, "+>file_path_list.txt";							


for ($j=0;$j<=$#inputfiles;$j++)
	{

	open IN, "<$inputfiles[$j]";					 
	print "$inputfiles[$j]\n";

	while (defined ($line = <IN>))					
		{				
		chomp $line;						

		print OUT "sound {wavefile {filename = \"/Audio/$line.wav\";};} $line;\n";
		}
	}

close IN;
close OUT;

--------------------------------------
Using "use warnings" i get one warning:
*readline() on closed filehandle IN at /Users/home/Desktop/EMOFR/Recordings/recording_list/writefromlist.pl line 20.*

I think thats it. If you ask me something I will not be able to answer till tomorrow morning (I live in Germany) 
Thanks!!!


----------



## Mikuro (Oct 21, 2008)

My perl skills are rusty (and were never that good to begin with), but I think I see the problem: the working directory of your perl script is, I believe, inherited from the shell; it is NOT automatically set to the location of the script. So if you just refer to "audio_file_list.txt", that means the file "audio_file_list.txt" in the shell's working directory (by default your Home folder).

You should be able to get the location of your script by accessing $0, and then base the path to your file off of that.


----------



## matthisd (Oct 22, 2008)

thanks mikuro! just a couple things (I am all new to perl). I was wondering where I would put the $0 sign (haven't dealt with that yet.
Plus, I finally figured out something, I put my input file under /users/d_mat/ and it worked!! So that is definitely the problem. Ich you could just let me know how to use $0 that would be great.
Is this how everyone using perl in Mac OSX has to work? perl scripts never just access files from where they are?? that doesn't make much sense to me...


----------



## ElDiabloConCaca (Oct 22, 2008)

Perl scripts, like Mikuro said, will access the files from where you launch perl... in this case, if you're simply opening the Terminal (which defaults the current working directory to your home folder -- you can see this by typing 'pwd' which is short for "print working directory"), then your perl script is looking for files in that directory (because you don't explicitly define a directory to look within in your script -- only a filename).

You could hard-code the directory into the script... instead of:

```
@inputfiles=glob 'audio_file_list.txt';
```
...you could put:

```
@inputfiles=glob '/path/to/audio_file_list.txt';
```
...and similarly in other places where you define a filename.



> perl scripts never just access files from where they are??


Perl _is_ accessing files "where they are."  Perl is being called from your home directory (because that's where "you are" when you launch your script via 'perl /path/to/script.pl'), so it's looking for those files in the current directory.  If you do a 'cd ~/Documents' before you 'perl /path/to/script.pl', then perl will look for the files in ~/Documents.


----------



## Mikuro (Oct 22, 2008)

$0 is automatically defined by perl, so you can use it from anywhere like a normal variable. For example:

```
#!/usr/bin/perl

print $0, "\n";
```
Save that as a file and it'll output the path to that file when you run it.

So to get the folder, you'd want to take $0, remove the last path component, and add a new path component (the name of the file you want to open). Again, I'm not a perl expert, so I don't know what the 'right' way to do this is, but I think File::Spec is the way to go. See splitpath() and catpath() specifically.


----------



## matthisd (Oct 23, 2008)

Thanks a lot guys! I've finally been able to get my script working. Of course I did write out the full path at IN and OUT 
I realized I would need more time to understand $0 and File::Spec. So that is in the plans as soon as I can fit it in 
Thanks again!


----------

