Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Locked thread
fansipans
Nov 20, 2005

Internets. Serious Business.

vozz posted:

It's supposed to generate random strings based on @dict. And I'm trying to get this to work on my web server, not the command line.

Do you really want it to print out each character? Are you expecting it to return a value? Each letter it generates is immediately printed out, and not returned.

You could just add:
code:
return "@pw"
Which would stringify it, or return @pw which would return the array - be careful though because when @pw is evaluated in scalar context it returns the size of the array, not a nice happy string.

What I mean by this is that "my $password = randPw(7)" would assign the length of the returned array to $password, which is the number 7.

Here's a slightly less verbose implementation, that uses the same "iterate through the allowed characters" idea:

code:
sub randPw {
  my $len = shift;
  my @a=("A".."Z","a".."z");
  my $s;
  $s .= $a[rand(@a-1)] for 1..$len;
  return $s;
}

fansipans fucked around with this message at 19:40 on Dec 10, 2007

Adbot
ADBOT LOVES YOU

fansipans
Nov 20, 2005

Internets. Serious Business.

SpeedFrog posted:

It might also be a symbolic reference:

code:
> $foo = "bar";
> $var = "foo";
> print $$var;
bar
This is pretty horrible programming practice and shouldn't be used on a widespread basis, if at all.

Also if you're using strict and warnings as you should at the top of every file:
code:
use strict;
use warnings;
then Perl will barf at you with the code above, because symbolic references are pretty smelly:
code:
Can't use string ("foo") as a SCALAR ref while "strict refs" in use at -e line 1.

fansipans
Nov 20, 2005

Internets. Serious Business.
I'll second SpeedFrog's recommendation of DBIx::Class. I started with Class:: DBI but got frustrated with the lack of features / extensibility / plugins available. I've heard Rose:: DB is a screamer, but haven't gotten around to taking a look at it.

DBIx::Class has many hooks and a great selection of third party plugins for profiling & debugging & adding new features. It's also got great support for the basics of database / object relationships (belongs_to, may have, has_one, has_many, many_to_many).

fansipans
Nov 20, 2005

Internets. Serious Business.

GregNorc posted:


code:
#!/usr/bin/perl

sub total{
        my $sum;
        foreach(@_){
                $sum += $_;
                   }
        $sum;
        }
my @fred = qw{ 1 3 5 7 9};
my $fred_total = &total(@fred);
print "The total of \@fred is $fred_total.\n";
print "Enter some numbers on seperate lines.";
my $user_total = &total(<STDIN>);
print "The total of those numbers is $user_total";

The code above works fine in OSX 10.4 for me.

Also there's no reason to prefix sub names with &.

fansipans fucked around with this message at 11:58 on Apr 28, 2008

fansipans
Nov 20, 2005

Internets. Serious Business.

LightI3ulb posted:

HELP!

What the hell is going on with this?

Why is it giving me syntax errors?

Always, always, always include these two lines:

code:
use strict;
use warnings;
Including those two lines doubles the error messages your script outputs, and will probably help you quickly identify where the problem is.

fansipans
Nov 20, 2005

Internets. Serious Business.

Triple Tech posted:

You can also use print Data::Dumper $structure.

Triple Tech nailed it on the head: Data::Dumper is your super number 1 friend:

code:
use Data::[b][/b]Dumper;

# blah blah blah code
# deep in the bowels of your lovely app:

print Dumper($g_mapTransform);

fansipans
Nov 20, 2005

Internets. Serious Business.

mister_gosh posted:

How can I NOT store the path for the files I put in a zip archive?

In other words, when you open the zip file, each file has a path of abc/def (see $myDir below). I don't want a user extracting the zip and having it generate the directories abc/def. I just want it to extract to a directory equaling the filename of the zip.

For what it's worth, here's the code-

code:
use Archive::Zip qw( :ERROR_CODES :CONSTANTS );
              
my $myDir = "C:/abc/def";
          
my $zip = Archive::Zip->new();
my $zipped;
my @fileList;
find sub { push @fileList, $File::Find::name if basename($File::Find::name) =~ /\.xml/ }, $myDir;

foreach $file (@fileList) {
   $zipped = $zip->addFile( $file );
}
if($zip->writeToFileNamed( "C:/sa.zip" ) != AZ_OK) {
   print "you loving bitch\n";
}
Thanks in advance!

From the Archive::Zip documentation, it appears that addFile will also accept a new name for the file once it's added. Keep in mind "filename" for zips includes their path, so you should be able to do:

code:
  $zip->addFile("C:/punk/junk/funk.txt", "funk.txt");
or with File::Basename:

code:
  $zip->addFile($file,basename($file));
Which is funny, because I already had those two URLs open in my browser :psyduck:

fansipans
Nov 20, 2005

Internets. Serious Business.

Mithaldu posted:

I have literally been looking for advantages of it over the past two weeks and aside from it being a different syntax (which is a disadvantage for me) i have found exactly none. It being slower than for only drives the nail in its coffin of uselessness for me.

It makes code a hell of a lot more readable ...

code:
my @names = qw/alice bob carol dan/;
my @name_lengths = map { length } @names;

fansipans
Nov 20, 2005

Internets. Serious Business.

Mithaldu posted:

Not to me. Looks more like perl golf than anything else.

Edit: I think my problem is that i mentally translate every code i see into actual english sentences. That works very well for most constructs of Perl unless golfed. Map is impossible for me to translate into an english instruction.

That reminds me of learning Spanish back in High School.

There were kids who did all their homework, memorized every word in the book, and got straight A's. But when it came time for spoken exams, you'd see their eyes roll up to the ceiling, and the words come out one at a time.

Then there were a few kids that could flip a switch, and speak almost entirely in Spanish. They'd often have a few pet phrases that they'd incessantly discuss in a variety of situations. Mine was "vaca", the word for cow. "The cow has gone to the police station to complain about the loud children and their annoying cars", "The drunken cow sang a song using the broccoli as a microphone", etc. These kids would accidentally keep speaking Spanish when they left class. They might not remember the word for brakes on a car, but instead they'd just say "la machina para hacer parar" (the machine to make to stop)

Both types of student could certainly survive if stranded in Madrid or Mexico City, but the latter would probably have much more fun :)

fansipans
Nov 20, 2005

Internets. Serious Business.

EVIR Gibson posted:

What does the following mean. I knew it had to do something with a key->value or perhaps a method but the second line I am not getting.
code:
$a->b;
$a->{b};

The first one is a method call on a blessed object. The method called is "b", and the package this looked up under is the package that the object $a was blessed with. See the Method Invocation section of perlobj.

The second one is accessing the key "b" from the hash reference $a.

fansipans
Nov 20, 2005

Internets. Serious Business.

SpeedFrog posted:

Best practices for global configuration data? I'm leaning towards handing out a JSON struct via REST, as it's a fair bit quicker than reading a file off disk and doesn't go stale as easily. Am starting to feel the pain of redundant configuration files.

Global configuration data in what context? What needs configuration? (Perl Questions -> JSON -> REST :raise:)

Adbot
ADBOT LOVES YOU

fansipans
Nov 20, 2005

Internets. Serious Business.

Sartak posted:

DBIx::Class seems to be the latest and greatest. It's the most actively developed and maintained.

Seconded. It's got very comprehensive documentation too.

I've also heard good things about Rose::DB

  • Locked thread