I am crafting a Perl script together at work that will generate a random number within a specified range, use that number to open up a specific file, then mail the file to a specified recipient. The script will then be executed once a week via a cron job.
The question I have concerns the use of the open() function. Essentially, the script calls rand() to generate a random number.
# Call a random number from a range of 1 to 97.
my $range = 97;
my $minimum = 1;
my $random_number = int(rand($range)) + $minimum;
I then want to use $random_number as part of the specific filename I need to open. I am setting the variable to call later in my script as follows:
my $logfile = “F:/dir1/dir2/tip”$random_number”.txt”;
Is the pathname I am giving correct to allow for the $random_number to be inserted correctly? I am planning on testing, but I am waiting for all of the files to be created by the person in question. I would just like to get most of the script complete.
#1 by Paul on December 12, 2006 - 4:53 pm UTC
yes you can use a variable inside an open() statement. for example:
Convert.pl:open(FILEIN,” $OutputFile”) || die “open() on $OutputFile failed. $!\n”;
and if you want to go (perhaps) overboard youc an do things like this:
open(ACTIVE_HOSTS, “$NMAP $NMAP_OPTIONS -PT \
$PingPort $NETWORK | \
$GREP -v \”Nmap run completed\” | \
$GREP -v \”Starting nmap\” | \
$GREP -v \”TCP probe port is\” |”) || \
die “failed to open pipes: $!\n”;
push(@ActiveHosts,);
close(ACTIVE_HOSTS);
You now have the resulst of that nmap (minus the grep -v’s) in the array ActiveHosts.
#2 by Paul on December 12, 2006 - 4:53 pm UTC
yes you can use a variable inside an open() statement. for example:
Convert.pl:open(FILEIN,” $OutputFile”) || die “open() on $OutputFile failed. $!n”;
and if you want to go (perhaps) overboard youc an do things like this:
open(ACTIVE_HOSTS, “$NMAP $NMAP_OPTIONS -PT
$PingPort $NETWORK |
$GREP -v “Nmap run completed” |
$GREP -v “Starting nmap” |
$GREP -v “TCP probe port is” |”) ||
die “failed to open pipes: $!n”;
push(@ActiveHosts,);
close(ACTIVE_HOSTS);
You now have the resulst of that nmap (minus the grep -v’s) in the array ActiveHosts.
#3 by Notorious R.O.B. on December 13, 2006 - 9:58 am UTC
Haven’t used Perl 6, so I don’t know if they’ve fixed this, but if I may draw back several years when I programmed in Perl, you’ll want to add the “.” character to concatenate the pieces of $logfile. For example:
my $logfile = “F:/dir1/dir2/tip” . $random_number . ”.txt”;
Another thing – as I’m sure you know, random numbers aren’t truly random. I don’t know how secure this needs to be, but you can use the srand function before calling rand to change the seed used.
If you want something even more random than that, there’s a package called Math::TrulyRandom that you can find in CPAN.
#4 by Notorious R.O.B. on December 13, 2006 - 9:58 am UTC
Haven’t used Perl 6, so I don’t know if they’ve fixed this, but if I may draw back several years when I programmed in Perl, you’ll want to add the “.” character to concatenate the pieces of $logfile. For example:
my $logfile = “F:/dir1/dir2/tip” . $random_number . ”.txt”;
Another thing – as I’m sure you know, random numbers aren’t truly random. I don’t know how secure this needs to be, but you can use the srand function before calling rand to change the seed used.
If you want something even more random than that, there’s a package called Math::TrulyRandom that you can find in CPAN.
#5 by Jason J. Thomas on December 13, 2006 - 10:06 am UTC
Paul: Hmmm…that’s good to know. I am just dangerous with Perl, and now I get to become moreso. It just sucks that I have to run it on Windows.
R.O.B.: That was the answer I was looking for. I wanted to make sure that I had the concatenation right. I will be seeing if this works in short order.
As for the randomness, I am really just picking from a bunch of text files, so I could care less. As long as it randomizes what it picks, that works. No need for it to be secure.
#6 by Jason J. Thomas on December 13, 2006 - 10:06 am UTC
Paul: Hmmm…that’s good to know. I am just dangerous with Perl, and now I get to become moreso. It just sucks that I have to run it on Windows.
R.O.B.: That was the answer I was looking for. I wanted to make sure that I had the concatenation right. I will be seeing if this works in short order.
As for the randomness, I am really just picking from a bunch of text files, so I could care less. As long as it randomizes what it picks, that works. No need for it to be secure.
#7 by mokiejovis on December 13, 2006 - 11:41 am UTC
Here, I’ll be the typical UNIX ass:
Why would you want to do that? No one should ever have to do that. Furthermore, I know how to do exactly what you’d want to do, but since no one should ever need to do that, I won’t tell you, and my assholery will be visible to google searchers for years to come.
Seriously though, I don’t even touch that stuff. Perl is greek to me.
#8 by mokiejovis on December 13, 2006 - 11:41 am UTC
Here, I’ll be the typical UNIX ass:
Why would you want to do that? No one should ever have to do that. Furthermore, I know how to do exactly what you’d want to do, but since no one should ever need to do that, I won’t tell you, and my assholery will be visible to google searchers for years to come.
Seriously though, I don’t even touch that stuff. Perl is greek to me.
#9 by Jason J. Thomas on December 13, 2006 - 12:04 pm UTC
My scripting-fu is strong, as I was able to get this to work as expected. Kneel before JJT!
Hey, mokie, there’s a few things you are missing to be the typical UNIX ass: a beard and suspenders. Once you do that, you will fit the mold.
#10 by Jason J. Thomas on December 13, 2006 - 12:04 pm UTC
My scripting-fu is strong, as I was able to get this to work as expected. Kneel before JJT!
Hey, mokie, there’s a few things you are missing to be the typical UNIX ass: a beard and suspenders. Once you do that, you will fit the mold.