Two Questions [perl]

Well, im kinda a newb to Applescript, but am familiar with many other languages so i have two questions/problems

first one is how to i delete all the contents of a file. i want to write to it later and i want it to be cleared out before that

and then also how to i get it to return a string from a perl script


set GUID to (do shell script "perl #!/usr/local/bin/perl
$length = 36;
$possible = '123456789ABCDEFGHJKLMNPQRSTUVWXYZ';
while (length($password) < $length) {
if (length($password) == 8 || length($password) == 13 || length($password) == 18 || length($password) == 23) {
$password .= " - ";
}
$password .= substr($possible, (int(rand(length($possible)))), 1);
}
echo $password;")

but it gives me the error “cant make…into type number” because as you can see it actually returns a 36 letter/digit string

thanks for all the help

Please post your question separately with an appropriate subject. This make the thread easier to follow, and it helps people to find things later when searching.

sorry bout that

i was able to solve my first problem, but now i just need help with the perl, so thanks to anyone who can help

Something this simple should work:

property perlPath : "/Users/administrator/TEST/perlscript.pl"
(* perlscript.pl:
#!/usr/bin/perl
print "Hello.\n";
*)
set rslt to do shell script (quoted form of perlPath)
rslt -- "Hello."

Dealing with parameter passage is just slightly more involved, but is this all you’re looking for?

  • Dan

First, you need to escape the double quotes inside the script; Otherwise AppleScript thinks you’re closing the string.

Second, you need to check out the man for perl to see how to run commands from the command line; You give perl the -e option, followed the code to run (enclosed by quotes to protect it from the shell). (Also, you don’t need a hashbang line when you’re passing the script to the interpreter.)

Finally, echo causes errors on my machine; I think you want print instead.

set GUID to (do shell script "/usr/bin/perl -e'
$length = 36;
$possible = \"123456789ABCDEFGHJKLMNPQRSTUVWXYZ\";
while (length($password) < $length) {
	if (length($password) == 8 || length($password) == 13 || length($password) == 18 || length($password) == 23) {
		$password .= \" - \";
	}
	$password .= substr($possible, (int(rand(length($possible)))), 1);
}
print $password;'")

Side note: A native subroutine (which I just posted): randomString()

With that, you could do something like this:

set GUID to randomString(8) & " - " & ¬
	randomString(2) & " - " & ¬
	randomString(2) & " - " & ¬
	randomString(2) & " - " & ¬
	randomString(10)

A bit OT, but if you already know Perl, check out Chris Nandor’s Mac::Glue and Mac::OSA::Simple modules if you’ve not already done so - you may find them of use.

http://search.cpan.org/~cnandor/

HTH